Grepping one file column from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping one file column from another file
# 1  
Old 12-26-2013
Grepping one file column from another file

Hi all,

I want to search the second col of a file as a sub-part of 4th col of another file and produce a joint output. In the example, search if B is contained as a sub-part in E:B:C (sub-parts separated by colons). Note the second row is not found doesnt find a match as F isnt there in col 4 second file (second line).

Code:
file1

A B
E F
G H

file2
C D L E:B:C  
Z Y F FFFF:EEE:PP
G H R H:H:I:J

out

A B C D L E:B:C  
E F
G H G H R H:H:I:J

Here is what I tried but doesn't work

Code:
awk ' NR == FNR { y[":"$2":"] next  }{ x = ":"$4":" for (i in y)  if (index(x,i)) print i, $0 } file1 file2

# 2  
Old 12-26-2013
Try this:
Code:
awk '
        NR == FNR {
                A[$0] = $NF
                next
        }
        {
                for ( k in A )
                {
                        n = split ( A[k], R, ":" )
                        for ( i = 1; i <= n; i++ )
                        {
                                if ( R[i] == $2 )
                                {
                                        print $0, k
                                        f = 1
                                        break
                                }
                        }
                }
                if ( !(f) )
                        print $0
                f = 0
        }
' file2 file1

This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-26-2013
Another approach :
Code:
$ awk  'FNR==NR{A[$4]=$0;next}{x=0;for(i in A){split(A[i],X,":");if($2==X[2]){x=1;print $0,A[i]}}if(x==0)print}'  file2 file1
A B C D L E:B:C  
E F
G H G H R H:H:I:J


Last edited by Akshay Hegde; 12-26-2013 at 01:55 PM..
This User Gave Thanks to Akshay Hegde For This Post:
# 4  
Old 12-26-2013
is it possible to limit the search by the first hit? That will reduce the runtime considerably, so as soon as the first hit is met, I would like to move on to the next line in file1, similar to a break in C.
# 5  
Old 12-26-2013
Quote:
Originally Posted by newbie83
is it possible to limit the search by the first hit? That will reduce the runtime considerably, so as soon as the first hit is met, I would like to move on to the next line in file1, similar to a break in C.
Yes, as soon as field2 of file1 == 2nd sub field of field4 of file2 is true print line of file1 and line of file2 and then next, if you use break here it just terminates loop does not move to next line
Code:
$ awk  'FNR==NR{A[$4]=$0;next}{x=0;for(i in A){split(A[i],X,":");if($2==X[2]){x=1;print $0,A[i];next}}if(x==0)print}' file2 file1

Example :
Code:
$ awk  'FNR==NR{A[$4]=$0;next}{x=0;for(i in A){split(A[i],X,":");if($2==X[2]){x=1;print $0,A[i]}}print "without next and break";if(x==0)print}' file2 file1
A B C D L E:B:C  
without next and break
without next and break
E F
G H G H R H:H:I:J
without next and break

$ awk  'FNR==NR{A[$4]=$0;next}{x=0;for(i in A){split(A[i],X,":");if($2==X[2]){x=1;print $0,A[i];next}}print "with next";if(x==0)print}' file2 file1
A B C D L E:B:C  
with next
E F
G H G H R H:H:I:J

$ awk  'FNR==NR{A[$4]=$0;next}{x=0;for(i in A){split(A[i],X,":");if($2==X[2]){x=1;print $0,A[i];break}}print "with break";if(x==0)print}' file2 file1
A B C D L E:B:C  
with break
with break
E F
G H G H R H:H:I:J
with break


Last edited by Akshay Hegde; 12-26-2013 at 03:51 PM..
# 6  
Old 12-26-2013
Can you let me know what I`m doing wrong, this is part of a real data-set where I`m trying to search col2 from file1 in col5 in file2.

File2
Code:
# cat inp2.txt
Q0UR51  Q0UR51_PHANO    5973037 XP_001796160.1  111065707: 169605479            GO:0004190; GO:0006508          UniRef100_Q0UR51        UniRef90_Q0UR51 UniRef50_N4XCZ1 UPI0000DD11E3           321614                  18024570        CH445332     EAT86827.1

Code:
# cat inp1.txt
comp100008_c0_seq1      169605479
comp1001565_c0_seq1     326493850
comp10017_c0_seq1       399172251
comp10020_c0_seq1       322703447
comp100416_c0_seq1      115471291
comp10045_c0_seq1       473789509
comp1007987_c0_seq1     357112415
comp100803_c0_seq1      475610100
comp10090_c0_seq1       322706779
comp1009402_c0_seq1     326510337

Code:
# awk  -F"\t" 'FNR==NR{A[$5]=$0;next}{x=0;for(i in A){split(A[i],X,":");if($2==X[2]){x=1;print $0,A[i];next}}if(x==0)print}' inp2.txt inp1.txt
comp100008_c0_seq1      169605479
comp1001565_c0_seq1     326493850
comp10017_c0_seq1       399172251
comp10020_c0_seq1       322703447
comp100416_c0_seq1      115471291
comp10045_c0_seq1       473789509
comp1007987_c0_seq1     357112415
comp100803_c0_seq1      475610100
comp10090_c0_seq1       322706779
comp1009402_c0_seq1     326510337

# 7  
Old 12-26-2013
Yoda's code will find a match if any subpart of field 4 in file2 is matched by field 2 in file1. Akshay's code will find a match only if the 2nd subpart of field 4 in file 2 is matched by field 2 in file1.

If I read your requirements correctly, I think Yoda's interpretation is what was wanted. The following is an alternative approach that should produce the same output. Depending on the sizes of the input files, this should run faster but use a little more memory:
Code:
awk '
NR == FNR {
        n = split($4, f, /:/)
        for(i = 1; i <= n; i++) o[f[i]] = " " $0
        next
}
{       print $0 o[$2]
}' file2 file1

Note that if there are multiple lines in file2 with with identical subfields in field4, this will print the last match rather than the 1st. If you need the 1st instead of the last match, change:
Code:
        for(i = 1; i <= n; i++) o[f[i]] = " " $0

to:
Code:
        for(i = 1; i <= n; i++) if(!(f[i] in o)) o[f[i]] = " " $0

If you want to try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh Script, Reading A File, Grepping A File Contents In Another File

So I'm stumped. First... APOLOGIES... my work is offline in an office that has zero internet connectivity, as required by our client. If need be, I could print out my script attempts and retype them here. But on the off chance... here goes. I have a text file (file_source) of terms, each line... (3 Replies)
Discussion started by: Brusimm
3 Replies

2. UNIX for Dummies Questions & Answers

Grepping al values of a particular column in a file

Name Num_free Num_active Pct_act Max_Used Reuse_cnt Instance_Name --------------------------------- --------------- ----------- ------- ----------- ----------- ------------------------------ additional network memory 0 ... (2 Replies)
Discussion started by: Rajeshneemkar
2 Replies

3. Shell Programming and Scripting

Grepping text from one file in another file

Hello, I have a file with a large number of words each listed in sequential order one word per line. I want to search these words in another file which has the structure Both the files are large, but the words in the sourcefile are all available in the target file. I tried to grep... (2 Replies)
Discussion started by: gimley
2 Replies

4. Programming

Grepping a column from multiple file

I have 20 files that look pretty much like this: 0.01 1 3822 4.97379915032e-14 4.96982253992e-09 0 0.01 3822 1 4.97379915032e-14 4.96982253992e-09 0 0.01 2 502 0.00993165137406 993.165137406 0 0.01 502 2 0.00993165137406 993.165137406 0 0.01 4 33 0.00189645523539 189.645523539 0 0.01 33 4... (5 Replies)
Discussion started by: kayak
5 Replies

5. Shell Programming and Scripting

Display file date after grepping a string in the file

Hi All, I need to recursively grep several folders for a MAC address and display the results with the date of the file name at the start. Even better would be if the final results were displayed chronologically so the newest file is always at the end. Oldest at the top, regardless of what... (8 Replies)
Discussion started by: quemalr
8 Replies

6. Shell Programming and Scripting

Grepping a file contents into another file

I have a file named as ucid.txt It has multiple rows of "id". I need to search and grep each line of it from a file named as pw_logs.txt and put the results into another file. Please help ! Thanks. (8 Replies)
Discussion started by: gopikrish81
8 Replies

7. Shell Programming and Scripting

Grepping file and returning passed variable if the value does not exist in file at all.

I have a list of fields that I want to check a file for, returning that field if it not found at all in the file. Is there a way to do a grep -lc and return the passed variable too rather then just the count? I am doing some crappy work-around now but I was not sure how to regrep this for :0 so... (3 Replies)
Discussion started by: personalt
3 Replies

8. Shell Programming and Scripting

Grepping from a point in a file to the end of the file

does any one know how to turn the equivalent of this command: awk '/2011 John Doe 8344/,0' /tmp/ops.log | egrep -c "received request" to something that would use egrep instead of awk? What the awk command does is, it searches the ops.log file for "2011 John Doe 8344". When it finds it,... (4 Replies)
Discussion started by: SkySmart
4 Replies

9. Shell Programming and Scripting

Grepping a file based on input from a second file

how to grep a file based on another input file File1 ashu 1 ninetwo hari qwer 6 givefour jan fghj 8 noeight mar vbmi 7 noput feb -- --- File2 noput noeight --- -- Taking the input of grep as File2, a search need to be made in File1 giving File3 as output: (7 Replies)
Discussion started by: er_ashu
7 Replies

10. Shell Programming and Scripting

grepping all lines of one file from another file

Hi First post on here hope you can help with something I have a file with a couple of thousand lines (all lines are one string long, i.e a number I have another file that is over 1 million lines long Example entry from file 1 123456 Example from file 2 123456 mjhyuihbn ... (5 Replies)
Discussion started by: nampahc
5 Replies
Login or Register to Ask a Question