Second comma to new line.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Second comma to new line.
# 1  
Old 02-13-2007
Second comma to new line.

Hi,

Input is comma delimited. How to convert every second comma ',' to a new line character.

I know tr ',' '\n' <filename will convert every occarance.

Input :
1,5,6,4,9,..n

Output:
1,5
6,4
9,..

Thanks in advance.
# 2  
Old 02-14-2007
Code:
echo "1,5,6,4,9,10" | tr ',' '\n' | paste -d',' - -

Cheers
ZB
# 3  
Old 02-14-2007
Thanks it worked.
# 4  
Old 02-14-2007
Using Awk

Code:
echo "1,5,6,4,9,10" | 
awk -F, '{for( i =1;i<=NF;++i)
if( i % 2 == 0 ) printf("%s\n",$i)
else printf("%s,",$i) ; } '

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove comma from first and last line?

Hi Gurus, I need to remove comma from first and last line. I tried below code, but no luck. It only remove first line. Gurus, please help. awk -F"," '{if(NR==1||NR==$NR) print $1; else print $0}' TEST sampe file: ABC HEADER TOTAL RECORDS ARE 2.00,,,,... (13 Replies)
Discussion started by: ken6503
13 Replies

2. Shell Programming and Scripting

How can i comma-delimited last field in line?

Awk gurus, Greatly appreciate for any kind of assistance from the expert community Input line: abc,11.22.33.44,xyz,7-8-9-10 pqr,111.222.333.444,wxy,1-2-3 def,22.33.44.55,stu,7-8 used the gsub function below but it changes all of the "-" delimiter: awk 'gsub("-",",")' Desired... (4 Replies)
Discussion started by: ux4me
4 Replies

3. Shell Programming and Scripting

How can we remove comma from end of each line ?

Hi, How can we remove the comma from the end of each line. I have a csv file in below format. file.csv Name,age,gender,location, Joel,18,M,Newyork, Monoj,21,M,Japan, Litu,23,M,turki, Expected o/p file1.csv Name,age,gender,location (4 Replies)
Discussion started by: Litu19
4 Replies

4. Shell Programming and Scripting

Adding comma to end of each line if more than 1 line

I have a file with dates as '2013-01-01' '2013-01-02' I want the output to be '2013-01-01','2013-01-02' if there is only 1 entry then there should not be any comma. (6 Replies)
Discussion started by: ATWC
6 Replies

5. Shell Programming and Scripting

How to grep after the first comma till the next comma in a line

Hi Can any one pls tell me how to grep this line POPULATION,69691,20120509 I want the number 69691 from the above line. How to grep from the first comma till the next comma. Thank You.:confused: (8 Replies)
Discussion started by: rxg
8 Replies

6. UNIX for Dummies Questions & Answers

Adding comma at the end of every line

Hi all, I have this sample file (actual file is larger) and i need to add comma at the end of every line. 1234 4335 232345 1212 3535 Output 1234, 4335, 232345, 1212, 3535, TIA - jak (2 Replies)
Discussion started by: jakSun8
2 Replies

7. UNIX for Dummies Questions & Answers

Regex for beginning of line until a comma

What is a regex for "the dalai lama, his holiness the" that would just grab "the dalai lama" and one that would just grab "his holiness the"? Both should exclude the comma.. I was trying '^.*' and many variants with no luck. (6 Replies)
Discussion started by: glev2005
6 Replies

8. UNIX for Dummies Questions & Answers

How to remove comma from the last line of the file

Hi, I have a file which has records which end with a comma. for example: My file looks like 1234, 5678, 3455, 3566, 4444, 9999, I need to remove comma for the last line in the file so that my file should look like: 1234, 5678, 3455, (5 Replies)
Discussion started by: sandeep_1105
5 Replies

9. Shell Programming and Scripting

need to get the last word in comma sep line

I have a file with aaa,bbb,ccc,dddd,eee,xyz aaa,bbb,ccc,dddd,eee,xyz,12345,rty aaa,bbb,ccc,dddd,eee,xyz,12345,rty,tsrt 1. line columns are not fixed 2. all words are seperated by comma what i want is always the string after last comma. regards, Senthil... (9 Replies)
Discussion started by: senthilk615
9 Replies

10. Shell Programming and Scripting

Add a comma at end of every line

hello A small shell scripting help.. I have a file say with 5 lines of text (text file). At the end of everyline I need to add a comma at the end of the file. Thanks, ST2000 (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question