Split multi columns line to 2 columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Split multi columns line to 2 columns
# 1  
Old 02-09-2016
Split multi columns line to 2 columns

I have data like this
Code:
1  a,b,c    2  a,c      3  b,d      4  e,f

would like convert like this
Code:
1   a    1   b    1   c    2   a    2   c    3   b    3   d    4   e    4   f

Please help me out

Last edited by Scrutinizer; 02-09-2016 at 04:01 PM.. Reason: code tags
# 2  
Old 02-09-2016
something along these lines......
awk -f rip.awk myFile where rip.awk is:
Code:
{
  for(i=1; i<=NF; i+=2) {
     split($(i+1), tA, ",")
     for(j=1; j in tA; j++)
       printf("%s%s%s%s", (j>1)?OFS:"",$i, OFS,tA[j])
     printf("%s", (i+2>NF)?ORS:OFS)
  }
}

# 3  
Old 02-09-2016
sorry , the format is wrong and here is the correct format
I have data like this
Code:
1 a b  c 
2 a c 
3 b d 
4 e f

would like convert like this
Code:
1 a 
1 b 
1 c 
2 a 
2 c
3 b 
3 d 
4 e 
4 f

Moderator's Comments:
Mod Comment Please use code tags for code and data

Last edited by Scrutinizer; 02-10-2016 at 12:33 AM.. Reason: Code tags
# 4  
Old 02-09-2016
Code:
perl -nale 'for(1..$#F){ print "$F[0] $F[$_]" }' jhonnyrip.file

# 5  
Old 02-10-2016
Quote:
Originally Posted by jhonnyrip
sorry , the format is wrong and here is the correct format
I have data like this
1 a b c
2 a c
3 b d
4 e f

would like convert like this
1 a
1 b
1 c
2 a
2 c
3 b
3 d
4 e
4 f
Hello jhonnyrip,

Please use code tags as per forum rules for commands/codes/Inputs which you are using in your posts. Following may help you in same too.
Code:
awk '{for(i=2;i<=NF;i++){print $1 OFS $i}}'  Input_file

Output will be as follows.
Code:
1 a
1 b
1 c
2 a
2 c
3 b
3 d
4 e
4 f

Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split columns into rows

Any one can help me in converting columns into rows. example I have input file 10000| 10002| 10003| 10004| 10005| I want output in below format PARTY|PART_DT 10000|12080000000 10002|13075200000 10003|13939200000 10004|1347200000 10004|133600000 10004|1152000000 (13 Replies)
Discussion started by: syd
13 Replies

2. Shell Programming and Scripting

Split 5 columns into 3 columns

I have a big data set as follows 8.519 8.601 8.833 9.183 9.602 1.003 1.041 1.070 1.085 1.084 1.06 1.034 9.879 9.307 8.66 I simply want this to arrange like this 8.519 8.601 8.833 9.183 9.602 1.003 1.041 1.070 1.085 1.084 1.06 1.034 9.879 9.307 8.66 ... (2 Replies)
Discussion started by: cnn
2 Replies

3. Shell Programming and Scripting

How to split all columns into multiple columns?

Hi, all. How can I split all columns into multiple columns separated by tab? Input: qq ee TT 12 m1 aa gg GG 34 2u zz dd hh 56 4h ww cc JJ 78 5y ss ff kk 90 j8 xx pp mm 13 p0 Output: q q e e T T 1 2 m 1 a a g g G G 3 4 2 u z z d d h h 5 6 4 h w w c c J J 7 8 5 y (8 Replies)
Discussion started by: huiyee1
8 Replies

4. Shell Programming and Scripting

How to format output in columns by appending multi lines one by one?

Hi, I need to display output in below format Customer : Apr 24 16:31 Customer_Name_111121.txt |---Space---|Apr 24 16:32 Customer_Name _111121. txt |---Space---|Apr 24 16:34 Customer_Name_111112. txt |---Space---|Apr 24 16:35 Customer_Name _222223. txt |---Space---|Apr 24 16:37... (8 Replies)
Discussion started by: ketanraut
8 Replies

5. Programming

Split the columns in Perl scripting

hi all, i have a file like thsi a 1 3;4 b 2 4;7 c 4 5;6 d 4 5;8 now i want 1st, 2nd and in 3rd column i want only 1st column the output should be like below a 1 3 b 2 4 c 4 5 d 4 5 I need it in perl; please dont write in Shell scripting (1 Reply)
Discussion started by: siva kumar
1 Replies

6. Shell Programming and Scripting

To split the columns

Hi, I have a file /tmp/1.txt. (Space b/w the fields are not equal) #cat 1.txt 35G 22G 13G 64% /mount1 15G 7.1G 7.5G 49% /mount2 2G 9.9G 4.7G 68% /mount3 15G 9.1G 5G 63% /mount4 i want to wite a script to display these values as below. O/P: 35G-22G-13G-64%-/mount1... (15 Replies)
Discussion started by: thomasraj87
15 Replies

7. Shell Programming and Scripting

Split columns

Greetings, I have an input file with two columns. The first column has different codes. Each time I have a new code I need to split the columns into new ones. The input file looks like this CR124 1320 CR124 1138 CR124 682 CR124 629 CR124 592 CR124 580 CR124 537 CR161 3967 CR161 3656... (6 Replies)
Discussion started by: vanesa1230
6 Replies

8. UNIX for Dummies Questions & Answers

Split columns

Greetings, I have an input file with two columns. The first column has different codes. Each time I have a new code I need to split the columns into new ones. The input file looks like this CR124 1320 CR124 1138 CR124 682 CR124 629 CR124 592 CR124 580 CR124 537 CR161 3967... (2 Replies)
Discussion started by: vanesa1230
2 Replies

9. UNIX for Dummies Questions & Answers

Sort on multi columns

Hope someone can help, I am trying to sort a generic file like this test2 A 468 0 test2 R 468 1 test2 E 468 1 test2 R 468 3 test2 R 468 4 test2 R 468 459 test2 Z 468 0 test1 A 191 0 test1 R 191 191 test1 Z 191 0 test3 A 3 0 test3 R 3 3 test3 Z 3 0 test0 A 1 0 test4 A 1 0 test4 E... (2 Replies)
Discussion started by: gio001
2 Replies

10. UNIX for Advanced & Expert Users

sorting data based on multi columns

Hi all I have data in following format: CSCH74,2007,1,09103,15 CSCH74,2007,10,09103,0 CSCH74,2007,11,09103,0 CSCH74,2007,12,09103,0 CSCH74,2007,2,09103,15 CSCH74,2007,3,09103,194 CSCH74,2007,4,09103,115 CSCH74,2007,5,09103,66 CSCH74,2007,6,09103,0 CSCH74,2007,7,09103,0... (2 Replies)
Discussion started by: sumeet
2 Replies
Login or Register to Ask a Question