0

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.

  • What do you mean by 'column1 match column 2'? All rows from col1 should be in col2 in the same order, and both columns should have the same amount of rows ? – mrc02_kr Oct 10 '18 at 10:17
  • sorry, i've edited my question, yes, you're right: "All rows from col1 of file1 should be in col1 of file2 in the same order, and both columns should have the same amount of rows" – User123 Oct 10 '18 at 10:18
2
#!/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

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.