Print lines that contain a value in a specific column shared by more than 1 entity in another col


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines that contain a value in a specific column shared by more than 1 entity in another col
# 1  
Old 10-31-2013
Print lines that contain a value in a specific column shared by more than 1 entity in another col

I want to expand on a question that I just asked here:

I want to extract only those values in Column 2 that are shared by at least 2 unique values in Column 2.

Using the same input (in this case 3- tab-separated columns):
Code:
waterline-n    below-sheath-v    14.8097 
dock-n    below-sheath-v     14.5095 
waterline-n    below-steel-n    11.0330 
picnic-n    below-steel-n    12.2277 
wavefront-n    at-part-of-variance-n    18.4888 
wavefront-n    between-part-of-variance-n    17.0656
audience-b    between-part-of-variance-n    17.6346 
game-n    between-part-of-variance-n    14.9652 
whereabouts-n    become-rediscovery-n    11.3556 
whereabouts-n    get-tee-n    10.9091

For the following desired output:
Code:
waterline-n    below-sheath-v    14.8097 
dock-n    below-sheath-v     14.5095 
waterline-n    below-steel-n    11.0330
picnic-n    below-steel-n    12.2277 
wavefront-n    between-part-of-variance-n    17.0656 
audience-b    between-part-of-variance-n    17.6346 
game-n    between-part-of-variance-n    14.9652

How can I do this using awk / grep?

Last edited by owwow14; 10-31-2013 at 08:39 AM.. Reason: Please use code tags
# 2  
Old 10-31-2013
Code:
awk '{arr[$2]++; line[$2]=line[$2] sprintf("%s\n", $0); next}
       END {for(i in arr){  
         if(arr[i]>1){ printf("%s", line[i]) }
       }} ' infile > newfile

See if that is what you want.
This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 10-31-2013
Another approach:
Code:
awk 'NR==FNR{a[$2]++;next} a[$2]>=2 ' file file

These 2 Users Gave Thanks to Franklin52 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 print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

3. Shell Programming and Scripting

Print lines in which value in specified Col is NOT unique

Hi everyone, I have the following file, which is a 3 column tab-delineated. cat big 24 cat small 13 cat red 63 dog big 34 chicken plays 39 fish red 294 I would like to print only those lines, in which the value in Col2 is repeated. Thus, given the above input file, the desired... (7 Replies)
Discussion started by: owwow14
7 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Delete column with exact string in specific col

Hi, my file structur looks like File structure looks: GeneID protein_gi Symbol 1246500 10954455 repA1 1246501 10954457 repA2 1246502 10954458 leuA But some of the cases do not have record for protein id. for example: 1343044 - orf01 I want to remove those rows. But I tried awk... (9 Replies)
Discussion started by: smitra
9 Replies

6. Shell Programming and Scripting

Print Specific lines when found specific character

Hello all, I have thousand file input like this: file1: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$ | | | |$$ $$ UERT | TTYH | TAFE | FRFG |$$ $$______|______|________|______|$$ $$ | | | |$$ $$ 1 | DISK | TR1311 | 1 |$$ $$ 1 |... (4 Replies)
Discussion started by: attila
4 Replies

7. UNIX for Advanced & Expert Users

Print line based on highest value of col (B) and repetion of values in col (A)

Hello everyone, I am writing a script to process data from the ATP world tour. I have a file which contains: t=540 y=2011 r=1 p=N409 t=540 y=2011 r=2 p=N409 t=540 y=2011 r=3 p=N409 t=540 y=2011 r=4 p=N409 t=520 y=2011 r=1 p=N409 t=520 y=2011 r=2 p=N409 t=520 y=2011 r=3 p=N409 The... (4 Replies)
Discussion started by: imahmoud
4 Replies

8. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

9. UNIX for Dummies Questions & Answers

print a specific column

Hi I want to print column2 if column1 is b with: awk '$1==b {print $2}' infile > outfile infile a 1 b 2 c 3 b 4 e 5 b 6 the outfile is empty. it should look like this: 2 4 6 (2 Replies)
Discussion started by: jdhahbi
2 Replies

10. Shell Programming and Scripting

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies
Login or Register to Ask a Question