Is there an easy way to print all lines of a file1 (so that the output has the same number of lines as the input file1) but print a message such as NoMatch where the first entries of file1 does not match the first entries of file2? file1:Entry1 Entry2a 2b 3c 4d 5file2:abbaddDesired output: Entry1a 2b 3NoMatch 4d 5I am trying withjoin -a1 -e NoMatch -11 -21 -o2.1 file1 file2since I would like to keep the unpairable lines from file1 that do not match file2, and give a message for these cases as no, but this keeps all of my records also in file2 (which contains duplicated records), what am I doing wrong? Could this be because my second file is tab delimited and my first file is space delimited?Thanks so much for all the help...
How do I print all records in file 1 but note the records that do not match file2?
text processing;awk;join
With awk read file2 first and save $1 in seen[$1] then read file1 and if $1 wasn't seen replace it with NoMatch:awk 'FNR==NR{seen[$1]++; next}{(FNR==1 || ($1 in seen)) || $1=NoMatch};1' file2 file1If you prefer join, you need sorted input. You'll have to extract the header from file1 first, sort the remaining lines then join the result with the sorted file2:{ head -n1; sort | join -j1 -a1 -e NoMatch -o 2.1 1.2 - <(sort file2); } <file1and if needed, pipe everything to uniq to avoid duplicate lines:{ head -n 1; sort | \join -j1 -a1 -e NoMatch -o 2.1 1.2 - <(sort file2) | \uniq; } <file1