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

Alex
  • 334
  • 5
  • 15
  • 1
    try sdiff or comm and also see this post : http://unix.stackexchange.com/questions/325087/how-to-sort-and-join-files-in-linux-with-huge-data-and-print-blank-values-for-th/325207#325207 – George Vasiliou Nov 22 '16 at 23:19

2 Answers2

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
steeldriver
  • 81,074
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.)

Wildcard
  • 36,499
  • 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