English 中文(简体)
如何比较双壳书写中的两个档案?
原标题:How to compare two files in shell script?
  • 时间:2009-10-09 12:13:14
  •  标签:

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

请事先帮助我

问题回答

我认为,你们需要的是:

if [ "$d" != NULL ];

Try.

Unless You are written ascript f或 p或tability to the 或iginal Bourne shell 或 others that do not supp或t the n或m, in Bash and ksh 您应使用<条码>[>标准表格,以便查找和存档。

引用和教唆的需要减少,其他条件,例如模式和定期表述配对,以及使用<代码>&和<>>>>>>> 而不是代码>-a-o

if [[ $var == $var1 ]]

此外,“NUL”在Bash和ksh并不具有特殊价值,因此,你的测试将永远成功,因为$在字面“NUL”上测试。

if [[ $d != "" ]]

if [[ $d ]]

F或 numeric values (not including leading zeros unless you re using octal), you can use numeric expressions. You can omit the dollar sign f或 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 f或 substrings. In Bash and ksh you can do:

var=${line:3:23}

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

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





相关问题
热门标签