0

as i said in the title im trying to replace a string in a file, that contains special characters , now the idea is to loop on every line of a "infofile" contains many lines of: whatiwantotreplace,replacer. once I have this i want to do sed to a certain file to replace all the occurrences of string-> "whatiwantotreplace" with ->"replacer". my code:

infofile="inforfilepath"
replacefile="replacefilepath"

while IFS= read -r line
do
   what2replace="a" #$(echo "$line" | cut -d"," -f1);
   replacer="b\\"  #$(echo "$line" | cut -d"," -f2 );
   sed  -i -e "s/$what2replace/$replacer/g" "$replacefile"
   #sed  -i -e "s/'$what2replace'/'$replacer'/g" "$replacefile"
   #sed  -i -e "s@$what2replace@$replacer@g" "$replacefile"
   #sed  -i -e s/$what2replace/$replacer/g' "$replacefile"
   #sed  -i -e "s/${what2replace}/${replacer}/g" "$replacefile"
   #${replacefile//what2replace/replacer}

done < "$infofile"

As you can see, the string that want to replace and the string that i want to replace with,may contain special characters , all the commented lines are the things I tried (things I saw online) but still clueless.

for some i got this error: "sed: -e expression #1, char 8: unterminated `s' command" and for some just nothing happend.

really need your help

Edit: inputs and outputs: It's hard to give inputs and output, because all of the variations I tried had the same thing , didn't changed anything, the only one gave the above error is the variation with @. thanks for your effort.

albert1905
  • 147
  • 1
  • 3
  • 12
  • 5
    Can you edit question and provide some sample input data and expected output. – anubhava Feb 20 '18 at 15:00
  • 1
    also, if you check https://stackoverflow.com/tags/sed/info, there's https://stackoverflow.com/questions/29613304/is-it-possible-to-escape-regex-metacharacters-reliably-with-sed which might help... – Sundeep Feb 20 '18 at 15:19

2 Answers2

1

You can try this way

what='a';to='b\\\\';echo 'sdev adfc xdae' | sed "s/${what}/${to}/g"

output

sdev b\\dfc xdb\\e
ctac_
  • 2,413
  • 2
  • 7
  • 17
  • Thanks, but I need the command to work on a file, I cant echo every line, it'll take forever. and I tried that specific "sed" variation (sed "s/${what}/${to}/g"). – albert1905 Feb 20 '18 at 16:23
  • what='a';to='b\\\\';sed "s/${what}/${to}/g" "${infofile}" > "${replacefile}" – ctac_ Feb 20 '18 at 16:42
1

You're barking up the wrong tree - you're trying to do literal string replacements using a tool, sed, that doesn't have functionality to handle literal strings. See Is it possible to escape regex metacharacters reliably with sed for the convoluted mess required to try to force sed to do what you want and also https://unix.stackexchange.com/q/169716/133219 for why to avoid shell loops for manipulating text.

Just use awk instead since it has literal string functions and loops implicitly itself:

awk '
NR==FNR{map[$1]=$2; next}
{
    for (old in map) {
        new = map[old]
        head = ""
        tail = $0
        while ( s = index(tail,old) ) {
            head = head substr(tail,1,s-1) new
            tail = substr(tail,s+length(old))
        }
        $0 = head tail
    }
}
' "$infofile" "$replacefile"

The above is untested of course since you didn't provide any sample input/output.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • thanks for your answer, can you please help me to understand your code? so that next time Ill be able to stand on my own two feet by my own. Thanks again. – albert1905 Feb 21 '18 at 07:01