【问题标题】:Compare alternate lines in two files in unix using grep使用 grep 比较 unix 中两个文件中的备用行
【发布时间】:2017-04-16 09:33:50
【问题描述】:

我正在尝试使用 unix 比较两个文件并返回成功(如果 file1 内容与 file2 匹配)或如果不匹配则返回失败并显示不匹配的记录

file1 的内容:

columnA1 columnA2
121     ab354664
columnB1 columnB2
143     be000431
ColumnC1 columnC2
001     21uy7732

file2 的内容:

columnA1 columnA2
121     ab354664
columnB1 columnB2
143     be000431
columnC1 columnC2
431     66575wqq

我有以下代码:

if (grep -v "column" $file1) == (grep -v "column" $file2)
then
    echo -e "match"
else
    echo -e "dont match"
    grep -B 1 -v "column" $file
fi    

它抛出错误

syntax error near unexpected token `=='

我不确定是否可以使用比较操作来比较两个不同文件中的字符串。

请提出建议。

【问题讨论】:

  • 您可以在某些情况下使用== 来比较两个字符串,但您没有两个字符串!但是你为什么不直接做diff file1 file2呢?
  • 你在使用bash吗?作为你的原生 shell 还是其他?
  • 是的,我只使用 bash

标签: unix if-statement grep


【解决方案1】:

试试下面的 awk 命令 -

$ cat f1
columnA1 columnA2
121     ab354664
columnB1 columnB2
143     be000431
ColumnC1 columnC2
001     21uy7732

$ cat f2
columnA1 columnA2
121     ab354664
columnB1 columnB2
143     be000431
columnC1 columnC2
431     66575wqq

cmd1:只打印 f1 和 f2 的匹配列

$ awk 'NR==FNR{a[$1FS$2]=$0;next}  ($1FS$2) in a {print  a[$1FS$2] " match"}' f2 f1
columnA1 columnA2 match
121     ab354664 match
columnB1 columnB2 match
143     be000431 match

cmd2: 打印文件 f1 & f2 的匹配列和不匹配的列 文件 f1

$ awk 'NR==FNR{a[$1FS$2]=$0;next} {print (a[$1FS$2] ? a[$1FS$2] " match" : $0 " Do not")}' f2 f1
columnA1 columnA2 match
121     ab354664 match
columnB1 columnB2 match
143     be000431 match
ColumnC1 columnC2 Do not
001     21uy7732 Do not

EDIT1 : 要忽略包含 Column 的行,请使用以下 -

$ awk 'NR==FNR{a[$1FS$2]=$0;next}  ($1FS$2) in a {if(a[$1FS$2] !~ /column/) {print a[$1FS$2] " match"}}' f2 f1
121     ab354664 match
143     be000431 match

EDIT2:仅打印不匹配的列 -

awk 'NR==FNR{a[$1FS$2]=$0;next}  {print  (!a[$1FS$2]?$0:"")}' f1 f2



columnC1 columnC2
431     66575wqq

【讨论】:

  • 这行得通。我做了一些修改以符合我的要求。并仅显示备用行(即排除列并比较具有数字的行。awk 'NR==FNR{a[$2]=$0;next} {(a[$2] ? a[$2] : print $0 " Do not")}' LL_CNT_Result_20170412_0743 LL_PD_Result_20170412_0743。但我只想在 file2 的输出中打印不匹配的行。columnC1 431 columnC2 66575wqq。为此我做了awk 'NR==FNR{a[$2]=$0;next} {( print a[$2] ? a[$2] :$0 "[$2]")}' f1 f2。这给了我一个语法错误
  • 谢谢。但我试图仅从 file2 column C1 columnC2 431 66575wqq 打印不匹配的行。这似乎让我再次感到困惑
  • @user1023627 - 检查我更新的答案 Edit2,它打印不匹配的值。
【解决方案2】:

也许diff 可以:

diff <(grep -vi column file1) <(grep -vi column file2)

输出:

3c3
< 001     21uy7732
---
> 431     66575wqq

或者,如果您想将其与 if-then-else 一起使用:

if diff <(grep -vi column file1) <(grep -vi column file2) > /dev/null; then 
  echo Yes
else 
  echo No
fi

输出:

No

【讨论】:

  • 这行得通。但我想从 file2 打印不匹配的条目。即columnC1 columnC2 431 66575wqq
【解决方案3】:

在一个 awk 解决方案中,打印文件中存在的哪些记录在另一个文件中不存在:

$ awk 'NR==FNR {
           file1=FILENAME
           a[$0]++
           next
       }
       ($0 in a) && a[$0]>0 {
           a[$0]--
           next
       }
       {
           print $0 " not in " file1
       }
       END {
           for(i in a)
               if(a[i]>0)
                   print i " not in " FILENAME
       }' file1 file2
columnC1 columnC2 not in file1
431     66575wqq not in file1
001     21uy7732 not in file2
ColumnC1 columnC2 not in file2

另外,如果file1 有两次记录而file2 只有一次,这是不匹配的。输出可能更漂亮...

【讨论】:

    【解决方案4】:

    如果每个文件中有两列,则可以将它们与paste 一起压缩,然后与awk 进行比较,例如:

    paste file1 file2 | 
    awk 'NR%2 { h = $0 } !(NR%2) && ($1 != $3 || $2 != $4) { print h; print $0 }'
    

    输出:

    ColumnC1 columnC2   columnC1 columnC2
    001     21uy7732    431     66575wqq
    

    【讨论】:

      猜你喜欢
      • 2012-12-17
      • 1970-01-01
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-17
      相关资源
      最近更新 更多