简体   繁体   中英

Adding escape back slash before every [ and ] in line using either sed or perl one liner

Ok so I have found the following which work providing the ] is followed by a space.

perl -pe s'/([\[\]])/\\$1/g'

perl -pe s'/([][])/\\$1/g'

sed 's/[][]/\\&/g'

Example lines

docs/[Heresay]_File.doc  
[Heresay]%20My%20File.doc

The first line doesn't get changed, I believe it is because it isn't followed by a space.

The second gets changed to

\[Heresay\]%20My%20File.doc

This is repeated across 100s of lines, all the successful ones have a %20 after the ], all the failed ones have another symbol. Some of the successful ones are following a / so the / before the [ is not breaking it.

Some additional info, it works directly as a command on the command prompt when I try it on a single sample, but breaks inside bash script processing many lines.

In addition in the script if I chain it twice so eg

sed 's/[][]/\\&/g' | sed 's/[][]/\\&/g'

Then it will work but of course then apply it twice.

Contrary to what you claim, all of your solutions work fine.


The first solution you posted works fine.

$ perl -pe s'/([\[\]])/\\$1/g' file
docs/\[Heresay\]_File.doc
\[Heresay\]%20My%20File.doc

Shorter:

$ perl -pe's/[\[\]]/\\$&/g' file
docs/\[Heresay\]_File.doc
\[Heresay\]%20My%20File.doc

The second solution you posted works fine.

$ perl -pe s'/([][])/\\$1/g' file
docs/\[Heresay\]_File.doc
\[Heresay\]%20My%20File.doc

Shorter:

$ perl -pe's/[][]/\\$&/g' file
docs/\[Heresay\]_File.doc
\[Heresay\]%20My%20File.doc

Understanding how it works relies on some pretty arcane knowledge, however. I don't recommend this solution.


The third solution you posted works fine.

$ sed 's/[][]/\\&/g' file
docs/\[Heresay\]_File.doc
\[Heresay\]%20My%20File.doc

This relies on the same arcane knowledge. That said, I don't know sed , and I haven't found a way to avoid this after a couple of quick tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM