2

As a novice linux user, I try to use sed to replace some text string. The new string is the content of a line of another file's. It basically works as intended apart from that a newline is inserted after the substituted text (which I dont want :-)

newString=`sed -n "$(($line+1))"p otherFile `
sed "s@oldString@$newString@" < oldFile > newFile

I unsuccessfully tried to remove a potential LF within newString (using different approaches I found online, among others the popular sed ':a;N;$!ba;s/\n/ /g' ) before I applied sed s.

Right now it only looks like as follows:

Some text to be replaced

turns e.g. into

Some text is
replaced

while I would like to have a single line result only. I guess the problem lies in the newString being read as a whole line via sed -n p. If I define manually a newString the substitute works as desired to produce a single line only.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
kermit
  • 23
  • 3
  • It is strange, I am doing some tests and it works fine to me. Can you say `cat -vet otherFile` to make sure you don't have any fishy character? What if you say `sed "s@oldString@XXX@"`? That is, is the old or the new string that makes this happen? – fedorqui Dec 04 '15 at 11:53
  • If I copy'n'paste the content of newString (without the newline) according to your suggestion of sed "s@oldString@XXX@" it just works fine, i.e. the string's "visible" content is fine. I tried the cat -vet option, otherFile doesnt contain weird characters, however, I noticed that the resulting String contains a ^M right after the substituted string. Interestingly, only end the end of the full string can I see a ^M$. (e.g. some text is^M replaced^M$ ) – kermit Dec 04 '15 at 15:55
  • Seems like removing the ^M from otherFile solved the issue. As mentioned earlier, the sed -s takes care of the newline, so my initial script works as intended. Definitely learned something new! Thanks – kermit Dec 04 '15 at 16:11
  • Aaah so your file was coming from a Windows machine. For this, you can use the tool `dos2unix` that will strip such characters. – fedorqui Dec 04 '15 at 16:15

1 Answers1

1

sed will always terminate its output with a newline (albeit process substitution normally strips the last newline, so I'm not sure why you see this problem).

You can modify your variable afterwards (assuming your shell is Bash):

newString="${newString%%$'\n'}"

Demonstration:

$ s=$'abc\n'
$ printf '>> %q <<\n' "$s" "${s%%$'\n'}"
>> $'abc\n' <<
>> abc <<
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Toby Speight
  • 27,591
  • 48
  • 66
  • 103