How to replace NewLine with comma using sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace NewLine with comma using sed?
# 1  
Old 07-02-2013
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,3
3,CHANGE_MEMBER,2010-12-28 00:02:10,
4,CHANGE_MEMBER,2010-12-28 00:01:03,

Thanks in Advance
Ajay

Last edited by Scrutinizer; 07-02-2013 at 04:31 PM.. Reason: code tags
# 2  
Old 07-02-2013
Try:
Code:
sed 's/,$/,NULL/' file

# 3  
Old 07-02-2013
Quote:
Originally Posted by bartus11
Try:
Code:
sed 's/,$/,NULL/' file

Sorry that did not worked
# 4  
Old 07-02-2013
It should work. Perhaps you have whitespace after the last ,?

Code:
$ sed 's/,[[:blank:]]*$/,NULL/' file

(or use a literal space and tab inside a single [ and ] if your sed doesn't support character classes.)
# 5  
Old 07-02-2013
Quote:
Originally Posted by Scott
It should work. Perhaps you have whitespace after the last ,?

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

Last edited by Ajaypal; 07-02-2013 at 05:40 PM.. Reason: change
# 6  
Old 07-02-2013
I would guess your file contained ^M (carriage return) characters at the end of each line.
# 7  
Old 07-02-2013
Run od or cat on your original file and check if there are any CR
Code:
od -c < file

OR
Code:
cat -evt file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace newline with comma and append quotes

Hi, I have below requirement. Apple Orange Banana Required O/p in bash 'Apple,Orange,Banana' Can you please help. Please wrap your samples, codes in CODE TAGS as per forum rules. (3 Replies)
Discussion started by: Rtk
3 Replies

2. Shell Programming and Scripting

Command to replace newline with comma

Hi Experts, I need an urgent help in replacing newline characters in my single column file by a comma. I am using following command for this purpose: sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' parameter.rtf | rev | cut -c 4- | revMy input file has: TABLE_1 TABLE_2 TABLE_3 What I need:... (11 Replies)
Discussion started by: neerndg123
11 Replies

3. Shell Programming and Scripting

Replace comma delimiter by newline

The input file is as below AR,age,marks,roll,section,evin,25,80,456,A,atch,23,56,789,B,eena,24 ,78H245,C,Ps,ot,ecessary,hat,ame comes first then age and rest AR AZ,kevin,25,80,456,A,Satch,23,56,789,Satch,23,56,789,B,Meena,24,78,H245,C,AZ ................ ................ I am writting... (8 Replies)
Discussion started by: millan
8 Replies

4. Shell Programming and Scripting

Sed, replace comma with pipe. but ignore qoutes

hi, I am trying to replace comma with pipe, but the issue is that i want to ignore the commas inside qoutes. for example: i have file with the string: 1,"2,3",4,"5","6,7" the result should be : 1|"2,3"|4|"5"|"6,7" i trying to use sed and awk (match function) for that, but i did not... (4 Replies)
Discussion started by: gabik
4 Replies

5. Shell Programming and Scripting

Script using Sed :Search all patterns & after the last Patter, insert a newLine with Comma Sep Value

I am trying to search the pattern "ARS (11)" and after the LAST pattern, i am trying to open new line and enter text using sed. My Existing Text file is Users.txtpaul, Paul Smith, Stevn Smiley, REQ000001, ARS (11) sam, Sam Martin, Stevn Smiley, REQ000001, ARS (11) mike, Mike Conway, Stevn... (8 Replies)
Discussion started by: evrurs
8 Replies

6. Shell Programming and Scripting

Replace comma with a blank space using SED

Hello everyone, I want to replace all "," (commas) with a blank space My command thus far is: cat test.text | sed -e s/\`//g | awk '{print$1" "$2" "$3}' I'm sure you guys know this, but the SED command that I am using is to get rid of the "`" (tics). which gives me: name ... (5 Replies)
Discussion started by: jayT
5 Replies

7. Shell Programming and Scripting

Replace newline with comma.

I have output from a file like this: 15,01,11,14:06 235 I would like to change this to: 15,01,11,14:06,235 Removing newline and change to "," I now this can be done with tr cat OUT | tr '\n' ','' My problem is that tr is not implemented in this shell. sed is, show it should be... (7 Replies)
Discussion started by: Jotne
7 Replies

8. Shell Programming and Scripting

Using sed I want to replace space by newline

Input: -------------------------- 123asd 456sdasda 789a ------------------------- output wanted: --------------------- 123asd 456sdasda 789a ---------------------- I want this by sed in simple way please help (I know by: tr ' ' '\n' < inputfile )I want it by sed only (5 Replies)
Discussion started by: RahulJoshi
5 Replies

9. Shell Programming and Scripting

Replace comma with newline

Hi, for some reason I cant seem to figure this out. I have a file which looks something like this word word word word word,word,word word word word,word,word,word,word word word Basically I want this whole thing to be a list with 1 word on each line like this... word word word... (1 Reply)
Discussion started by: eltinator
1 Replies

10. UNIX for Dummies Questions & Answers

sed utility to replace /307 with comma

Hi, I have a requirement to replace '/307' with comma ',' . for e.g. : $ cat dm.dat ------------- decimdal("\307") acct $echo $l \307 $echo $k , $sed -e "s/$l/$k/" dm.dat > dm1.dat sed: Function s/\307/,/ cannot be parsed. I want dm1.dat to be : $ cat dm1.dat (1 Reply)
Discussion started by: obedkhan
1 Replies
Login or Register to Ask a Question