Delete Files with Backslash in Linux

How to delete lines containing backslash \

Let's consider file foo.txt as below:

foo
bar
sna\fu
baz

Any of below commands would remove lines with \:

grep -v '\\' foo.txt
sed -ne '/\\/ d; p' foo.txt

Remove backslashes with sed command in a file

Replaces sequences of 4 backslashes with 2 backslashes:

sed 's/\\\\\\\\/\\\\/g' input.txt

Alternatively, use {4} to indicate how many \ are matched:

sed 's/\\\{4\}/\\\\/g' input.txt

input.txt

'AAA' 'a\\\\b\\\\a\\'
'BBB' 'q\\l\\s\\'

output

'AAA' 'a\\b\\a\\'
'BBB' 'q\\l\\s\\'

You must escape special regex characters like \ with another \.

{ and } are also regex characters, however the ed-like tools (ed,vim,sed,...) don't recognize them as such by default. To use curly-braces to specify a regex count (e.g., {4}), sed requires you escape them (e.g., \{4\})

So...

  • escape \ to use it literally; not as a regex character
  • escape { and } to use them as regex characters rather than literal braces

remove backslash only from quote character using sed

Your command is almost correct. You just forgot to substitute with a double quote:

sed -i 's/\\"/"/g' file.txt

sed's s command substitutes the first part (between / here) with the second part. The g flag repeats the operation throughout the line. Your mistake was that the second part was empty, thus effectively deleting the whole \" string.

BTW, escaping the double quote is not necessary since your sed command is inside single quotes.

Delete files with double quotes in their names

If you want to delete the files containing double quotes "", you can do this.

find . -type f -name '*"*"*.svg' -exec rm -f -- {} +

Edit: You need to first go to your file path and then execute above command.

cd /media/bruno/HDD Externo/temp/

How do I use sed to remove a double backslash

You can choose another delimiter than / in your command:

sed 's;^//;;' file

Or, if you want to escape the /:

sed 's/^\/\///' file


Related Topics



Leave a reply



Submit