用grep命令匹配特定列 [英] Match specific column with grep command

查看:3540
本文介绍了用grep命令匹配特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将特定列与grep命令匹配时遇到问题。我有一个像这样的测试文件(test.txt)。

I am having trouble matching specific column with grep command. I have a test file (test.txt) like this..

Bra001325       835     T       13      c$c$c$c$c$cccccCcc      !!!!!68886676
Bra001325       836     C       8       ,,,,,.,,        68886676
Bra001325       841     A       6       ,$,.,,. BJJJJE
Bra001325       866     C       2       ,.      HJ

我想提取所有包含 866 在第二列。当我使用 grep 命令时,我得到所有包含数字的行

And i want to extract all those lines which has a number 866 in the second column. When i use grep command i am getting all the lines that contains the number that number

grep "866" test.txt

Bra001325       835     T       13      c$c$c$c$c$cccccCcc      !!!!!68886676
Bra001325       836     C       8       ,,,,,.,,        68886676
Bra001325       866     C       2       ,.      HJ

如何使用grep命令匹配特定列?

How can i match specific column with grep command?

推荐答案

试试这样做:

Try doing this :

$ awk '$2 == 866' test.txt

不需要添加 {print} awk 的默认行为是打印在 true 条件。

No need to add {print}, the default behaviour of awk is to print on a true condition.

with grep

with grep :

$ grep -P '^\S+\s+866\b' *

但是 awk 可以打印文件名&比 grep 的问题显示得更加健壮:

But awk can print filenames too & is quite more robust than grep here :

$ awk '$2 == 866{print FILENAME":"$0; nextfile}' *

这篇关于用grep命令匹配特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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