Shell 在awk中比较2个文件

Shell 在awk中比较2个文件,shell,unix,awk,ksh,Shell,Unix,Awk,Ksh,我有2个输入文件,如下所示 File1 India USA China Russia France England File2 India USA China Russia France England 我需要验证文件2中的所有列是否在文件1中以相同的顺序可用。在awk脚本(ksh)中实现这一点的有效方法是什么 我已经编写了一个示例脚本,如下所示。但是,我想知道简单有效的解决方案 #!/bin/ksh i=1 while read line do val=

我有2个输入文件,如下所示

File1

India USA China Russia France England

File2

India
USA
China
Russia
France
England
我需要验证文件2中的所有列是否在文件1中以相同的顺序可用。在awk脚本(ksh)中实现这一点的有效方法是什么

我已经编写了一个示例脚本,如下所示。但是,我想知道简单有效的解决方案

#!/bin/ksh
        i=1
while read line
do
        val=`cat File1 | cut -d" " -f$i`
        echo $val $line

        if [ $val = $line ]
        then
                echo "Matches"
        else

                echo "Not matches"
        fi
        i=$((i+1))
done < ./File2
#/bin/ksh
i=1
读行时
做
val=`cat File1 | cut-d”“-f$i`
echo$val$行
如果[$val=$line]
然后
回声“匹配”
其他的
回显“不匹配”
fi
i=$((i+1))
完成
请您尝试一下下面的内容好吗?仅使用提供的样品进行测试

awk '
{
  sub(/[[:space:]]+$/,"")
}
FNR==NR{
  a[FNR]=$0
  next
}
{
  for(i=1;i<=NF;i++){
    count=a[i]==$i?++count:count
  }
  if(count==length(a)){
    print "Line number " FNR " whose contents are:" $0 " present in both the files."
  }
  count=""
}'   Input_file2   Input_file1
如果字符串的值中可能包含大写或小写字母,现在添加解决方案可以解决该问题

awk '
{
  sub(/[[:space:]]+$/,"")
}
FNR==NR{
  a[FNR]=tolower($0)
  next
}
{
  for(i=1;i<=NF;i++){
    count=a[i]==tolower($i)?++count:count
  }
  if(count==length(a)){
    print "Line number " FNR " whose contents are:" $0 " present in both the files."
  }
  count=""
}'  Input_file2   Input_file1
awk'
{
子(/[:空格:]+$/,“”)
}
FNR==NR{
a[FNR]=托洛尔(0美元)
下一个
}
{

对于(i=1;i,即使
File1
有多行,以下awk命令也将起作用

awk 'FNR == NR { for (i=1;i<=NF;++i) a[++n] = $i; next }
     { print "Line " FNR ": " ($0 == a[FNR] ? "matches" : "does not match.") "." }' file1 file2
$paste使用Perl

$ cat vinoth1.txt
India USA China Russia France England

$ cat vinoth2.txt
India
USA
China
Russia
France
England

$ perl -lane ' BEGIN{@k=map{chomp($_);$_} qx(cat vinoth2.txt)} print "Line:$. matches" if join(" ",@k) eq join(" ",@F) ' vinoth1.txt
Line:1 matches

另外,请务必在您的帖子中提及预期输出。使用ksh或bash:
如果diff-Z使用示例代码和我的要求更新了问题。删除后面的空格Russia@Cyrus我尝试了你提到的方法,但它给出了以下错误。差异:无效选项--'Z'Remove
-Z
选项仅用于忽略GNU diff.@Vinoth karthigh行末尾的空格,如果这对您有帮助,请尝试一下我的解决方案好吗?我只是复制粘贴了您的代码(更改了输入文件名)在ksh scirpt中,并执行ksh脚本。它不提供任何输出。我应该以其他方式运行它吗?@Vinothkarchigh,首先尝试在服务器终端本身上作为命令运行它,并确保您正在传递2个文件名,如我所述,如下所示
Input_file2 Input_file1
,让我知道吗?我也尝试在终端上运行。这是没有给出任何输出,文件名正确无误given@VinothKarthick,好的,您可以检查两个文件上运行的
cat-v file1
cat-v file2
,并检查其中是否有回车字符吗?请告诉我?
awk 'FNR == NR { for (i=1;i<=NF;++i) a[++n] = $i; next }
     { print "Line " FNR ": " ($0 == a[FNR] ? "matches" : "does not match.") "." }' file1 file2
Line 1: matches.
Line 2: matches.
Line 3: matches.
Line 4: matches.
Line 5: matches.
Line 6: matches.
$ paste <(tr ' ' '\n' < file1) file2 | awk '{print $0, ($1 == $2 ? "" : "no ") "match"}'
#India   India match
#USA     USA match
#China   China match
#Russia  Russia match
#France  France match
#England England match
$ cat vinoth1.txt
India USA China Russia France England

$ cat vinoth2.txt
India
USA
China
Russia
France
England

$ perl -lane ' BEGIN{@k=map{chomp($_);$_} qx(cat vinoth2.txt)} print "Line:$. matches" if join(" ",@k) eq join(" ",@F) ' vinoth1.txt
Line:1 matches