views:

735

answers:

3

Here is my scenario. I have two files which are having records with each record's 3-25 characters is an identifier. Based on this I need to compare both of them and update the old file with the new file data if their identifiers match. Identifiers start with 01. Please look at the script below. This is giving some error as "argument expected at line 12 which I am not able to 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 out thanks in advance

+1  A: 

I think what you need is :

if [ "$d" != NULL ];

Try.

NawaMan
that helped in removing first error...thanks....but there is one more simialr error when comparing both the indicators....inif [ $var == $var1 ]; help needed........
ALN
DId you make sure the "Spacings" in if statement?
Aviator
The thing about comparing string in bash is that you should put quot around it unless you are so sure that it will never have space in it. Because Bash will substitute the value of that variable in as parameter for "if" and parameters are separated by space (therefore if the value contain space, you get multiple variable). So first, wrapping it with quotes. Next is that, use "==" and "!=" when you compare strings. :D
NawaMan
A: 

I think you could use the DIFF command

diff file1 file2 > whats_the_diff.txt
Phill Pafford
Thats not what I needed. I need to compare only the identifiers and update like in database.
ALN
A: 

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

There is a reduced need for quoting and escaping, additional conditions such as pattern and regular expression 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 and so your test will always succeed since $d is tested against the literal string "NULL".

if [[ $d != "" ]]

or

if [[ $d ]]

For numeric values (not including leading zeros unless you're using 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

It's not necessary to use echo and cut for substrings. In Bash and ksh you can do:

var=${line:3:23}

Note: cut uses character positions for the beginning and end of a range, while this shell construct uses starting position and character count so you have to adjust the numbers accordingly.

And it's a good idea to get away from using backticks. Use $() instead. This can be nested and quoting and escaping is reduced or easier.

Dennis Williamson