I have a file like this:
'AAA' 'a\\\\b\\\\a\\'
'BBB' 'q\\l\\s\\'
...
And I want to replace all occurrences of \\\\ with \\.
I tried this sed command:
sed 's/\\//g'
but this removes all \.
This is my output:
'AAA' 'aba'
'BBB' 'qls'
The output I want:
'AAA' 'a\\b\\a\\'
'BBB' 'q\\l\\s\\'
...
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...
\ to use it literally; not as a regex character{ and } to use them as regex characters rather than literal bracesWith any sed that supports EREs, e.g. GNU or AIX seds:
$ sed -E 's/(\\){4}/\\\\/' file
'AAA' 'a\\b\\\\a\\'
'BBB' 'q\\l\\s\\'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With