octopusgrabbus
octopusgrabbus

Reputation: 10695

Why does s/$/,/g replace first character?

OS Ubuntu 12.04

Shell bash

Why does this sed command

sed -e 's/$/,/g' test.in

replace the 5 in test.in?

Here are the contents of test.in

52147398,9480,12/31/2011 23:22,101049000,LNAM,FNAM,80512725,43,0,75,1/1/2012 6:45,101049000

Here are the results of running sed

,2147398,9480,12/31/2011 23:22,101049000,LNAM,FNAM,80512725,43,0,75,1/1/2012 6:45,101049000

I want to put a comma after 1010489000

After replacing the date-time format so that it's in yyyy-mm-dd hh:mm:ss format, now

s/$/,/g

works as expected. Is this because sed got hung up on the mm/dd/yyyy hh:mm format?

Upvotes: 0

Views: 933

Answers (2)

Scrutinizer
Scrutinizer

Reputation: 9926

Probably your file is in DOS format. The carriage return just before the newline would cause the comma to be written as the first character on the line overwriting an existing character (in this case the 5).

Convert to Unix format first:

tr -d '\r' < file > newfile

Upvotes: 1

ktm5124
ktm5124

Reputation: 12123

$ cat test.txt | sed 's/$/,/g'
52147398,9480,12/31/2011 23:22,101049000,LNAM,FNAM,80512725,43,0,75,1/1/2012 6:45,101049000,

Maybe the file is corrupt? Try copy/pasting into a new file...

Also try,

$ hexdump test.txt
hexdump test.txt
0000000 35 32 31 34 37 33 39 38 2c 39 34 38 30 2c 31 32
0000010 2f 33 31 2f 32 30 31 31 20 32 33 3a 32 32 2c 31
0000020 30 31 30 34 39 30 30 30 2c 4c 4e 41 4d 2c 46 4e
0000030 41 4d 2c 38 30 35 31 32 37 32 35 2c 34 33 2c 30
0000040 2c 37 35 2c 31 2f 31 2f 32 30 31 32 20 36 3a 34
0000050 35 2c 31 30 31 30 34 39 30 30 30 0a            
000005c

The first character should be 0x35.

Upvotes: 1

Related Questions