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
# 15  
Old 12-26-2013
I have made up an output manually if that helps
# 16  
Old 12-26-2013
Neither of the input files you just uploaded contained any tab characters; only spaces. But assuming that both inp1.txt and inp2.txt do use tab characters as the field separator and that the match field is field 5 (with a colon followed by zero or more spaces as the subfield separator), the following should do what you want:
Code:
awk -F '\t' '
NR == FNR {
        n = split($5, f, /: */)
        for(i = 1; i <= n; i++) {
                o[f[i]] = "\t" $0
        }
        next
}
{       print $0 o[$2]
}' inp2.txt inp1.txt

# 17  
Old 12-26-2013
Quote:
Originally Posted by newbie83
I have made up an output manually if that helps
There is only one tab character in your manually created sample output file. That is between the end of the 1st line of inp1.txt and the entire contents of inp2.txt.
Please show us the output of the commands:
Code:
od -c inp1.txt
od -c inp2.txt

on your system so we can see where the tabs are supposed to be located.
# 18  
Old 12-26-2013
Thank you for being so patient, here is the information you requested. the code still doesnt produce output, it is the same as file1


Code:
od -c inp2.txt
0000000   Q   0   U   R   5   1  \t   Q   0   U   R   5   1   _   P   H
0000020   A   N   O  \t   5   9   7   3   0   3   7  \t   X   P   _   0
0000040   0   1   7   9   6   1   6   0   .   1  \t   1   1   1   0   6
0000060   5   7   0   7   :       1   6   9   6   0   5   4   7   9  \t
0000100  \t   G   O   :   0   0   0   4   1   9   0   ;       G   O   :
0000120   0   0   0   6   5   0   8  \t  \t   U   n   i   R   e   f   1
0000140   0   0   _   Q   0   U   R   5   1  \t   U   n   i   R   e   f
0000160   9   0   _   Q   0   U   R   5   1  \t   U   n   i   R   e   f
0000200   5   0   _   N   4   X   C   Z   1  \t   U   P   I   0   0   0
0000220   0   D   D   1   1   E   3  \t  \t   3   2   1   6   1   4  \t
0000240  \t  \t   1   8   0   2   4   5   7   0  \t   C   H   4   4   5
0000260   3   3   2  \t   E   A   T   8   6   8   2   7   .   1  \t  \t
0000300  \t  \t  \n
0000303

Code:
#  od -c inp1.txt
0000000   c   o   m   p   1   0   0   0   0   8   _   c   0   _   s   e
0000020   q   1  \t   1   6   9   6   0   5   4   7   9  \r  \n   c   o
0000040   m   p   1   0   0   1   5   6   5   _   c   0   _   s   e   q
0000060   1  \t   3   2   6   4   9   3   8   5   0  \r  \n   c   o   m
0000100   p   1   0   0   1   7   _   c   0   _   s   e   q   1  \t   3
0000120   9   9   1   7   2   2   5   1  \r  \n   c   o   m   p   1   0
0000140   0   2   0   _   c   0   _   s   e   q   1  \t   3   2   2   7
0000160   0   3   4   4   7  \r  \n   c   o   m   p   1   0   0   4   1
0000200   6   _   c   0   _   s   e   q   1  \t   1   1   5   4   7   1
0000220   2   9   1  \r  \n   c   o   m   p   1   0   0   4   5   _   c
0000240   0   _   s   e   q   1  \t   4   7   3   7   8   9   5   0   9
0000260  \r  \n   c   o   m   p   1   0   0   7   9   8   7   _   c   0
0000300   _   s   e   q   1  \t   3   5   7   1   1   2   4   1   5  \r
0000320  \n   c   o   m   p   1   0   0   8   0   3   _   c   0   _   s
0000340   e   q   1  \t   4   7   5   6   1   0   1   0   0  \r  \n   c
0000360   o   m   p   1   0   0   9   0   _   c   0   _   s   e   q   1
0000400  \t   3   2   2   7   0   6   7   7   9  \r  \n   c   o   m   p
0000420   1   0   0   9   4   0   2   _   c   0   _   s   e   q   1  \t
0000440   3   2   6   5   1   0   3   3   7  \r  \n

# 19  
Old 12-26-2013
OK. It looks like inp1.txt was created on a Windows box. Your matching fields defined in inp2.txt are strings of numbers, but in inp1.txt they are strings of numbers followed by a <carriage-return> character. Let's try once more:
Code:
awk -F '\t' '
{       gsub(/\r/, "")
}
NR == FNR {
        n4f = split($5, f, /: */)
        for(i = 1; i <= n4f; i++) {
                out[f[i]] = "\t" $0
        }
        next
}
{       print $0 out[$2]
}' inp2.txt inp1.txt

This User Gave Thanks to Don Cragun For This Post:
# 20  
Old 12-27-2013
fantastic, works , thank you so much !!


Hi Don,

I tested with small samples and everything seems to work, I was wondering about the run-time of the original data.
File1 is 3.5Mb and File2 is 7.9Gb, is the code supposed to run for a long time? It has been 4 hrs, and it hasn't produced any output lines.

Last edited by newbie83; 12-27-2013 at 03:34 PM..
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