0

Using

sed 's/$/x/'

and entering the string

foo"bar

to standard input gives the expected result

foo"barx

However, putting the same string into a file and running

sed 's/$/x/' filename

gives the result

xoo"bar

Using the sed append command gives the same result. What should the bash/sed commands be to get the desired result when appending characters at end of lines in files containing quotation marks?

  • This is likely nothing to do with sed or quotation marks - it happens because you saved filename with Windows-style (CRLF) line endings – steeldriver Oct 18 '19 at 13:53
1

I do not get the same results you do. The first thought that comes to my mind is line endings though. Traditionally \n means line feed (go down) while \r means carriage return (go back to start), and both in a row means go down then back. Unix traditionally uses only \n, while Microsoft uses \r\n.

I'm speculating on how sed would handle this, but perhaps your file has \r\n for the line ending, but sed being a Linux tool is matching \n. So first the carriage returns to the start of the line, then sed matches the next character, \n, and puts an x (but now the cursor is at the start of the line, so it overwrites the first character).

Try creating the file using a Linux editor, if you weren't already.

  • Thanks, that was the problem. Actually I was using a Linux editor (gedit). It seems there is no option in gedit to force Unix style line endings, it keep the endings there are when starting to edit. Obviously the original file comes from the Windows environment. I now used Kate to convert. – Christer Oct 18 '19 at 14:12
  • @Christer you should be able to choose/change the file's line endings in gedit - there's a drop-down for it at the bottom of the Save dialog – steeldriver Oct 18 '19 at 17:49
  • Feel free to accept my answer if it helped you :) – izzy Oct 28 '19 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.