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

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

Try:

sed 's/,$/,NULL/' file

Sorry that did not worked

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.

I would guess your file contained ^M (carriage return) characters at the end of each line.

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.

 
$ 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.