Delete all lines containing backslash from text file
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?
You just have to escape the backslash (escape the escape!)
$ sed '/\\/d' pdataf.txt
apple
cat
egg
grep
, printing all lines that do not have \
:
grep -v '\\' pdataf.txt
Similarly awk
:
awk '!/\\/' pdataf.txt
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