Compare 2 files print the lines of file 2 that contain a string from file 1


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Compare 2 files print the lines of file 2 that contain a string from file 1
# 1  
Old 07-09-2011
Question Compare 2 files print the lines of file 2 that contain a string from file 1

Hello I am a new unix user, and I have a work related task to compare 2 files and print all of the lines in file 2 that contain a string from file 1 Note: the fields are in different columns in the files. I suspect the is a good use for awk? Thanks for your time & help

File 1

123 232 W343
321 323 W454
999 666 W777

File 2

333 W343 989
577 W323 979
888 W777 555

Result =

333 W343 989
888 W777 555
# 2  
Old 07-09-2011
This does a couple loops per line in file2, could be optimized maybe if more was known?

Code:
[mute@geek ~/test]$ awk 'NR == FNR { a[$0]++ } NR != FNR { for (e in a) for (i=1;i<NF;i++) if (e ~ $i) print $0 }' file1 file2
333 W343 989
888 W777 555

# 3  
Old 07-09-2011
Thank you so much!

Scott,
Thanks! I am anxious to try your solution, here is a litte more of the story, we have a large file of errors related to all parts, (File2 in my example) I am trying to filter that file & find only the errors related to parts in a specific product (File1) the common denominator between the 2 files is something called the work order number. this work order number is found in both files, the files are .csv files

so.......

My ideal script or program would search for the list of work orders in file1, then filter file2(The large file) for the lines containing the corresponding work orders, then create a 3rd file containing the result that could be printed so that we can fix the errors related to a specific product,this will enable us to prioritze the parts to be fixed.

I hope this helps
Thanks again!Smilie
# 4  
Old 07-09-2011
Then we can create an array containing just the work order #, if I knew how to extract it from file1.

Is it in every row, and is it same column each row? if you can make quick sample of each file replacing confidential parts with fake data would help.
# 5  
Old 07-09-2011
More to the story.........

Scott I am @ home so I dont have access to the file but I can answer your questions, the work order is in a consistant field in both files, although I believe its in different fields (for example in file1 I beleve its field 5 in file2 I believe its field 3) also both files will always contain a work order number. sorry I cant provide the file today

Thanks again Smilie

P.S. I am anxious to learn any reccomended books for the beginner?
# 6  
Old 07-09-2011
I do not know book. I have done some C and awk is easy after referencing manual at The AWK Manual - Table of Contents

Is ok you don't have sample but knowing they are both csv just with different column we can use like:

Code:
awk -F',' 'NR == FNR { a[$5]++ } NR != FNR && a[$3]' file1 file2

NR is record number, by default a record is a line. FNR is file record number, thus, for the first file the two are the same. When file2 is opened, NR keeps incrementing, but FNR resets. So they are equal for file1 and we put the 5th field into associative array. So a[workorder#] is counting them... Then the expression is a[$3] for file2, to see if that key exists from file1 and the default action is to print the line...

So hopefully you only need to change the 5 and 3 for this to work.
# 7  
Old 07-09-2011
Thanks again Scott

SmilieThanks again Scott,

I am actually looking forward to work on Monday so that I can take your solution out for a test drive. I really appreciate your insights and I will let you know how it goes

Take care
Kevin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to compare 2 files and create a result file with unmatched lines from first file.?

HI, I have 2 text files. file1 and file2. file1.txt (There are no duplicates in this file) 1234 3232 4343 3435 6564 6767 1213 file2.txt 1234,wq,wewe,qwqw 1234,as,dfdf,dfdf 4343,asas,sdds,dsds 6767,asas,fdfd,fdffd I need to search each number in file1.txt in file2.txt's 1st... (6 Replies)
Discussion started by: Little
6 Replies

2. Shell Programming and Scripting

[Solved] awk compare two different columns of two files and print all from both file

Hi, I want to compare two columns from file1 with another two column of file2 and print matched and unmatched column like this File1 1 rs1 abc 3 rs4 xyz 1 rs3 stu File2 1 kkk rs1 AA 10 1 aaa rs2 DD 20 1 ccc ... (2 Replies)
Discussion started by: justinjj
2 Replies

3. Shell Programming and Scripting

Compare columns of multiple files and print those unique string from File1 in an output file.

Hi, I have multiple files that each contain one column of strings: File1: 123abc 456def 789ghi File2: 123abc 456def 891jkl File3: 234mno 123abc 456def In total I have 25 of these type of file. (5 Replies)
Discussion started by: owwow14
5 Replies

4. Shell Programming and Scripting

How to print the lines in a file for the given string?

Hi, I have a file with contents test id text day test sah dh dhs yeay fg jsh jsjk my need: I give a string as a input, it check the file and display the lines with the given string e.g input : test output: test id text day test sah dh dhs (1 Reply)
Discussion started by: nanthagopal
1 Replies

5. Shell Programming and Scripting

awk compare specific columns from 2 files, print new file

Hello. I have two files. FILE1 was extracted from FILE2 and modified thanks to help from this post. Now I need to replace the extracted, modified lines into the original file (FILE2) to produce the FILE3. FILE1 1466 55.27433 14.72050 -2.52E+03 3.00E-01 1.05E+04 2.57E+04 1467 55.27433... (1 Reply)
Discussion started by: jm4smtddd
1 Replies

6. Shell Programming and Scripting

Print #of lines after search string in a big file

I have a command which prints #lines after and before the search string in the huge file nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=0 a=10 s="STRING1" FILE The file is 5 gig big. It works great and prints 10 lines after the lines which contains search string in... (8 Replies)
Discussion started by: prash184u
8 Replies

7. Shell Programming and Scripting

awk to compare flat files and print output to another file

Hello, I am strugling from quite a some time to compare flat files with over 1 million records could anyone please help me. I want to compare two pipe delimited flat files, file1 with file2 and output the unmatched rows from file2 in file3 Sample File1: ... (9 Replies)
Discussion started by: suhaeb
9 Replies

8. Shell Programming and Scripting

print string at the end of lines in text file

hello, I go text file like this E:/DDD/Dyndede/wwww E:/DDD/sss.com/ffffg/fff E:/DDD/vvvvvv/dd E:/DDD/sss.com/bbbbbb E:/DDD/sss.com/nnnn/xxI want to print /alpha.jpg at the end of every lines like that E:/DDD/Dyndede/wwww/alpha.jpg E:/DDD/sss.com/ffffg/fff/alpha.jpg... (8 Replies)
Discussion started by: davidkhan
8 Replies

9. Shell Programming and Scripting

compare two columns of different files and print the matching second file..

Hi, I have two tab separated files; file1: S.No ddi fi cu o/l t+ t- 1 0.5 0.6 o 0.1 0.2 2 0.2 0.3 l 0.3 0.4 3 0.5 0.8 l 0.1 0.6 ... (5 Replies)
Discussion started by: vasanth.vadalur
5 Replies

10. Shell Programming and Scripting

Need help to print lines contains particular string format in a file

Hi, I want to print the lines in a file that matches particular string format using shell scripting. (4 Replies)
Discussion started by: sudhakaryadav
4 Replies
Login or Register to Ask a Question