How do I compare two files in a shell script?

Here's my scenario. I have two files that have entries with each entry. 3-25 characters is the identifier. Based on this, I need to compare both of them and update the old file with the new file data if their IDs are the same. IDs start with 01. Take a look at the script below. This is giving some error as "the argument expected on line 12 that I cannot understand".

#!/bin/ksh
while read line
  do
    c=`echo $line|grep '^01' `
    if [ $c -ne NULL ];
      then
        var=`echo $line|cut -c 3-25`
    fi
    while read i
      do
        d=`echo $i|grep '^01' `
        if [ $d -ne NULL ];
          then
            var1=`echo $i|cut -c 3-25`
            if [ $var -eq $var1 ];
              then
                $line=$i
            fi
        fi
      done < test_monday
  done < test_sunday

      

Please help me well in advance

0


source to share


3 answers


I think what you need is:

if [ "$d" != NULL ];
      



Try.

+1


source


Unless you are writing a script for portability of the original Bourne shell or others that do not support this feature, in Bash and ksh you should use the [[

test form
for strings and files.

There is a reduced need for quoting and escaping, additional conditions such as pattern and regex matching, and the ability to use &&

and ||

instead of -a

and -o

.

if [[ $var == $var1 ]]

      

Also, "NULL" is not a special value in Bash and ksh, so your test will always succeed because it $d

tests for the literal string "NULL".

if [[ $d != "" ]]

      

or

if [[ $d ]]

      



For numeric values โ€‹โ€‹(not including leading zeros if you don't use octal), you can use numeric expressions. You can omit the dollar sign for variables in this context.

numval=41
if ((++numval >= 42))  # increment then test
then
    echo "don't panic"
fi

      

No need to use echo

and cut

substring. In Bash and ksh, you can:

var=${line:3:23}

      

Note: cut

uses character positions for the start and end of a range, whereas this shell construct uses a start position and number of characters, so you need to adjust the numbers accordingly.

And it's a good idea to get away from using backticks . Use instead $()

. It can be nested and quoting and speeding up is shortened or simplified.

+1


source


I think you could use the DIFF command

diff file1 file2 > whats_the_diff.txt

      

0


source







All Articles