在awk中比较2个文件

Comparing 2 files in awk

我有 2 个输入文件如下。

File1

India USA China Russia France England

File2

India
USA
China
Russia
France
England

我需要验证文件 2 中的所有列是否在文件 1 中以相同的顺序可用。在 awk scripting(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

可以请您尝试关注一次吗?仅使用提供的样品进行测试。

awk '
{
  sub(/[[:space:]]+$/,"")
}
FNR==NR{
  a[FNR]=[=10=]
  next
}
{
  for(i=1;i<=NF;i++){
    count=a[i]==$i?++count:count
  }
  if(count==length(a)){
    print "Line number " FNR " whose contents are:" [=10=] " present in both the files."
  }
  count=""
}'   Input_file2   Input_file1

输出结果如下。

Line number 1 whose contents are:India USA China Russia France England present in both the files.

如果您的字符串可能在其值中包含大写或小写字母,现在添加可以解决该问题的解决方案。

awk '
{
  sub(/[[:space:]]+$/,"")
}
FNR==NR{
  a[FNR]=tolower([=12=])
  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:" [=12=] " present in both the files."
  }
  count=""
}'  Input_file2   Input_file1

或者在带有 else 条件的聊天代码中讨论。

awk '
{
  sub(/[[:space:]]+$/,"")
}
FNR==NR{
  a[FNR]=tolower([=13=])
  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:" [=13=] " present in both the files."
  }
  else{
    print "Line number " FNR " is NOT matching..."
  }
  count=""
}'  Input_file2   Input_file1

即使 File1 有多行,以下 awk 命令也能正常工作。

awk 'FNR == NR { for (i=1;i<=NF;++i) a[++n] = $i; next }
     { print "Line " FNR ": " ([=10=] == 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 [=10=], ( ==  ? "" : "no ") "match"}'
#India   India match
#USA     USA match
#China   China match
#Russia  Russia match
#France  France match
#England England match

使用 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