【问题标题】:Compare fields in two files and print to another in shell比较两个文件中的字段并在 shell 中打印到另一个文件
【发布时间】:2018-01-11 17:17:02
【问题描述】:

请帮我解决以下问题。我想在 shell 脚本中打印第三个文件中的值

文件的格式应符合以下一项 a_file - col1 col2 col3 b_file - col1 col2

。 a_file - col1 col2 col3 1个PI 1 1Q JI

.b_file 1 我

如何比较 a_file 的第 1 字段和第 3 字段与 b_file 的第 1 和第 2 字段?谁能帮我解决这个问题。

【问题讨论】:

  • 打印什么值?到目前为止你做了什么?

标签: linux shell awk


【解决方案1】:

好吧,如果我正确理解您的问题,最简单的方法是逐行读取两个文件,比较指定的列并将自定义输出写入第三个文件。

 # read 3 columns from file descriptor 11 and two columns from file descriptor 12
 while read -u 11 acol1 acol2 acol3 && read -u 12 bcol1 bcol2; do
    # do comparisions betweencolumns
    if [ $acol1 = $bcol1 -a $acol3 = $bcol2 ]; then
       echo Comparision true
       # print values in 3rd file in shell scripting
       echo $acol1 $bcol1 $acol3 $bcol2 >>3rd_file
    else
       echo Comparision false
    fi
  # bind a_file to 11 file descriptor, and b_file to 12 file descriptor
  done 11<a_file 12<b_file

【讨论】:

    猜你喜欢
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    相关资源
    最近更新 更多