Comma separated to rows based on field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comma separated to rows based on field
# 1  
Old 04-22-2013
Comma separated to rows based on field

Hi to all,

I have a file like:

Code:
chr1 a1 a2 a3 a4 a5 a6,a7,a8,a9
chr1 b1 b2 b3 b4 b5 b6,b7
chr2 c1 c2 c3 c4 c5 c6,c7,c8,c9,c10
...

I would like an output like this:

Code:
chr1 a6
chr1 a7
chr1 a8
chr1 a9
chr1 b6
chr1 b7
chr2 c6
chr2 c7
chr2 c8
chr2 c9
chr2 10
...

Based on field1, split the comma separated values in different rows.
Thanks,
Anna
# 2  
Old 04-22-2013
What happened to chr1 a1?
Quote:
split the comma separated values
I don't see any commas.
# 3  
Old 04-22-2013
Quote:
Originally Posted by hanson44
What happened to chr1 a1?

I don't see any commas.
Hi Hanson44 i think he wants some thing like this from his input, commas separated part i have made red colored ones just to make more visibility Smilie
Code:
chr1 a1 a2 a3 a4 a5 a6,a7,a8,a9
chr1 b1 b2 b3 b4 b5 b6,b7
chr2 c1 c2 c3 c4 c5 c6,c7,c8,c9,c10

and he wants output like this
Code:
chr1 a6
chr1 a7
chr1 a8
chr1 a9
chr1 b6
chr1 b7
chr2 c6
chr2 c7
chr2 c8
chr2 c9
chr2 10
...

# 4  
Old 04-22-2013
An awk solution:
Code:
awk '{ n=split($NF,A,","); while (++i<=n) { print $1, A[i] } i=0 }' file

This User Gave Thanks to Yoda For This Post:
# 5  
Old 04-22-2013
Quote:
Originally Posted by Yoda
An awk solution:
Code:
awk '{ n=split($NF,A,","); while (++i<=n) { print $1, A[i] } i=0 }' file

Hi yoda,

can you please explain your code so that it would be great learning for me.

ThanksSmilie
# 6  
Old 04-22-2013
Another one:
Code:
awk '{gsub(/,/,RS $1 FS,$NF); print $1,$NF}' file

# 7  
Old 04-22-2013
Sure, by the way this code works only if the last field is separated by comma and there are no spaces in between them:
Code:
awk '
        {
                n = split ($NF, A, ",")         # Split last field using field separator comma "," Get number of elements created in variable: n
                while ( ++i <=n )               # while ++i <= n
                {
                        print $1, A[i]          # Print first field and element in array: A indexed by variable: i
                }
                i = 0                           # Reset variable: i value to 0
        }
' file

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to extract fields from a CSV i.e comma separated where some of the fields having comma as value?

can anyone help me!!!! How to I parse the CSV file file name : abc.csv (csv file) The above file containing data like abv,sfs,,hju,',',jkk wff,fst,,rgr,',',rgr ere,edf,erg,',',rgr,rgr I have a requirement like i have to extract different field and assign them into different... (4 Replies)
Discussion started by: J.Jena
4 Replies

2. Shell Programming and Scripting

awk to parse comma separated field and removing comma in between number and double quotes

Hi Experts, Please support I have below data in file in comma seperated, but 4th column is containing comma in between numbers, bcz of which when i tried to parse the file the column 6th value(5049641141) is being removed from the file and value(222.82) in column 5 becoming value of column6. ... (3 Replies)
Discussion started by: as7951
3 Replies

3. Shell Programming and Scripting

Need comma separated output

Hi, I am having the file with server names & its corresponding process, i need your help how to convert into comma separated output between server & app #cat apps.txt Server1 oracle was Server2 http webadmin Server3 tsm db2 My requirement is like below. Server1,oracle/was... (5 Replies)
Discussion started by: ksgnathan
5 Replies

4. UNIX for Dummies Questions & Answers

[solved] Comma separated values to space separated

Hi, I have a large number of files which are written as csv (comma-separated values). Does anyone know of simple sed/awk command do achieve this? Thanks! ---------- Post updated at 10:59 AM ---------- Previous update was at 10:54 AM ---------- Guess I asked this too soon. Found the... (0 Replies)
Discussion started by: lost.identity
0 Replies

5. Shell Programming and Scripting

Need Help - comma inside double quote in comma separated csv,

Hello there, I have a comma separated csv , and all the text field is wrapped by double quote. Issue is some text field contain comma as well inside double quote. so it is difficult to process. Input in the csv file is , 1,234,"abc,12,gh","GH234TY",34 I need output like below,... (8 Replies)
Discussion started by: Uttam Maji
8 Replies

6. Shell Programming and Scripting

Comma separated file

Hi all, I have the following files types: FileA: 100, 23, 33, FileB: 22, 45, 78, and i want to make File C: 100,22 23,45 33,78 any nice suggestions for making it easy. (3 Replies)
Discussion started by: hen1610
3 Replies

7. Shell Programming and Scripting

Inserting string in between field in comma separated file

Hello Mates, I have one txt file having commo seperated values. I have to insert string "FALSE" in 2nd field from the end. E.G SE18 6RN,,,,5439070,1786840,,1000002148671600,123434 Out put should be: SE18 6RN,,,,5439070,1786840,FALSE,1000002148671600,123434 Can some one help me to... (8 Replies)
Discussion started by: krsnadasa
8 Replies

8. UNIX for Dummies Questions & Answers

Sum the rows number based on first field string value

Hi, I have a file like this one h1 4.70650E-04 4.70650E-04 4.70650E-04 h2 1.92912E-04 1.92912E-04 1.92912E-04 h3A 3.10160E-11 2.94562E-11 2.78458E-11 h4 0.00000E+00 0.00000E+00 0.00000E+00 h1 1.18164E-12 2.74150E-12 4.35187E-12 h1 7.60813E-01 7.60813E-01 7.60813E-01... (5 Replies)
Discussion started by: f_o_555
5 Replies

9. Shell Programming and Scripting

How to format file into comma separated field

Guys, Need you help, i have a a file content that look like this. Nokia 3330 <spaces><spaces><more spaces>+76451883874 Nokia 3610 +87467361615 so on and so forth, - there are so many spaces in between. - e.g.... (5 Replies)
Discussion started by: shtobias
5 Replies

10. Shell Programming and Scripting

Its PERL + Comma separated seventh field

Hi Friends, I'm working on a perl script, which seems to be simpler. But I'm very new to PERL scripting. I have a comma separated data file, from which I need to extract only the seventh field data out of available twenty fields to an array using perl. Any help would be much appreciated. ... (17 Replies)
Discussion started by: ganapati
17 Replies
Login or Register to Ask a Question