3

I am using GNU SED for find and replace functionality on large files(upto 2GB).

Find and replace characters can contain any characters, hence I want find and replace parameters to be treated as plain text as it comes.

I do not want to treat either find or replace parameters as regex by sed command.

I have experimented a lot, but every time I am getting new combinations of regex which does not work for sed as plain text.

How can this be achieved?

Is there any formula to escape the special characters?

Note: I am using ~ operator as command seperator instead of /

Below is the example

sed -ne "s~^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$~Replace" -ne "w output.txt" "input.txt"

Above command does not work, as it treats the find parameter as regex(as it is regex). Hence to find the text I have to escape some special characters in regex as below

sed -ne "s~\^\[-+\]?\[0-9\]\*\\.?\[0-9\]+(\[eE\]\[-+\]?\[0-9\]+)?\$~Replace" -ne "w output.txt" "input.txt"

In another example I have to modify .*$ to .\*\$ But in (.*$) I do not want to mofify input.

So is there any universal rule for escape sequence?

sagar
  • 139

1 Answers1

1

Q: Is there any formula to escape the special characters?
Q: Is there any universal rule for escape sequence?

A: You can use the corresponding hex code for special characters, in cases where just typing /,.,*,?,$, etc. becomes annoying. For example:

sed -rn '/\x22/p' file

will print lines that contain double quotes, since \x22 represents ".

If you need to look up hex codes, you can conveniently save them all to a file with this command:

gawk 'BEGIN{for(i=0;i<255;i++){printf("%d\t%x\t%c\n", i,i,i)}}' null >chars.txt
simlev
  • 3,912
Tyl
  • 231