Command to replace newline with comma


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command to replace newline with comma
# 1  
Old 10-17-2013
Question 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:
Code:
sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}' parameter.rtf | rev | cut -c 4- | rev

My input file has:
Code:
TABLE_1
TABLE_2
TABLE_3

What I need:
Code:
TABLE_1,TABLE_2,TABLE_3

What I am getting:
Code:
TABLE_1,TABLE_2,TABLE_3,

I want to avoid that last comma after TABLE_3 which is coming since there is a newline character at the end of the file. I am not sure whether the file will always have that newline char at the end of file so I need to be sure it doesnt blindly trims the last char but removes it only if it is a comma.

Can you please suggest a solution for this?

Regards,
Newbie
# 2  
Old 10-17-2013
try:
Code:
awk '$1=$1' RS= OFS=, infile

# 3  
Old 10-17-2013
Using sed,
Code:
sed ':a;N;$!ba;s/\n/,/g'

This User Gave Thanks to greet_sed For This Post:
# 4  
Old 10-17-2013
Code:
sed -n -e '1h;1!H;${x;s/\n/,/g;p;}' parameter.rtf

Solution from the previous post seems elegant; Unix sed needs
Code:
sed -e ':a' -e 'N;$!ba' -e 's/\n/,/g'


Last edited by MadeInGermany; 10-18-2013 at 09:37 AM..
# 5  
Old 10-17-2013
A simple solution using paste:
Code:
$ paste -sd, file
TABLE_1,TABLE_2,TABLE_3

# 6  
Old 10-18-2013
Thanks a lot guys. It helped!!!

Now an addition to it:
TABLE_1
TABLE_2
TABLE_3
are transposed as TABLE_1,TABLE_2,TABLE_3

Now I need it as 'TABLE_1','TABLE_2','TABLE_3'
Again to make sure the last character is not a comma.

Appreciate your support.

Cheers,
NG
# 7  
Old 10-18-2013
Try

Code:
$ cat file
TABLE_1
TABLE_2
TABLE_3

Code:
$ awk '{x=(NR==1)?"\x27"$0"\x27":x OFS "\x27"$0"\x27"}END{print x}' OFS=, file

Resulting
Code:
'TABLE_1','TABLE_2','TABLE_3'


Last edited by Akshay Hegde; 10-18-2013 at 07:44 AM..
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

Replace spaces with underscores up to first comma but not after the comma

I have a comma delimited file of major codes and descriptions. I want to replace all occurrences of spaces with underscores up to the first comma (only in the first field), but not replace spaces following the comma. For instance I have the following snippet of the file: EK ED,Elementary and... (7 Replies)
Discussion started by: tdouty
7 Replies

3. Shell Programming and Scripting

Replace comma and blank with comma and number

I, I have a file and i need to replace comma and blank space with comma and 0. cat file.txt a,5 b,1 c, d, e,4 I need the output as cat file.txt a,5 b,1 c,0 d,0 (4 Replies)
Discussion started by: jaituteja
4 Replies

4. Shell Programming and Scripting

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... (8 Replies)
Discussion started by: Ajaypal
8 Replies

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

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

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

How to Use Sed Command to replace white spaces with comma from between two fields - Mayank

SHELL SCRIPT Hi I have a file in the following format Mayank Sushant Dheeraj Kunal ARUN Samir How can i replace the white space in between and replace them with a comma?? The resultant output should be Mayank,Sushant Dheeraj,Kunal ARUN,Samir i tried using sed -e... (8 Replies)
Discussion started by: mayanksargoch
8 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. Shell Programming and Scripting

replace a newline (\n)

dear all: maybe i have a file like : 12 34 56 78 end how do write can i replace newline into NA : make the file inte : 12 NA 34 NA 56 78 END (3 Replies)
Discussion started by: jeter
3 Replies
Login or Register to Ask a Question