Hi ,
I have a huge file with following records and I want to replace the last comma with ',NULL'. I try using SED but could not create a correct script .
In my opinion I need a script which can convert ,/n with ,NULL/n
1,CHANGE_MEMBER,2010-12-28 00:05:00,
2,CHANGE_MEMBER,2012-09-02 00:00:20,3
3,CHANGE_MEMBER,2010-12-28 00:02:10,
4,CHANGE_MEMBER,2010-12-28 00:01:03,
Thanks in Advance
Ajay
bartus11:
Try:
sed 's/,$/,NULL/' file
Sorry that did not worked
Scott
July 2, 2013, 3:39pm
4
It should work. Perhaps you have whitespace after the last ,?
$ sed 's/,[[:blank:]]*$/,NULL/' file
(or use a literal space and tab inside a single [ and ] if your sed doesn't support character classes.)
scott:
It should work. Perhaps you have whitespace after the last ,?
$ sed 's/,[[:blank:]]*$/,NULL/' file
(or use a literal space and tab inside a single [ and ] if your sed doesn't support character classes.)
Sorry , your first script was perfect .
I copied records from my txt file and created a new file using putty and bartus11's script worked perfectly on that.
but I am not sure what is wrong in my original txt file where it does not work.
Scott
July 2, 2013, 4:41pm
6
I would guess your file contained ^M (carriage return) characters at the end of each line.
Yoda
July 2, 2013, 4:58pm
7
Run od
or cat
on your original file and check if there are any CR
od -c < file
OR
cat -evt file
Hello,
May be this will be helpful.
How to replace NewLine with comma using sed?
Hi ,
I have a huge file with following records and I want to replace the last comma with ',NULL'. I try using SED but could not create a correct script .
In my opinion I need a script which can convert ,/n with ,NULL/n
Code:
1,CHANGE_MEMBER,2010-12-28 00:05:00,2,CHANGE_MEMBER,2012-09-02 00:00:20,33,CHANGE_MEMBER,2010-12-28 00:02:10,4,CHANGE_MEMBER,2010-12-28 00:01:03,
Thanks in Advance
Ajay
$ cat test121
1,CHANGE_MEMBER,2010-12-28 00:05:00,
2,CHANGE_MEMBER,2012-09-02 00:00:20,3
3,CHANGE_MEMBER,2010-12-28 00:02:10,
4,CHANGE_MEMBER,2010-12-28 00:01:03,
$ awk '{sub(/, *$/,",NULL",$2)} {print$1 $2}' test121
Thanks,
R. Singh
Thanks a lot bartus11 ,Scott and Yoda .. It is working perfectly now.