Search code examples
linuxfilescriptingcomparisonmultiple-columns

Two file Comparison with while .sh


The script written in sh is used to compare the columns of two different files and give the output.

File 1 content

9999
0
2
3

File 2 content

SDC 4
abc 0
abx 2
cdf 3

Script logic

 while read Colm_1_File1 <&3 && Colm_1_File2 Colm_2_File2 <&4

     do

     Check_Count_File1=$Colm_1_File1

     v_File_Name=$Colm_1_File2

     Check_Count_File2=$Colm_1_File2

     if [ "$Check_Count_File1" -eq "$Check_Count_File2" ]

      then

      echo "$v_File_Name:- OK" 
      elif [ "$Colm_1_File1" -eq "9999" ]

      then

      echo "$v_File_Name File Missing"

      else

      echo "$v_File_Name:- NOK" 

     fi

     done 3<File1 4<File2 

The if condition only works from "0"; any other number that is equal doesn't match. elif and else behave as expected.


Solution

  • THis is what you need, your variable name Colm_1_File2 is written two times, hence the issue

    while read Colm_1_File1 <&3 && read Colm_1_File2 Colm_2_File2 <&4
     do
     Check_Count_File1=$Colm_1_File1
     v_File_Name=$Colm_1_File2
     Check_Count_File2=$Colm_2_File2
     if [ "$Check_Count_File1" -eq "$Check_Count_File2" ]
      then
      echo "$v_File_Name:- OK"
      elif [ "$Colm_1_File1" -eq "9999" ]
      then
      echo "$v_File_Name File Missing"
      else
      echo "$v_File_Name:- NOK"
     fi
     done 3<File1 4<File2