tags:

views:

33

answers:

1

I'm trying to use sed to fix a file, and I'm having trouble grokking what I'm doing wrong. Any pointers on why sed isn't behaving as I expect it to would be appreciated.

My file consists of a bunch of lines, each containing a single value. The content of the values is irrelevant for the replacing, save that they're always alpha-numeric and follow a pattern. I want to prepend and append these values with other data, in order to create a CSV file.

When prepending, sed works admirably:

Prepend (works)

sed 's/\(.*\)/NULL;0;400;6;2010-07-27;\1/' selection.csv

Resulting in:

NULL;0;400;6;2010-07-27;1326GP11SCH
NULL;0;400;6;2010-07-27;2312BB30DUM

Which is exactly as I hoped it would.

Append (fails)

However, when appending, things get ugly:

sed 's/\(.*\)/\1 ;NULL;NULL;1/' selection.csv

Resulting in:

 ;NULL;NULL;1
2312BB30DUM ;NULL;NULL;1

The to-be-replaced value disappears on all but the last line, which is quite disheartening.

It must be something quite obvious I'm doing wrong here, but can't figure it out.

(note: Not sure whether this is SO or SU material... feel free to move it to SuperUser, if you can and believe it should be over there)

+2  A: 

do a dos2unix on the file first, then use sed

$ dos2unix file
$ sed 's/$/;NULL;NULL;1/' file
ghostdog74
You sir, are awesome. Many thanks! Answer accepted and upvoted.
kander