Walk the line…endings

Have a file where you want to add a comma to the end of each line?

$ sed 's/$/,/' ip_addresses.txt | head -n2

This should work…but, wait, it didn’t. Instead, it replaced the first character of each line with a comma:


Ah, because the file is dos encoded, see the “\r \n”:

$ head -n1 ip_addresses.txt | od -c
0000000 9 6 . 1 2 7 . 0 . 0 / 1 7 \r \n

Ok, so dos2unix to the rescue:

$ dos2unix ip_addresses.txt
dos2unix: converting file ip_addresses.txt to Unix format...

And, finally:

$ sed 's/$/,/' ip_addresses.txt | head -n2,,

But, not so fast. We actually wanted commas on all but the last line:

$ sed 's/$/,/' ip_addresses.txt | tail -n2,,

So, use “$!” which matches lines which are NOT the last line:

$ sed '$!s/$/,/' ip_addresses.txt | tail -n2,