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
# 8  
Old 12-26-2013
Your specification said that field5 had subfields separated by a colon; your real data seems to contain subfields you want to match in fields 5 and 6 separated by a colon and a space. None of the code we provided will work if your input data doesn't match the specifications you supplied.
This User Gave Thanks to Don Cragun For This Post:
# 9  
Old 12-26-2013
Thanks, but it is not producing any output, what am I doing wrong? I changed $4 to $5, added a new field to dataset

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

# 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


# awk -F"\t" '
> NR == FNR {
>         n = split($5, f, /:/)
>         for(i = 1; i <= n; i++) o[f[i]] = " " $0
>         next
> }
> {       print $0 o[$2]
> }' 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

---------- Post updated at 04:05 PM ---------- Previous update was at 04:04 PM ----------

Quote:
Originally Posted by Don Cragun
Your specification said that field5 had subfields separated by a colon; your real data seems to contain subfields you want to match in fields 5 and 6 separated by a colon and a space. None of the code we provided will work if your input data doesn't match the specifications you supplied.
I`m sorry, if that is the problem I didn't realize it earlier. the space is inconsistent and has to be trimmed.
# 10  
Old 12-26-2013
Now I am confused, what do you expect from your new input ? can you show output sample
This User Gave Thanks to Akshay Hegde For This Post:
# 11  
Old 12-26-2013
I`m terribly sorry for the confusion, the output should be the following with only line1 col2 in file1 being found in file2, the rest of the lines appear in the same way as file1.

First line of output
Code:
comp100008_c0_seq1      169605479    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

all other lines
Code:
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

Where there is a tab between the junction of files,
Code:
169605479    Q0UR51


Last edited by newbie83; 12-26-2013 at 04:16 PM..
# 12  
Old 12-26-2013
It's field 6, my awk is not responding with FS as tab, so with default FS $6 is 169605479

Code:
$ awk  'FNR==NR{A[$6]=$0;next}{print $0,A[$2]}' file2 file1

comp100008_c0_seq1      169605479 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
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

# 13  
Old 12-26-2013
Please give us a clear and complete description of where we are supposed to find the matching values in file2! The value you seem to want to match in your latest example (169605479) does not appear in field 4 or field 5 in your input; it is in field 6. Tring to guess at a pattern to use to match your input from a sample of one line (with no English description that matches your input) is a waste of time.

If you have spaces in the middle of your fields and spaces separating fields, how are we supposed to determine which fields are to be processed?

Last edited by Don Cragun; 12-26-2013 at 04:39 PM.. Reason: fix typo
# 14  
Old 12-26-2013
Code:
111065707: 169605479

is supposed to be the 5th column in the input file2, I need to search 169605479 from file1 in the 5th column, which is present as a sub-field separated by ':' , I can see there is a leading space before 169605479, can that be trimmed?

I have attached the original input files which are tab delimited, maybe there is some formatting error when I copy paste.


I can see $5 in tab delimited format

Code:
awk -F"\t" '{print $5}' inp2.txt
111065707: 169605479

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