【问题标题】:Is there a way to filter a file for lines that only occur once, based on a field?有没有办法根据字段过滤文件中只出现一次的行?
【发布时间】:2019-11-02 07:25:28
【问题描述】:

我有一个“类似差异”的文件,其中的字段由制表符分隔。有没有办法只使用标准的 gnu 工具只获取出现一次的行,如果它们的第二个字段相等,则两条线相等?

本质上,我只想过滤这个文件中的新行和删除的行,而不是编辑的行。

文件首先按第二列(名称)排序,然后按第一列(减法然后加法)。

<   Chandler Hutchison  Chicago Bulls   Wed, Oct 30, 2019   Out (Hamstring) - The Bulls have listed Hutchison as OUT for Wednesday's (Oct. 30) game against the Cavs.
>   Chandler Hutchison  Chicago Bulls   Thu, Oct 31, 2019   Out (Hamstring) - The Bulls have listed Hutchison as OUT for Friday's (Nov. 1) game.
>   Daniel Theis    Boston Celtics  Thu, Oct 31, 2019   Day To Day (Left Ankle) - Boston Celtics F-C Daniel Theis is probable for Friday's (Nov. 1) game against New York right a left ankle sprain.
<   Edmond Sumner   Indiana Pacers  Wed, Oct 30, 2019   Day To Day (Hand) - Sumner is OUT for Wednesday's game (Oct. 30) against the Nets.
>   Edmond Sumner   Indiana Pacers  Thu, Oct 31, 2019   Out (Hand) - Sumner will miss at least three weeks with a non-displaced fracture in his right hand.
<   Elfrid Payton   New York Knicks Wed, Oct 30, 2019   Day To Day (Hamstring) - The Knicks have listed Payton Jr. as OUT for Wednesday's game (Oct. 30) against the Magic.
>   Elfrid Payton   New York Knicks Thu, Oct 31, 2019   Day To Day (Hamstring) - Payton is listed as out for Friday's (Nov. 1) game against Boston.
<   Enes Kanter Boston Celtics  Wed, Oct 30, 2019   Day To Day (Left Knee) - The Celtics have listed Kanter as OUT for Wednesday's game (Oct. 30) against the Bucks.
>   Enes Kanter Boston Celtics  Thu, Oct 31, 2019   Day To Day (Left Knee) - The Celtics have listed Kanter as OUT for Friday's game (Nov. 1) against New York.
<   Evan Turner Atlanta Hawks   Wed, Oct 30, 2019   Day To Day (Achilles) - Turner is listed as doubtful for Thursday's (Oct. 31) game.
>   Evan Turner Atlanta Hawks   Thu, Oct 31, 2019   Out (Achilles) - Turner is listed as OUT for Thursday's (Oct. 31) game.
>   Frank Ntilikina New York Knicks Thu, Oct 31, 2019   Day To Day (Neck) - Ntilikina is listed as probable for Friday's (Nov. 1) game.
<   Harry Giles Sacramento Kings    Wed, Oct 30, 2019   Day To Day (Knee) - Giles III was inactive for Wednesday's game (Oct. 30) against the Hornets.
>   Harry Giles Sacramento Kings    Thu, Oct 31, 2019   Day To Day (Knee) - Giles III is listed as questionable for Friday's (Nov. 1) game.
<   Jaylen Brown    Boston Celtics  Wed, Oct 30, 2019   Day To Day (Illness) - Brown is listed as OUT for Wednesday's (Oct. 30) game against the Bucks.
>   Jaylen Brown    Boston Celtics  Thu, Oct 31, 2019   Day To Day (Illness) - Brown is listed as questionable for Friday's (Nov. 1) game against New York.
>   Justise Winslow Miami Heat  Thu, Oct 31, 2019   Out (Lower Back) - Miami Heat F Justise Winslow is OUT for Thursday's (Oct. 31) game against Atlanta with lower back stiffness.
>   Kawhi Leonard   Los Angeles Clippers    Thu, Oct 31, 2019   Out (Load Management) - Los Angeles Clippers F Kawhi Leonard is out for Thursday's (Oct. 31) against San Antonio.
>   Kyle Korver Milwaukee Bucks Thu, Oct 31, 2019   Out (Rest) - Korver is listed as OUT for Friday's (Nov. 1) game.
>   Matthew Dellavedova Cleveland Cavaliers Thu, Oct 31, 2019   Out (Personal) - Dellavedova is listed as OUT for Friday's (Nov. 1) game.
<   Myles Turner    Indiana Pacers  Wed, Oct 30, 2019   Day To Day (Sprained Right Ankle) - Turner left Wednesday's (October 30) game with a sprained right ankle.
>   Myles Turner    Indiana Pacers  Thu, Oct 31, 2019   Out (Sprained Right Ankle) - Turner is week-to-week with a sprained right ankle.
<   Rajon Rondo Los Angeles Lakers  Tue, Oct 29, 2019   Day To Day (Calf) - Rondo did not play in Tuesday's game (Oct. 29) against the Grizzlies.
>   Rajon Rondo Los Angeles Lakers  Thu, Oct 31, 2019   Day To Day (Calf) - Rondo is listed as out for Friday's (Nov. 1) game against Dallas.
<   Sekou Doumbouya Detroit Pistons Wed, Oct 30, 2019   Day To Day (Concussion) - The Pistons have listed Doumbouya as OUT for Wednesday's game (Oct. 30) against the Raptors.
>   Sekou Doumbouya Detroit Pistons Thu, Oct 31, 2019   Out (Concussion) - The Pistons have listed Doumbouya as OUT for Friday's (Nov. 1) game.
>   Will Barton Denver Nuggets  Thu, Oct 31, 2019   Out (Left Toe) - Denver Nuggets G Will Barton is OUT for Thursday's (Oct. 31) game against New Orleans with left toe inflammation.

【问题讨论】:

  • 能否请您发布示例预期输出,以及您为解决自己的问题所做的努力。
  • 你的字段分隔符是什么?
  • \t(制表符)字符
  • 也许你想join 文件。 diff 很难解析,我认为并没有显示出你想要的意图。请显示第一个文件和第二个文件的摘录,然后显示您想要的示例预期输出,以显示您想要做什么。

标签: bash awk gnu uniq


【解决方案1】:

经过一番 awk 争论后,我想出了这个。 它计算第二个字段的所有出现次数,然后打印等于 1 的次数。

awk -F $'\t' '{count[$2]++} count[$2] == 1'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 2021-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多