Compare two text files and print matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Compare two text files and print matches
# 1  
Old 01-14-2012
Compare two text files and print matches

Hi,

I am looking for a way to compare two text files and print the matches. For example;

File1.txt
Code:
89473036
78474384 
48948408
95754748
47849030


File2.txt
Code:
47849030 
46730356
16734947
78474384 
36340047

Output:
Code:
78474384
47849030


Last edited by lewk; 01-14-2012 at 10:57 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 01-14-2012
Code:
awk '{++a[$2]} END {for(i in a) if(a[i]>1) print i}' file1 file2


Last edited by Franklin52; 01-14-2012 at 09:23 AM.. Reason: Please use code tags for data and code samples, thank you
# 3  
Old 01-14-2012
Code:
awk 'NR==FNR{a[$2];next}$2 in a{print $2}' file1 file2

--ahamed
# 4  
Old 01-14-2012
Hi guys,

Thanks for the help so far, however both statements print blank lines and I think it is because I need to tweak my example.

To elaborate further, it only contains numbers but the numbers are not a fixed length as in my first example.

File1.txt
Code:
8947303689037809238094
7847438449838
4894840834098337
95754748280926266290
478490309034879362612


File2.txt
Code:
47849030292119167904387
46730356340247247474934994287
478490309034879362612 
4894840834098337 
363400472727249727

Output:
Code:
4894840834098337
478490309034879362612

Moderator's Comments:
Mod Comment How to use code tags when posting data and code samples.

Last edited by lewk; 01-14-2012 at 10:56 AM.. Reason: Please use code tags for data and code samples, thank you
# 5  
Old 01-14-2012
Do you have the serial # at the beginning of the file? i.e. 1. 2. etc
And which is your OS? If solaris, use nawk

If you fon't have serial no, then use this...
Code:
grep -f file2 file1

--ahamed
# 6  
Old 01-14-2012
There is no structure to the data really, each line is unique. I need a formula to cat file 1, cat file 2 and print the matching lines.

I am on Linux, grep -f printing blank.
# 7  
Old 01-14-2012
Have you tried ahamed's suggestions? They should work if your input file is anything like you described. What do you mean with no structure? What you have posted so far is two files with 2 fields per line. The first field is a serial number followed by a dot, the second is a number of arbitrary length. Is this correct or does the structure deviate from this?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare two files and print output

Hi All, i am trying to compare two files in Centos 6. F1: /tmp/d21 NAME="xvda" TYPE="disk" SIZE="40G" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="" NAME="xvda1" TYPE="part" SIZE="500M" OWNER="root" GROUP="disk" MODE="brw-rw----" MOUNTPOINT="/boot" NAME="xvda2" TYPE="part"... (2 Replies)
Discussion started by: balu1234
2 Replies

2. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

3. Shell Programming and Scripting

Print line if values in fields matches number and text

datafile: 2017-03-24 10:26:22.098566|5|'No Route for Sndr:RETEK RMS 00040 /ZZ Appl:PF Func:PD Txn:832 Group Cntr:None ISA CntlNr:None Ver:003050 '|'2'|'PFI'|'-'|'EAI_ED_DeleteAll'|'EAI_ED'|NULL|NULL|NULL|139050594|ActivityLog| 2017-03-27 02:50:02.028706|5|'No Route for... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

BASH - Compare 2 Files, Output All Matches

This is probably rehash but I did look. :rolleyes: I want a bash script that will take Item 1 in File1, traverse all lines in File2 and output if a match exists. Continuing the pattern recursively, Item2, File1, traverse all lines in File2 for a match, continue this pattern until all lines... (6 Replies)
Discussion started by: rcbarr2014
6 Replies

5. UNIX for Dummies Questions & Answers

Print only '+' or '-' if string matches (two files)

I would like to add two additional conditions to the actual code I have: print '+' if in File2 field 5 is greater than 35 and also field 7 is grater than 90. while read -r line do grep -q "$line" File2.txt && echo "$line +" || echo "$line -" done < File1.txt ' Input file 1: ... (5 Replies)
Discussion started by: bernardo.bello
5 Replies

6. Shell Programming and Scripting

Compare 2 files and print matches and non-matches in separate files

Hi all, I have two files, chap.txt and complex.txt. chap.txt looks like this: a d l m r k complex.txt looks like this: a c d e l m n j a d l p q r c p r m ......... (7 Replies)
Discussion started by: AshwaniSharma09
7 Replies

7. Shell Programming and Scripting

Compare two files and print list

Hi Gents, I have 2 files as seen below. File 1: 9 1020 10 1001 11 1001 12 1002 13 1003 14 1004 15 1004 File 2: 9 1000 11 1001 12 1002 13 1003 15 1004 (5 Replies)
Discussion started by: jiam912
5 Replies

8. Shell Programming and Scripting

Compare two files and print using awk

I have 2 files: email_1.out 1 abc@yahoo.com 2 abc_1@yahoo.com 3 abc_2@yahoo.com data_1.out <tr> 1 MAIL # 1 TO src_1 </tr> <tr><td class="hcol">col_id</td> <td class="hcol">test_dt</td> <td class="hcol">user_type</td> <td class="hcol">ct</td></tr> <tr><td... (1 Reply)
Discussion started by: sol_nov
1 Replies

9. Shell Programming and Scripting

compare two files and print the last row into first

suppose fileA vis vis gyh gye gyh fileB vis 23 gyh 21 gye 32 output shud be like in fileA ... vis 23 vis 23 gyh 21 gye 32 gyh 21 (1 Reply)
Discussion started by: cdfd123
1 Replies

10. Shell Programming and Scripting

to compare two files and to print the difference

suppose one file P1168S P2150L P85L Q597R R1097C Another file P2150L P85L Q597R R1097C R1379C R1587K Then output shud be R1379C R1587K thanks (5 Replies)
Discussion started by: cdfd123
5 Replies
Login or Register to Ask a Question