【问题标题】:Find common values in field查找字段中的共同值
【发布时间】:2015-06-28 01:52:25
【问题描述】:

假设我有 2 个带有值的文件:

文件A:

name        occupation      grade
ishaan      doctor          best
sinha       engineer        worst
palak       sales           good
nishant     commissioner    best
ishaan      doctor          good
palak       sales           okey

文件B:

name        grade       occupation
ishaan      best        doctor
sinha       worst       engineer
palak       good        sales
nishant     best        commissioner
ishaan      good        doctor
palak       okey        sales

我想使用 shell 脚本仅打印两个文件中字段 Occupation 中的公共值。如果您建议使用 awk 实用程序,请同时解释命令(带参数)。

【问题讨论】:

    标签: php linux shell unix


    【解决方案1】:

    在终端打印输出:

    awk '{print $(NF-1)}'
    

    将输出导出到文件:

    awk '{print $(NF-1)}' /local/file/path > /output/file/path
    

    注意:NF -- 打印最后一个字段。 NF-1 -- 打印最后一个字段 -1

    【讨论】:

    • 感谢您的回复。但它正在打印所有值..它没有给出通用数字。 :( 我只需要这两个领域的共同值.....
    • 有人可以帮忙查询吗???我需要在上面提到的行中找到共同的值。
    【解决方案2】:

    认为这不是一个好的答案,但你试试这个..

    您运行 for 循环获取记录并使用 array_push() 将每个记录推送到新数组中;

    并检查 if(!in_array()) 不在数组中然后显示新记录..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      相关资源
      最近更新 更多