8

I have a file that has content like this:

apple
b\all
cat
\34
egg

I want to remove all lines containing backslashes. I tried using

sed '/\/d' pdataf.txt

But it didn't work. What should I try?

3 Answers 3

11

You just have to escape the backslash (escape the escape!)

$ sed '/\\/d' pdataf.txt
apple
cat
egg
9

grep, printing all lines that do not have \:

grep -v '\\' pdataf.txt

Similarly awk:

awk '!/\\/' pdataf.txt
2

You need to escape the backslash (escape character) in order to replace it. And if your version of sed supports it, the -i (in-place) option will do the edits on your file without you having to provide an intermediate file. Also, if you use the -i option, note that it accepts a (recommended!) backup file extension, however if you do not provide one, it is useful to precede your sed command with -e to inform sed that you are not using a backup file extension.

Putting it all together:

# Run sed to remove lines with backslash in them

$ sed -i -e '/\\/d' pdataf.txt

# Cat your file to confirm edits

$ cat pdataf.txt
apple
cat
egg

You must log in to answer this question.