awk +一行 awk 语法,如果为“true”,则仅打印第二个字段一次单词匹配

发布于 2024-09-11 10:34:46 字数 722 浏览 1 评论 0原文

我不知道如何用 awk 做到这一点 但我的目标是创建 awk 一行语法,以便打印第二个字段 ($2) if 所有第一个字段($1)都是 true

例如

我有文件:

   true my_name_is_lenon
   true my_name_is_lenon
   true my_name_is_lenon
   false my_name_is_lenon
   true my_name_is_lenon

   false my_dog_is_fat
   true my_dog_is_fat
   true my_dog_is_fat

   true I_am_very_tall
   true I_am_very_tall
   true I_am_very_tall

   true my_ball_is_fine
   true my_ball_is_fine

awk 将仅打印以下内容:

 I_am_very_tall
 my_ball_is_fine

因为 I_am_very_tall , my_ball_is_fine 在第一个字段上为 true 而没有 false

my_dog_is_fat , my_name_is_lenon 未打印,因为第一个字段上的错误单词

规则是如果第一个字段上没有错误的单词,则打印第二个字段! (第二个字段上的所有同一句话)

lidia

I don't know how to do this with awk
But my target is to create awk one line syntax in order to print the second field ($2) if
all first field ($1) are true

for example

I have the file:

   true my_name_is_lenon
   true my_name_is_lenon
   true my_name_is_lenon
   false my_name_is_lenon
   true my_name_is_lenon

   false my_dog_is_fat
   true my_dog_is_fat
   true my_dog_is_fat

   true I_am_very_tall
   true I_am_very_tall
   true I_am_very_tall

   true my_ball_is_fine
   true my_ball_is_fine

the awk will print only the following:

 I_am_very_tall
 my_ball_is_fine

Because I_am_very_tall , my_ball_is_fine get true on the first field without false

my_dog_is_fat , my_name_is_lenon not printed because the false word on the first field

The rule is to print the second field if no false word on the first field! (Of all the same sentence on the second field)

lidia

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

好多鱼好多余 2024-09-18 10:34:46

这是一种方法:

awk '{if ($1 == "false") {array[$2] = $1} else if (array[$2] != "false") array[$2] = $1} END {for (i in array) if (array[i] == "true") print i}' inputfile

编辑:

这里是多行:

awk '{if ($1 == "false") 
         {array[$2] = $1} 
     else if (array[$2] != "false") 
         array[$2] = $1} 
     END {for (i in array)
             if (array[i] == "true") 
                 print i}
' inputfile

Here is one way:

awk '{if ($1 == "false") {array[$2] = $1} else if (array[$2] != "false") array[$2] = $1} END {for (i in array) if (array[i] == "true") print i}' inputfile

Edit:

Here it is on multiple lines:

awk '{if ($1 == "false") 
         {array[$2] = $1} 
     else if (array[$2] != "false") 
         array[$2] = $1} 
     END {for (i in array)
             if (array[i] == "true") 
                 print i}
' inputfile
聚集的泪 2024-09-18 10:34:46

假设每个块属于同一类别。

$ awk -vRS= '!/false/' file | uniq | awk '{print $NF}'
I_am_very_tall
my_ball_is_fine

Assuming each block is of the same category.

$ awk -vRS= '!/false/' file | uniq | awk '{print $NF}'
I_am_very_tall
my_ball_is_fine
过期以后 2024-09-18 10:34:46

我不确定它是否可以作为 awk 的单行代码。至少不容易。 awk 是绝对必需的吗?因为如果 awk 只是解决方案的一部分,则可以使用单行 shell 解决方案。

例如,这是一个 shell 单行(只是很长的一行):

awk '{ print $2 }' < {infile} | while read line; do grep -i "false $line" {infile} > /dev/null || echo $line; done | uniq

它效率不高,但它确实可以完成工作。

相比之下,我得到的最短的 awk 解决方案大约有 25 行(如果您愿意,我可以将其放在此处 - 留下评论,我将相应地更新它)。但这意味着您可以将其保存到文件中并执行 awk -f alltrue.awk {infile}。我不确定这是否严格归类为 Perl 单行代码:-)

更好地理解您最终想要实现的目标或它的用途可能会很有用。

I'm not sure it is possible as an awk one-liner. At least not easily. Is awk absolutely required? As there are one line shell solutions available if awk is just part of the solution.

For example, this is a shell one-liner (just a long line):

awk '{ print $2 }' < {infile} | while read line; do grep -i "false $line" {infile} > /dev/null || echo $line; done | uniq

It's not efficient, but it does do the job.

In comparison, the shortest awk solution I've got is about 25 lines (which I can drop here in here if you want -- leave a comment and I'll update this accordingly). But this would then mean you could save this to a file and execute awk -f alltrue.awk < {infile}. I'm not sure this strictly classes as a perl one-liner though :-)

A better understanding of what you're ultimately trying to achieve or what this is for might be useful.

地狱即天堂 2024-09-18 10:34:46

另一个:

awk 'END { 
  for (A in all)  
    if (!(A in ko)) print A
  }
NF { all[$2] } 
$1 == "false" { ko[$2] }
' infile

如果您想保留原始顺序,则需要更多代码。

And another one:

awk 'END { 
  for (A in all)  
    if (!(A in ko)) print A
  }
NF { all[$2] } 
$1 == "false" { ko[$2] }
' infile

If you want to preserve the original order, you'll need more code though.

一页 2024-09-18 10:34:46

另一个紧凑的解决方案。

awk '
  $1 == "true" && ! ($2 in false) {true[$2]}
  $1 == "false" {false[$2]; delete true[$2]}
  END {for (word in true) print word}
' true.txt

Another compact solution.

awk '
  $1 == "true" && ! ($2 in false) {true[$2]}
  $1 == "false" {false[$2]; delete true[$2]}
  END {for (word in true) print word}
' true.txt
柒夜笙歌凉 2024-09-18 10:34:46

这是另一种方法:

gawk '{a[$2]=a[$2] || $1=="false"} END {for (i in a) if (!a[i] && i) print i}' a.txt

my_ball_is_fine
I_am_very_tall

Here is another way:

gawk '{a[$2]=a[$2] || $1=="false"} END {for (i in a) if (!a[i] && i) print i}' a.txt

my_ball_is_fine
I_am_very_tall
假装不在乎 2024-09-18 10:34:46

这是一个衬里:

awk '{$1=="false" ? false[$2]="false" : true[$2]="true"} END{for (i in true) if   (!false[i]) {print i} }'

my_ball_is_fine
我非常高

Here is a one liner:

awk '{$1=="false" ? false[$2]="false" : true[$2]="true"} END{for (i in true) if   (!false[i]) {print i} }'

my_ball_is_fine
I_am_very_tall

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文