Skip to content
Advertisement

Matching files using awk in linux

I have 2 files:

1.txt:

e10adc3949ba59abbe56e057f20f883e
f8b46e989c5794eec4e268605b63eb59
e3ceb5881a0a1fdaad01296d7554868d

2.txt:

e10adc3949ba59abbe56e057f20f883e:1111
679ab793796da4cbd0dda3d0daf74ec1:1234
f8b46e989c5794eec4e268605b63eb59:1@/233:

I want 2 files as output: One is result.txt which contains lines from 2.txt whose match is in 1.txt and another is left.txt which contains lines from 1.txt whose match is not in 2.txt

Expected output of both files is below: result.txt

e10adc3949ba59abbe56e057f20f883e:1111
f8b46e989c5794eec4e268605b63eb59:1@/233:

left.txt

e3ceb5881a0a1fdaad01296d7554868d

I tried 1-2 approaches with awk but not succeeded. Any help would be highly appreciated.

My script:

awk '
FNR==NR{
  val=$1;
  sub(/[^:]*/,"");
  sub(/:/,"");
  a[val]=$0;
  next
}
!($NF in a){
  print > "left.txt";
  next
}
{
  print $1,$2,a[$NF]> "result.txt"
}
'  FS=":" 2.txt FS=":"  OFS=":" 1.txt

Advertisement

Answer

Following awk may help you in same.

awk 'FNR==NR{a[$1]=$0;next} ($0 in a){print a[$0] > "results.txt";next} {print > "left.txt"}' FS=":" OFS=":" 2.txt FS=" " OFS=":" 1.txt

EDIT: Adding explanation of code too here.

awk '
FNR==NR{    ##FNR==NR condition will be TRUE when first Input_file is being read by awk. Where FNR and NR are the out of the box variables for awk.
  a[$1]=$0; ##creating an array named a whose index is $1 and value is $2 from 2.txt Input_file.
  next      ##next is out of the box keyword from awk and will skip all further statements of awk.
}
($0 in a){  ##Checking here condition if current line of Input_file 1.txt is present in array named a then do following.
  print a[$0] > "results.txt"; ##Printing the current line into output file named results.txt, since current line is coming in array named a(which was created by 1st file).
  next      ##next is awk keyword which will skip further statements for awk code now.
}
{
  print > "left.txt" ##Printing all lines which skip above condition(which means they did not come into array a) to output file named left.txt as per OP need.
}
' FS=":" OFS=":" 2.txt FS=" " OFS=":" 1.txt ##Setting FS(field separator) as colon for 2.txt and Setting FS to space for 1.txt here. yes, we could set multiple field separators for different Input_file(s).
Advertisement