1

I want to compare two files and print only the first word of the lines that match and that don't match along with a string.

file1.txt
=========

abc 123
bcd 234
cde 345

file2.txt
=========

abc 123
bcd 223
cde 234

above abc is matching in both file1.txt and file2.txt. So I need to print as below:

abc is matching. it is ok.

above bcd and cde in file1.txt and file2.txt are not different. So I need to print as below:

bcd is not matching. please check.
cde is not matching. please check.
4
  • 1
    Where did you get stuck?
    – Jeff Schaller
    Dec 27, 2016 at 2:57
  • I am new to scripting and learning things. I tried by using the solutions provided by other users in this forum for other similar questions but could not proceed beyond displaying the first word.
    – user206245
    Dec 27, 2016 at 4:00
  • And Jeff. I now understood from your question it would be helpful if I mentioned what was done and where I was stuck. I'll surely follow this going forward.
    – user206245
    Dec 27, 2016 at 4:05
  • Sounds good, and welcome to U&L stack exchange! Some of us like to see an effort to a point where you get stuck; others like to solve the problem on their own. Just something to be aware of going forward.
    – Jeff Schaller
    Dec 27, 2016 at 4:33

3 Answers 3

2

With awk:

awk 'NR==FNR{a[$1]=$2; next}; $2==a[$1] {print $1 " OK"; next} \
                                        {print $1 " Not OK"}' f1.txt f2.txt
  • NR==FNR is true for only the first file, f1.txt here; we are creating array a with the first field as the key and the second field as the value, next goes to the next record without moving further down the conditionals

  • $2==a[$1] checks if the second field of the second file, f2.txt matches the values of the key first field of the array a, and then printing in desired format

Example:

% cat f1.txt
abc 123
bcd 234
cde 345

% cat f2.txt
abc 123
bcd 223
cde 234

% awk 'NR==FNR{a[$1]=$2; next}; $2==a[$1] {print $1 " OK"; next} {print $1 " Not OK"}' f1.txt f2.txt
abc OK
bcd Not OK
cde Not OK
1
  • Thank you for your time and help on this. It is working perfectly.
    – user206245
    Dec 27, 2016 at 4:03
1
awk 'NR==FNR{A[NR]=$0;next}{if($0==A[FNR]){print $0 "is matching. it is ok"}else{print $0 "is not matching. please check"}}' file1.txt file2.txt

readable format

awk 'NR==FNR{
A[NR]=$0;
next
}
{
if($0==A[FNR])
{
    print $0 "is matching. it is ok"
}
else{
print $0 "is not matching. please check"
}
}' file1.txt file2.txt
1
  • Kamaraj thank you for your time and help. I tried with your code, but it is giving the entire line along the string that we wish to add to it. @heemayl example is giving the desired output.
    – user206245
    Dec 27, 2016 at 4:03
1

the code @ Kamaraj has been updated. Please you need replace the the lines :

print $0 "is matching. it is ok"
print $0 "is not matching. please check"

by

split($0,a,/ /); print a[1] " is matching. it is ok"
split($0,a,/ /);print a[1] " is not matching. please check"

or print $1 "is matching. it is ok" print $1 "is not matching. please check"

All the script will be :

awk 'NR==FNR{
A[NR]=$0;
next
}
{
if($0==A[FNR])
{
split($0,a,/ /); print a[1] " is matching. it is ok"
}
else{
split($0,a,/ /);print a[1] " is not matching. please check"
}
}' file1.txt file2.txt

TIMES:

@heemayl real 0m0.159s user 0m0.000s sys 0m0.046s

@Kamaraj real 0m0.102s user 0m0.000s sys 0m0.031s with split

@Kamaraj real 0m0.099s user 0m0.000s sys 0m0.062s with $1

Thanks @Kamaraj.

2
  • no need to use the split. instead $0, we can use $1
    – Kamaraj
    Dec 27, 2016 at 8:46
  • Solution With Performance. Thanks @Kamaraj. Need to check code though. :)
    – user206245
    Dec 27, 2016 at 16:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .