Print lines matching value(s) in other file using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines matching value(s) in other file using awk
# 1  
Old 04-26-2010
Print lines matching value(s) in other file using awk

Hi,

I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and would save generating multiple files to get the end result. I will really appreciate help in this regard. Thanks!

Code:
File1

123,1,123,...
12,1,456,...
1234,12,798,...

Code:
File2 

11,1,"ABC"
111,12,"RTF"
1,1234,"ESC"

Code:
Output

111,12,"RTF"
1,1234,"ESC"

# 2  
Old 04-26-2010
Code:
nawk -F, 'FNR==NR{f1[$1];next}$2 in f1' OFS=, file1 file2

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 04-27-2010
Thanks vgresh. It worked! I was trying similar code since couple of days with the help of information I found in different search results and I was able to store the value in array but cannot get it referenced to the second file. I understand my mistake now. Thanks alot!

---------- Post updated 04-27-10 at 12:07 AM ---------- Previous update was 04-26-10 at 03:58 PM ----------

Hi Vgersh,

If I would like to print the lines matched in file2 comma separated in file1 side by side then what change will help? I tried the following but its only print file2 match entries.

Code:
Desired Output
12,1,456,111,12,"RTF"
1234,12,789,1,1234,"ESC"

Code:
awk -F, 'FNR==NR{f1[$1]=$1;next}$2 in f1{print f1[$1],$0}' OFS="," file1 file2

I only gets file2 matched entries in this case as array print blank value for second file.

Thanks for the help in advance!
# 4  
Old 04-27-2010
Quote:
Originally Posted by SBC
Thanks vgresh. It worked! I was trying similar code since couple of days with the help of information I found in different search results and I was able to store the value in array but cannot get it referenced to the second file. I understand my mistake now. Thanks alot!

---------- Post updated 04-27-10 at 12:07 AM ---------- Previous update was 04-26-10 at 03:58 PM ----------

Hi Vgersh,

If I would like to print the lines matched in file2 comma separated in file1 side by side then what change will help? I tried the following but its only print file2 match entries.

Code:
Desired Output
12,1,456,111,12,"RTF"
1234,12,789,1,1234,"ESC"

Code:
awk -F, 'FNR==NR{f1[$1]=$1;next}$2 in f1{print f1[$1],$0}' OFS="," file1 file2

I only gets file2 matched entries in this case as array print blank value for second file.

Thanks for the help in advance!

Code:
awk -F, 'FNR==NR{f1[$1]=$0;next}$2 in f1{print f1[$2],$0}' OFS="," file1 file2

This User Gave Thanks to ahmad.diab For This Post:
# 5  
Old 04-28-2010
Thanks Ahmad and Vgersh for the response. Its really helpful.
# 6  
Old 05-03-2010
Hi,

I'm working on my script and trying to develop understanding of array association. Following for my own reference for the records which are not found in file2, print entries of file1 using Vgersh script:

Code:
nawk -F, 'FNR==NR{f1[$1];next} !($2 in f1)' OFS=, file1 file2



---------- Post updated at 10:08 PM ---------- Previous update was at 09:58 PM ----------

Hi,

I have got the results using above awk commands. Now I would like to perform comparison among the fields to look for specific set of characters. Based on the entry in field4 if it look for that entry in field 7 on the same line and see if it find the record then print "Record Match" otherwise "Difference in record". Please note that the entries in field7 contains longer string so the entry in field4 will be part of the complete string in field7. Furthermore, there is no specific start position of the string in field7.

Sample Input file:
Code:
12,1,456,RTF,111,12,PROG-RTF 12
1234,12,798, ESC,1,1234,ENTY ESC 345
456,1,886,ABC,434,567,YTRU-POYH 765

Sample Output File:
Code:
12,1,456,RTF,111,12,PROG-RTF 12 || Record Match
1234,12,798, ESC,1,1234,ENTY ESC 345 || Record Match
456,1,886,ABC,434,567,YTRU-POYH 765 || Difference in record

Thanks in advance for your help!

Last edited by Franklin52; 05-03-2010 at 05:02 AM.. Reason: Please use code tags!
# 7  
Old 05-03-2010
Try...

Code:
awk -F, '{for(i=0;++i<=length($7);)
if($4==substr($7,i,length($4))){print $0"|| Matched";next}
{print $0"|| Not Matched"}
}' infile

This User Gave Thanks to malcomex999 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

awk to average matching lines in file

The awk below executes and is close (producing the first 4 columns in desired). However, when I add the sum of $7, I get nothing returned. Basically, I am trying to combine all the matching $4 in f1 and output them with the average of $7 in each match. Thank you :). f1 ... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to print matching lines in files that meet critera

In the tab delimited files below I am trying to match $2 in file1 to $2 of file2. If a match is found the awk checks $3 of file2 and if it is greater than 40% and $4 of file2 is greater than 49, the line in file1 is printed. In the desired output line3 of file1 is not printed because $3 off file2... (9 Replies)
Discussion started by: cmccabe
9 Replies

3. Shell Programming and Scripting

awk to combine matching lines in file

I am trying to combine all matching lines in the tab-delimited using awk. The below runs but no output results. Thank you :). input chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 110925349 110925532 ALG13 chrX 47433390 47433999 SYN1... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. UNIX for Dummies Questions & Answers

awk - Print lines if only matching key is found

I am looking to move matching lines (01 - 07) from File1 and 77 tab the matching string from File2, to File3.txt. I am almost done but - Currently, script is not printing lines to File3.txt in order. Thanks a lot. Any help is appreciated. Script I am using: awk 'FNR == NR && ! /^]*$/ {... (9 Replies)
Discussion started by: High-T
9 Replies

5. Shell Programming and Scripting

Want to print out lines with a matching pattern from file

Hi all, I want to search for strings in file1 that can be found in file2 and print out the whole line when matching pattern is found. I have used the below command, but this is not working for me, because it is writing out only the matching patterns from file2, not the whole line. fgrep -o... (2 Replies)
Discussion started by: MonikaB
2 Replies

6. Shell Programming and Scripting

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 Replies

7. Shell Programming and Scripting

awk print non matching lines based on column

My item was not answered on previous thread as code given did not work I wanted to print records from file2 where comparing column 1 and 16 for both files find rows where column 16 in file 1 does not match column 16 in file 2 Here was CODE give to issue ~/unix.com$ cat f1... (0 Replies)
Discussion started by: sigh2010
0 Replies

8. Shell Programming and Scripting

Print matching lines in a file

Hello everyone, I have a little script below: die "Usage infile outfile reGex" if @ARGV != 3; ($regex) = @ARGV; open(F,$ARGV) or die "Can't open"; open(FOUT,"+>$ARGV") or die "Can't open"; while (<F>) { print FOUT if /$regex/.../$regex/; } No matter what I give $regex on the... (2 Replies)
Discussion started by: new bie
2 Replies

9. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

10. Shell Programming and Scripting

How to print file without few exactly matching lines?

Hi I have a very long file with 4 columns of numbers for example 1875 1876 12725 12723 13785 13786 4232 4230 13184 13185 ... (2 Replies)
Discussion started by: ananyob
2 Replies
Login or Register to Ask a Question