I want to add this character at the end of each line in a file:

sed -e 's/$/\/$/g' myfile.txt > myfile_new.txt

The result add $/ with explicit $ in each line. but I do not want to add $.

For example, this is a line in the original file:


I executed the command, the result become:


But I want:


What is wrong? Plz note that the file was originally dos type, I convert it to unix using:

dos2unix myfile.txt

You just need 's/$/\//' - the $ anchors the pattern to the end of the line, but it's not an actual character that can be replaced. Similarly for ^. What you're actually matching (and replacing) is the empty pattern anchored at the end of the line.

Also as noted by @Phillippos in the comments, the g is unnecessary here: since the expression is anchored, it can only match at one place.

  • 5
    1) As there is only one end-of-line per line, the g option is not neccessary (true for all anchored patterns) 2) If the pattern or replacement contains a slash, better use a different delimiter: s_$_/_ is easier to read. – Philippos Jun 27 at 12:07
  • 1
    another "in other words..." -- the left-hand side of s/// is a regular expression; the right-hand side is a plain string (except for & and \1...). When you put a $ in the right-hand side, you're adding a plain character. – glenn jackman Jun 27 at 13:29

You can try with awk command too


awk '{print $0"/"}' filename

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.