Print each ; separated value, in front of the value preceding them


 
Thread Tools Search this Thread
Operating Systems Linux Print each ; separated value, in front of the value preceding them
# 1  
Old 10-28-2015
Print each ; separated value, in front of the value preceding them

I have a file that looks like this,
Code:
P38112 Q12389; Q03532; P43586; Q08208; P37838; P40693; P38805
Q12389 P32892; P36049; Q03532; P43586; Q12176; P38112; P39744; P37838
P11154 P39940
P50094 P50095
P32892 Q12389; Q04660; P43586; P37838; P40693
Q05022 Q08235; Q12176; P39744
P40347 P32333
P63085 Q62108; Q9DBB1; P49841; Q03172; Q8CDB0; Q8R332-1; Q62132; Q9Z2B9
Q12749 Q07913
P32589 P02829; P08107; P10591; P11484
P53145 P38344; Q12522; Q02256
P38628 P40151
P53096 P14922; P61830
Q03497 P29366; P19073; P36006; Q04439; Q12163; P80667; P18851
Q39023 O80719; O64903
P32785 P00359

I want to print each ; separated value, in front of the value preceding them such that the preceding value also gets printed each time.
For example this is how I want the output to be,
Code:
P38112 Q12389 
P38112 Q03532 
P38112 P43586 
P38112 Q08208 
P38112 P37838 
P38112 P40693 
Q12389 P32892 
Q12389 P36049 
Q12389 Q03532 
Q12389 P43586

and so on...!

How can I do it using awk?
Any help would be highly appreciated.
# 2  
Old 10-28-2015
Hello Syeda,

Could you please try following and let me know if this helps.
Code:
awk '{for(i=2;i<=NF;i++){sub(/\;/,X,$i);print $1 OFS $i}}' Input_file

Output will be as follows.
Code:
P38112 Q12389
P38112 Q03532
P38112 P43586
P38112 Q08208
P38112 P37838
P38112 P40693
P38112 P38805
Q12389 P32892
Q12389 P36049
Q12389 Q03532
Q12389 P43586
Q12389 Q12176
Q12389 P38112
Q12389 P39744
Q12389 P37838
P11154 P39940
P50094 P50095
P32892 Q12389
P32892 Q04660
P32892 P43586
P32892 P37838
P32892 P40693
Q05022 Q08235
Q05022 Q12176
Q05022 P39744
P40347 P32333
P63085 Q62108
P63085 Q9DBB1
P63085 P49841
P63085 Q03172
P63085 Q8CDB0
P63085 Q8R332-1
P63085 Q62132
P63085 Q9Z2B9
Q12749 Q07913
P32589 P02829
P32589 P08107
P32589 P10591
P32589 P11484
P53145 P38344
P53145 Q12522
P53145 Q02256
P38628 P40151
P53096 P14922
P53096 P61830
Q03497 P29366
Q03497 P19073
Q03497 P36006
Q03497 Q04439
Q03497 Q12163
Q03497 P80667
Q03497 P18851
Q39023 O80719
Q39023 O64903
P32785 P00359

I have removed only ; in the characters if you have more specific requirement please do mention the same, as I see apart from ; character like -1 also there in Input_file. Hope this helps.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-28-2015
Yep, it works. Thanks a lot Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print next line beside preceding line on column match

Hi, I have some data like below: John 254 Chris 254 Matt 123 Abe 123 Raj 487 Moh 487 How can i print it using awk to have: 254 John,Chris 123 Matt,Abe 487 Raj,Moh Thanks. (4 Replies)
Discussion started by: james2009
4 Replies

2. Shell Programming and Scripting

awk print - fields separated with comma's need to ignore inbetween double quotes

I am trying to re-format a .csv file using awk. I have 6 fields in the .csv file. Some of the fields are enclosed in double quotes and contain comma's inside the quotes. awk is breaking this into multiple fields. Sample lines from the .csv file: Device Name,Personnel,Date,Solution... (1 Reply)
Discussion started by: jxrst
1 Replies

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

4. Shell Programming and Scripting

Grep and print next 10 Lines separated by ,

Hi All, I need to grep through a file for a string and print the next ten lines to a file separating the lines with a , and save it as a csv file to open it as a XL file. The 10 lines should be on a sigle row in xl. Any suggesstions please. Note; I dont have a GNU Grep to use -A flag. ... (6 Replies)
Discussion started by: Nani369
6 Replies

5. Shell Programming and Scripting

Compare Tab Separated Field with AWK to all and print lines of unique fields.

Hi. I have a tab separated file that has a couple nearly identical lines. When doing: sort file | uniq > file.new It passes through the nearly identical lines because, well, they still are unique. a) I want to look only at field x for uniqueness and if the content in field x is the... (1 Reply)
Discussion started by: rocket_dog
1 Replies

6. Shell Programming and Scripting

Perl script to parse output and print it comma separated

I need to arrange output of SQL query into a comma separated format and I'm struggling with processing the output... The output is something like this: <Attribute1 name><x amount of white spaces><Atribute value> <Attribute2 name><x amount of white spaces><Atribute value> <Attribute3... (2 Replies)
Discussion started by: Juha
2 Replies

7. Shell Programming and Scripting

Print separated datas

Hi People !! I need your help. I have a a txt file "example" with it datas. 11:35 10/12/2003 10.10.10.1 God.com 5 country Responsable of ASN 11:37 12/12/2003 10.10.10.1 FATHER.COM 5 country Responsable of ASN 11:40 14/12/2003 10.10.10.3 www.mother 6... (6 Replies)
Discussion started by: ras
6 Replies

8. Shell Programming and Scripting

How do I print out lines with the same number in front using awk?

Hi, I need help in printing out the dates with the largest value in front of it using awk. 436 28/Feb/2008 436 27/Feb/2008 436 20/Feb/2008 422 13/Feb/2008 420 23/Feb/2008 409 21/Feb/2008 402 26/Feb/2008 381 22/Feb/2008 374 24/Feb/2008 360... (7 Replies)
Discussion started by: SIFA
7 Replies

9. Shell Programming and Scripting

Finding word in file then print the preceding....

Hi, I am looking for a way to find a particular word in a file then print a line that precedes this line, as well as this line. Sometimes in a log file there is only one word per line and I need to print one of the lines leading up to the single worded line. Example - I can grep for ouch... (5 Replies)
Discussion started by: g_jumpin
5 Replies
Login or Register to Ask a Question