首页 > 解决方案 > 在比较列时打印具有差异的整行

问题描述

我想打印值不匹配的整行

例如:

Symbol Qty       Symbol Qty         Symbol qty

a      10          a     10           a    11


b      11          b     11           b    11

c      12          c     12           f    13 

f      12          f      12          g    13                     

输出 :

a   10    a      10        a    11 

c   12    c      12        (empty Space)

f 12      f       12    f    13

empty space    {ES}         g   13


awk 'FNR==NR{a[$0];next}!($0 in a ) '  output1.csv output2.csv  >> finn1.csv
awk 'FNR==NR{a[$0];next}!($0 in a ) '  finn1.csv output4.csv  >> finn.csv

但这会打印在一列中,就像 11 一样,但我需要整行

标签: bashscripting

解决方案


假设您只想测试不匹配的Qty字段,请尝试以下操作:

#!/bin/bash

declare    input_file="/path/to/input_file"

declare -i header_flag=0 a b c
while read line; do
  [ ${header_flag} -eq 0 ] && header_flag=1 && continue   # Ignore first line.
  [ ${#line} -eq 0 ] && continue                          # Ignore blank lines.
  read x a x b x c x <<< ${line}         # Reuse ${x} because it is not used.
  [ ${a} -ne ${b} -o ${a} -ne ${c} -o ${b} -ne ${c} ] && echo ${line}
done < ${input_file}

推荐阅读