Hi,
I am looking for a way to compare two text files and print the matches. For example;
File1.txt
89473036
78474384
48948408
95754748
47849030
File2.txt
47849030
46730356
16734947
78474384
36340047
Output:
78474384
47849030
Hi,
I am looking for a way to compare two text files and print the matches. For example;
File1.txt
89473036
78474384
48948408
95754748
47849030
File2.txt
47849030
46730356
16734947
78474384
36340047
Output:
78474384
47849030
awk '{++a[$2]} END {for(i in a) if(a>1) print i}' file1 file2
awk 'NR==FNR{a[$2];next}$2 in a{print $2}' file1 file2
--ahamed
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
8947303689037809238094
7847438449838
4894840834098337
95754748280926266290
478490309034879362612
File2.txt
47849030292119167904387
46730356340247247474934994287
478490309034879362612
4894840834098337
363400472727249727
Output:
4894840834098337
478490309034879362612
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...
grep -f file2 file1
--ahamed
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.
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?
Ahh man, my apologies those were meant to be line numbers, they were not necessary. My text editor notes the line numbers and I added them to my original post without realising Ahamed's suggestion took it into consideration. My bad, I have now removed them. :wall:
awk 'NR==FNR{a[$1];next}$1 in a{print}' file1 file2
or
grep -f file1 file2
When I copied your contents, there were spaces at the end of some lines (and that is probably the reason why grep didn't work), if that is the case use awk else the grep should do the job!
--ahamed
And if there are no double entries in the first file then this should work too:
awk 'A[$1]++' file1 file2
Thanks guys, the awk statements are working well now!
Can any one give me the whole programming for this need in perl?? please