1

I am getting the below error in my script. Can you please help identifying the issue?

sed "s/$(printf "\302")/ /g" file1 > file1_new
sed: -e expression #1, char 4: unterminated `s' command

But this one is working fine without issues:

sed "s/$(printf "\240")/ /g" file1 > file1_new
cat file1_new > file1
rm file1_new

What's the problem with the first one?

1
  • 1
    Probably less an sed issue but more about escaping: The " of the printf terminates the " of the script?
    – Philippos
    Commented Mar 7, 2017 at 16:39

2 Answers 2

0

It appears sed doesn't play very nicely with binary substitutions. I did just test what you're doing with tr though and it appears to be working. It's actually pretty easy to do with one command, also:

cat /path/to/file1 | tr "$(printf "\302\240")" " " > /path/to/file1_new

tr is a tool to "translate" the set of characters in its first argument into the set in its second argument (which is extended to match the length of the first set if needed). So we simply translate each character into a space, as you had been trying to do with sed.

1
  • Thank you so much. That worked :-) Really appreciated your help. Commented Mar 7, 2017 at 16:45
0

Note that you can access octal/hex numbers directly in sed as:

sed -e 's/\o302/ /g'  #in octal
sed -e 's/\xC2/ /g'   #in hex
1
  • I don't think this is portable to all sed flavors
    – Philippos
    Commented Mar 7, 2017 at 18:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .