Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to add a string at the end of all lines in a text file, but I have a mistake somewhere.


I've this in a text file:

I run:

sed -i "s|\x0D$|--end|" file.txt

And I get:

The string is added only in certain lines and not in others.

Any idea why?

share|improve this question
up vote 9 down vote accepted

\x0D is carriage return, which may or may not be visible in your text editor. So if you've edited the file in both Windows and a Unix/Linux system there may be a mix of newlines. If you want to remove carriage returns reliably you should use dos2unix. If you really want to add text to the end of the line just use sed -i "s|$|--end|" file.txt.

share|improve this answer
Yes, it's a text file from Windows edited in Linux. dos2unix really solve problem and now, add --end at the end of all my lines. Thank You!!!! – user294721 Jan 30 '14 at 17:54
@user294721 if this answer solves your problem, please remember to mrk it as accepted. – terdon Jan 30 '14 at 18:06
you can remove the carriage returns in the same sed: sed 's/\r\?$/--end/' – glenn jackman Jan 30 '14 at 18:17

There are may ways of doing this:

  1. Perl

    perl -i -pe 's/$/--end/' file.txt
  2. sed

    sed -i 's/$/--end/' file.txt
  3. awk

    awk '{$NF=$NF"--end"; print}' file.txt > foo && mv foo file.txt
  4. shell

    while IFS= read -r l; do 
     echo "$l""--end"; 
    done < file.txt > foo && mv foo file.txt
share|improve this answer
Great answer and options! Thank you! – user294721 Feb 2 '14 at 19:42
@user294721 you're very welcome. Please remember to accept one of these answers so the question can be marked as answered. – terdon Feb 2 '14 at 19:44

You may try using ex:

ex +"%s/$/--end/g" -cwq foo.txt 

which is equivalent to vi -e.

Option -i isn't quite compatible between Linux and Unix and it may not be available on other operating systems.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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