Linux – How to Compare Column1 of Two Files

awkcolumnslinuxtext processing

I need to compare the column 1 of file 1 with the column 1 of file2 in my script, if the column 1 of file1 matches with the column 1 of file2, then only it should proceed further ,else exit.

I'm with the below code, but it's not giving me the desired result:

if awk 'NR==FNR{c[$1]++;next};c[$1] > 0' /path/abc/example.log /path/abc/example2.log
then
//perform some actions//
else
exit 1
fi

Input data:

file1:

77 abc 20000200 FAILED 10-10-2018 03:37:36
94 hgu 20000126 FAILED 10-10-2018 03:37:34

file2:

77 abc 20000200 FAILED 10-10-2018 03:37:36

In the above sample data, column 1 of file1 doesn't match with the column 1 of file2, so in this case, it should exit.

Hope i'm clear.

Best Answer

#!/bin/bash

var=$(cut -d" " -f 1 file1)
var1=$(cut -d" " -f 1 file2)

if [ "$var" == "$var1" ]
then
echo "columns are matching each other "
else
echo "columns are not matching with each other!"

fi
Related Question