Linux 如何grep,然后从分隔的列文件中剪切?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15129896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to grep, then cut from a delimited column file?
提问by Andrew Tsay
I have a file with multiple rows, each row delimited with |
into multiple columns.
I can grep for a certain row, and I can cut for a certain column, but I can't figure out how to do both.
我有一个多行的文件,每行分隔|
成多列。我可以对某一行进行 grep,也可以对某一列进行剪切,但我不知道如何同时进行。
grep '^1001' customer
grabs rows starting with 1001, from a file named customer
从名为 customer 的文件中抓取以 1001 开头的行
cut -d "|" -f 3 customer
cuts column 3 from all the rows in customer file.
从客户文件中的所有行中剪切第 3 列。
So....
所以....
grep '^1001' customer | cut -d "|" -f 3 customer
采纳答案by NPE
Simply omit the filename when you call cut
and it'll use the output of grep
as its input:
调用时只需省略文件名cut
,它将使用的输出grep
作为其输入:
grep 1001 customer | cut -d "|" -f 3
It is also worth noting that grep 1001
doesn't grab rows starting with 1001; it grabs rows containing1001.
还值得注意的是,grep 1001
不会抓取以 1001 开头的行;它抓取包含1001 的行。
回答by anubhava
Better use ask for this task like this and avoid multiple commands with pipe:
更好地使用这样的任务请求并避免使用管道的多个命令:
awk -F "|" '==1001{print }' customer