仅当第二字段与字符串匹配时,如何获取文件的第一字段? [英] How to get 1st field of a file only when 2nd field matches a string?

查看:31
本文介绍了仅当第二字段与字符串匹配时,如何获取文件的第一字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当第二字段与给定字符串匹配时,如何获取文件的第一字段?

How to get 1st field of a file only when 2nd field matches a given string?

#cat temp.txt

#cat temp.txt

Ankit   pass
amit    pass
aman    fail
abhay   pass
asha    fail
ashu    fail


cat temp.txt | awk -F"\t" '$2 == "fail" { print $1 }'*

无输出

推荐答案

当我最后删除 * 时,您的代码似乎可以正常工作

Your code seems to work fine when I remove the * at the end

cat temp.txt | awk -F"\t" '$2 == "fail" { print $1 }'

要检查的另一件事是文件是否使用制表符或空格.我的数据文件复制/粘贴复制了空格,因此我需要这一行:

The other thing to check is if your file is using tab or spaces. My copy/paste of your data file copied spaces, so I needed this line:

cat temp.txt | awk '$2 == "fail" { print $1 }'

另一种方法是使用grep:

The other way of doing this is with grep:

cat temp.txt | grep fail$ | awk '{ print $1 }'

这篇关于仅当第二字段与字符串匹配时,如何获取文件的第一字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆