sed is adding character to start of word vs end

311 Views Asked by At

So I'm trying to add a "!" to the end of every word in my placesCapEx file from my placesCap file

This is what it looks like: Yugoslavian Zambia Zambian Zomba

This is what I want it to look like: Yugoslavian! Zambia! Zambian! Zomba!

I've tried sed 's/$/\!/' Wordlists/placesCap > Wordlists/placesCapEx and just sed 's/$/!/' Wordlists/placesCap > Wordlists/placesCapEx

What happens is when I run this and then cat Wordlists/placesCapEx it outputs !ugoslavian !ambia !ambian !omba

I've done some research and someone stated something about it being a Unix thing but they never went into detail

1

There are 1 best solutions below

1
On BEST ANSWER

Your simpler sed command should work fine for a text file where end-of-line is a single newline character. You likely have "dos" format files here (carriage return / linefeed).

Consider:

$ cat zippy
Zippy
$ od -c zippy
0000000   Z   i   p   p   y  \r  \n
0000007
$ sed 's/$/!/' zippy
!ippy
$ sed 's/$/!/' zippy | od -c
0000000   Z   i   p   p   y  \r   !  \n
0000010

You're seeing the effect of \r displayed on a terminal: move the cursor to start of line, print the '!', newline goes to next line.

To handle the presence of \r\n pairs as your end-of-line character, you might try:

$ sed 's/\r*$/!/' zippy
Zippy!

...assuming your sed honors the \r as mine (GNU sed 4.2.2) does.