0

I have two different files with one column each. The file 1 has more info and I want a command to search the matches against the file 2 and then return the original file 1 with an extra column saying for example "match"

File 1

Mg_134
Mg_560

File 2

Mg_1
Mg_134

Output

Mg_134 match
Mg_560

I tried to use join and grep -Ff, but I would like to have this specific output. Thanks

1

That should be a pretty standard task for awk

awk 'NR==FNR {a[$1]++; next} $1 in a {$2="match"}1' File2 File1
Mg_134 match
Mg_560
1

If the files are sorted, the standard tool for this is comm:

comm -2 file1 file2

The -2 option is to suppress printing lines unique to file2.

It doesn't give the exact format you specify. However, you can parse it to give that output if you like. (Standard tools and standard formats are usually pretty well thought out.)

  • Did not work, I sorted the files using "sort -V" and I got the same number of lines of the file 1, and the messages: comm: file 1 is not in sorted order comm: file 2 is not in sorted order – Alex Nov 23 '16 at 15:08
  • @Clarissa, sort -V is a GNU extension, not standard. That doesn't count as being actually sorted. See the specs for comm as linked to from my answer. – Wildcard Nov 23 '16 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.