0

Let's say I have two files, file1 and file2.

file1:

passd:xxx
hopla:alli
gnar:gungg
araf:utap

file2:

alli
utap

How can i check what lines/words from file2 match file? Indeed I could probably do it with comm -1 -2 file1 file2 but is it possible to do it with awk?

trolkura
  • 407
  • 1
  • 6
  • 12

3 Answers3

0

With awk, you can start with what @jasonwryan suggested Compare two files...

But modify it to suit your needs. Here is what you can do on the command line, with results:

$ awk -F: 'FNR==NR {arr[$0];next} $2 in arr' file2 file1
hopla:alli
araf:utap
  • you specified file2 was the list with words to search for
  • file1 was the large text file to search in
  • -F: specifies delimiter is colon :
  • $2 since the field to match is in this case file1's field 2
  • when match occurs, default action to print the line, thus you see the two matching lines

Additional info

clarity123
  • 3,519
  • 1
  • 12
  • 16
0

This command could help you:

cat file1 | awk '{FS=":" print $0}' | xargs grep file2

I'm not sure if the diff command could help. But if it is installed, I would use it: diff file1 file2

Kiran
  • 313
  • 3
  • 11
  • 21
0

If having issues with grep,

  1. Try dos2unix file1 file2 beforehand to remove any problematic non-Unix formatting that could prevent grep from working
  2. as @drewbenn mentioned, grep -f file2 file1

You should then successfully see the matching lines:

hopla:alli
araf:utap
clarity123
  • 3,519
  • 1
  • 12
  • 16