for j in $(cat ${list_B})
do
to_replace_2=$(grep $j ${useralias}_2)
sed "s/^${j}/${to_replace_2}/p" ${entries} > ${entries}_2
mv ${entries}_2 ${entries}
done
Hi,
I've the above sed command running in a script. Its basically looping through a file and replacing its beginning of line grep'd match in another file.. It works 90% of the time but sometimes i'll get a garbled error like the one below.. The var ${to_replace_2} can contain any random string of characters. I've also tried to use different separators but that didn't work either..
You know that some characters are special to seds command language, don't you? For instance:
x="abc" ; y="def"
sed "s/$x/$y/" /path/to/input > /path/to/output
will work as expected, but:
x="ab/c" ; y="def"
sed "s/$x/$y/" /path/to/input > /path/to/output
will not (and changing the delimiters will only move the problem around, because now some other character will cause the problem). The following even will work, but not in the expected way:
x="ab*c" ; y="def"
sed "s/$x/$y/" /path/to/input > /path/to/output
The only real solution to this is to escape the problematic characters and this is done by prepending them with backslashes:
x="ab/c" ; y="def"
x="$(sed 's/\/\\&/')"
sed "s/$x/$y/" /path/to/input > /path/to/output
Here is a quick (and unrefined!) solution which escapes the problematic characters. Use at your own risk:
Hi thanks for the response.. I do understand that sed has some characters that are special to the command language..
I don't quite see how your above example would fit into my for loop?
Are you saying it should look like the below?
for j in $(cat ${list_B})
do
to_replace_2=$(grep $j ${useralias}_2)
to_replace_2="(sed 's/\/\\&/')"
sed "s/^${j}/${to_replace_2}/p" ${entries} > ${entries}_2
mv ${entries}_2 ${entries}
done
I tried the first way you gave and it still doesn't seem to fix the issue.. (Although it did help me out further down my script..) Can't figure out why it stalls on one particular file.. It's formatted the exact same way as everything else..
Also tried your sed suggestion but I ended up just getting a load of the following:
My code is as follows but still getting a garbled command
for j in $(cat ${list_B})
do
to_replace_2="$( grep $j ${useralias}_2 |\
sed 's/\[/\\&/g
s/\]/\\&/g
s/[/*?{}]/\\&/g' \
)"
j="$( echo $j |\
sed 's/\[/\\&/g
s/\]/\\&/g
s/[/*?{}]/\\&/g' \
)"
sed "s/${j}/${to_replace_2}/1" ${entries} > ${entries}_2
mv ${entries}_2 ${entries}
done