开发者

Replacing with SED, trying to append content doesn't work

开发者 https://www.devze.com 2023-01-08 21:12 出处:网络
I\'m trying to use sed开发者_如何学Python 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.

I'm trying to use sed开发者_如何学Python 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)


do a dos2unix on the file first, then use sed

$ dos2unix file
$ sed 's/$/;NULL;NULL;1/' file
0

精彩评论

暂无评论...
验证码 换一张
取 消