Using Ubuntu 18, with the bash shell
The "$" character is not behaving as it should for end of line match in sed.
When I try:
sed '/^>/ s/$/_new/g' <file>
this should result in
>sometext_new
but I am getting
_newetext
This may be related to this question: Error while matching End of line with $ in Sed commandbut that was using csh and had variable assignments which I do not have.
I have tried turning off shell expansion set -f ; but this did not help
I have tried sed '/^>/ s/"$"/_new/g' <file>
this turned $ into a literal character, resulting in no substitution
I would appreciate any help in restoring the previous behaviour of $ as end of line character in sed
1 Answer
Your <file> almost certainly has Windows-style (CRLF) line endings:
$ printf '>sometext\n' | sed '/^>/ s/$/_new/'
>sometext_newbut
$ printf '>sometext\r\n' | sed '/^>/ s/$/_new/'
_newetextIf you want to preserve the CRLF line endings, but replace text at the (Windows) EOL, you could use s/\r$/_new\r/
ex. (piping to cat -A to make the line endings explicit)
$ printf '>sometext\r\n' | sed '/^>/ s/\r$/_new\r/' | cat -A
>sometext_new^M$OTOH if you want to convert the whole file to Unix endings, you could use
sed -e 's/\r$//' -e '/^>/ s/$/_new/'or convert the file with dos2unix before using sed.
Note that the g modifier has no effect when a replacement is anchored to the end (or start) of a pattern.