Print Matches to New Columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Print Matches to New Columns
# 1  
Old 07-03-2014
Print Matches to New Columns

Hi all,

I have a problem that I'm struggling to resolve. I have two files that look like this:

Code:
File 1
654654654 3
987987987 2
321321321 1

Code:
File 2
14NS0064 654654654
14NS0054 654654654
14NS0032 654654654
14NS0090 987987987
14NS0093 987987987
14NS0056 321321321

As you may notice, file 1 counts the occurrences of each value in column 2 in file 2. Generating file 2 from file 1 was no issue but what I would like to do now is append to new columns in file 1 the values in column 1 in file 2 that have the same value in column 2. So, my desired output is:

Code:
654654654 3 14NS0064 14NS0054 14NS0032
987987987 2 14NS0090 14NS0093
321321321 1 14NS0056

Thanks in advance for any help.
# 2  
Old 07-03-2014
Try
Code:
awk '{A[$2]=A[$2] " " $1; B[$2]++} END {for (i in A) print i, B[i], A[i]}  ' file2
987987987 2  14NS0090 14NS0093
321321321 1  14NS0056
654654654 3  14NS0064 14NS0054 14NS0032

This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-03-2014
Thank you very much, it works perfectly.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk add all columns if column 1 name matches

Hi - I want to add all columns if column1 name matches. TOPIC1 5 1 4 TOPIC2 3 2 1 TOPIC3 7 2 5 TOPIC1 6 3 3 TOPIC2 4 1 3 TOPIC3 9 5 4 . . . . . . . . . . . . Result should look like TOPIC1 11 4 7 TOPIC2 7 3 4 (1 Reply)
Discussion started by: oraclermanpt
1 Replies

2. UNIX for Beginners Questions & Answers

Matches columns from two different files in shell script

Hi friends, i want to compare first columns from two different files ,if equal print the file2's second column else print the zero.Please help me... file1: a b c d efile2: a 1 c 20 e 30 desired output: 1 0 20 0 30 Please use CODE tags as required by forum rules! Please post in... (1 Reply)
Discussion started by: bhaskar illa
1 Replies

3. UNIX for Dummies Questions & Answers

Print only '+' or '-' if string matches (two files)

I would like to add two additional conditions to the actual code I have: print '+' if in File2 field 5 is greater than 35 and also field 7 is grater than 90. while read -r line do grep -q "$line" File2.txt && echo "$line +" || echo "$line -" done < File1.txt ' Input file 1: ... (5 Replies)
Discussion started by: bernardo.bello
5 Replies

4. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

5. UNIX for Dummies Questions & Answers

Print only matches

Im not sure how to tell this awk command to only print when there is a match between the two files. Right now its printing the words in quotation even if there is not a match. I tried removing the additional info and it prints blank spaces?? awk ' { IP = $1 $1 = "" } FNR == NR { ... (2 Replies)
Discussion started by: sectech
2 Replies

6. Shell Programming and Scripting

Merge two columns from two files into one if another column matches

I have two text files that look something like this: A:B:C 123 D:E:F 234 G:H:I 345 J:K:L 123 M:N:O 456 P:Q:R 567 A:B:C 456 D:E:F 567 G:H:I 678 J:K:L 456 M:N:O 789 P:Q:R 890 I want to find the line where the first column matches and then combine the second columns into a single... (8 Replies)
Discussion started by: pbluescript
8 Replies

7. Shell Programming and Scripting

Extract columns where header matches a given string

Hi, I'm having trouble pulling out columns where the headers match a file of key ID's I'm interested in and was looking for some help. file1.txt I Name 34 56 84 350 790 1215 1919 7606 9420 file2.txt I Name 1 1 2 2 3 3 ... 34 34... 56 56... 84 84... 350 350... M 1 A A A A... (20 Replies)
Discussion started by: flotsam
20 Replies

8. Shell Programming and Scripting

Print xml if value matches

Hello Gurus.... Can any one please let me know how to print entire xml if the vlaue in the xml matches to a variable given. In PERL. example: <rpad:systemId>abc</hcdd:systemId> <rpad:hcdd>1172</hcdd:hcdid> </get:Request></soapenv:Body></soapenv:Envelope> if value macthes 1172 I... (3 Replies)
Discussion started by: thankful123
3 Replies

9. Shell Programming and Scripting

Joining columns from two files, if the key matches

I am trying to join/paste columns from two files for the rows with matching first field. Any help will be appreciated. Files can not be sorted and may not have all rows in both files. Thanks. File1 aaa 111 bbb 222 ccc 333 File2 aaa sss mmmm ccc kkkk llll ddd xxx yyy Want to... (1 Reply)
Discussion started by: sk_sd
1 Replies

10. Shell Programming and Scripting

print next line if matches a particular word..need help

Hi i need a help for making a script whch can print next line if it matches a particular word like file1 have ename Mohan eid 2008 ename Shyam eid 345 if scipt got Mohan it will print next line (eid 2008) pls help me .......:) (2 Replies)
Discussion started by: anish19
2 Replies
Login or Register to Ask a Question