1

I have two text files, a.txt and b.txt where:

a.txt

1
2
3
4
5

b.txt

3
5

I'd like to have c.txt where matching numbers will be printed "0" and non matching numbers to be "1".

1
1
0
1
0

I understand that comm can suppress/ print unique lines but how do I get around to get the c.txt as above?

Many thanks.

1
  • yeah, I'm sure it is, I'm just really bad at it :) – jiveturkey Feb 13 '17 at 22:08
2

If you wanted to mess around with diff and not join here's another option.

sdiff -l a.txt b.txt | sed -e 's/.*<.*/1/' -e 's/.*(.*/0/'

You could probably get some better sed expression in there or change it up using awk but it's a quick shot, then just redirect this to c.txt

2

As don_crissti pointed out, with awk it's pretty simple:

awk 'FNR==NR { match_nmbrs[$1]++; next;}
             {if($1 in match_nmbrs)
                  print 0;
              else
                  print 1; }
    ' b.txt a.txt

The first {...} condition works eclusively on records in the first file, while the second {...} condition works exclusively on records in the second file. All other things are self-explaining imo.

2

Use awk on the comm output:

$ comm a.txt b.txt | awk -F'\t' '$1 || $2 { print 1 } $3 { print 0 }'

comm will output three tab-separated columns:

  1. lines only in the first file,
  2. lines only in the second file, and
  3. lines present in both files.

The awk script will classify any input that has something in either of the first two columns as a "mismatch" and output 1, and if there's something in the third column it will output 0 for a match.

Note that comm requires the two files to be sorted.

1
  • One potential problem is that comm expects its inputs in LC_COLLATE order, so you may have to sort first if input file contains, say, both 1 and 10 – iruvar Feb 13 '17 at 23:50
0

Here's another awk solution

awk '1,$0=system("grep -qx "$1" b.txt")' a.txt

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.