0

I have a file pattern.txt which is composed of one very long line of complicated code (~8200 chars).

This code can be found in multiple files inside multiple directories.

I can easily identify a list of these files using

grep -rli 'uniquepartofthecode' *

My concern is how do I replace it with the exact text from within the file ?

I tried to do:

var=$(cat pattern.txt)
sed -i "s/$var//g" targetfile.txt

but I got the following error :

sed: -e expression #1, char 96: unknown option to `s'

sed is interpreting my $var content as a regular expression, I would like it to just match the exact text.

The pattern.txt content could be more or less any combination of characters so I'm afraid I cannot escape every characters efficiently.

Is there a solution using sed ? Or should I use another tool for that ?

EDIT:

I tried using this solution to make a proper regex pattern from my text file. Is it possible to escape regex metacharacters reliably with sed

the overall process is:

var=$(cat pattern.txt)

searchEscaped=$(sed 's/[^^]/[&]/g; s/\^/\\^/g' <<<"$var")

sed -n "s/$searchEscaped/foo/p" <<<"$var" # if ok, echoes 'foo'

This last command displays "foo". $searchEscaped seems to be properly escaped. Though, this is not returning anything (it should display foo + the rest of the file without the matched part):

sed -n "s/$searchEscaped/foo/p" targetfile.txt
Community
  • 1
  • 1
TheTrueTDF
  • 184
  • 1
  • 17
  • 1
    Check this: http://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed – hek2mgl Mar 09 '16 at 08:42
  • Works great until `sed -n "s/$searchEscaped/foo/p" <<<"$search" # if ok, echoes 'foo'` Then I try `sed -i "s/$searchEscaped//g" targetfile.txt` but have no success – TheTrueTDF Mar 09 '16 at 09:24
  • You are using `-n` this suppresses sed's output. Can you show your input file? – hek2mgl Mar 09 '16 at 11:44

1 Answers1

0

I think that the best solution is to not use regular expressions at all and resort to string replacement.

One way to do this is using perl:

$ echo "$string_to_replace"
some other stuff abc$^%!# some more
$ echo "$search"
abc$^%!#
$ perl -spe '$len = length $search; 
while (($pos = index($_, $search, $n)) > -1) { 
    substr($_, $pos, $len) = "replacement"; 
    $n = $pos + $len; 
}' <<<"$string_to_replace" -- -search="$search"
some other stuff replacement some more

The -p switch tells perl to loop through each line of the variable $string_to_replace (which could easily be replaced by a file). -s allows options to be passed to the script - in this case, I've passed a shell variable containing the search string.

For each line of the file, the while loop runs through all of the matches of the search string. substr is used on the left hand of the assignment to replace a substring of $_, which refers to the current line being processed.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141