bash 如何使用 awk 显示所有行,但在第二个字段中有一个条件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13574227/
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 do I use awk to display all lines but with a criteria in the 2nd field?
提问by albert
So the question is:
所以问题是:
Display all of the lines in the file with less than 100 items in inventory, using awk. Quantity is the 2nd field.
使用 awk 显示文件中库存少于 100 件的所有行。数量是第二个字段。
I tried
我试过
 awk ' < 100' inventory
but that ONLY shows the lines less than 100 in the 2nd field, this is asking me to show all the lines, and i am not sure how to show that, can someone help me out?
但这仅在第二个字段中显示少于 100 行,这是要求我显示所有行,我不知道如何显示,有人可以帮助我吗?
Strawberry Jam,300,4
草莓酱,300,4
Raspberry Jam,1216,7
覆盆子果酱,1216,7
Blueberry Jam,96,195
蓝莓果酱,96,195
Strawberry Compote,49,621
草莓果盘,49,621
Raspberry Compote,1937,624
覆盆子果盘,1937,624
Blueberry Compote,200,625
蓝莓果盘,200,625
Frozen Strawberries,130,1941
冷冻草莓,130,1941
Straw Hats,16,2047
草帽,16,2047
^ inventory file contents
^ 库存文件内容
回答by sampson-chen
This should do it:
这应该这样做:
awk 'BEGIN{FS=OFS=","}  < 100 {print}' inventory
Explanation::
解释::
- awk: invoke the awk command
- BEGIN{FS=OFS=","}': use- ,as delimiter for both input and output, because I still remember the format for your input file from your other question
- $2 < 100 {print}: whenever the condition- $2 < 100is true, print the entire line.
- inventory: name of your input file.
- awk: 调用 awk 命令
- BEGIN{FS=OFS=","}':- ,用作输入和输出的分隔符,因为我还记得你的另一个问题中输入文件的格式
- $2 < 100 {print}: 只要条件- $2 < 100为真,就打印整行。
- inventory: 输入文件的名称。
Input:
输入:
Strawberry Jam,300,4
Raspberry Jam,1216,7
Blueberry Jam,96,195
Strawberry Compote,49,621
Raspberry Compote,1937,624
Blueberry Compote,200,625
Frozen Strawberries,130,1941
Straw Hats,16,2047
Output:
输出:
Blueberry Jam,96,195
Strawberry Compote,49,621
Straw Hats,16,2047
回答by Chris Seymour
If you just want to print all the lines with awkthen:
如果您只想打印所有行,awk则:
awk '{print $ cat inventory
1,2,3
1,12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,0
}'
If you want to print line where-by the second field is less than 100 characters:
如果要打印第二个字段少于 100 个字符的行:
`awk -F, ' < 100 {print $ awk -F, '<100' file
Blueberry Jam,96,195
Strawberry Compote,49,621
Straw Hats,16,2047
$ awk -F, '>100' file
Strawberry Jam,300,4
Raspberry Jam,1216,7
Raspberry Compote,1937,624
Blueberry Compote,200,625
Frozen Strawberries,130,1941
}' inventory`
1,2,3
Like this:
像这样:
awk -F, '<100' file
If you want to print line the lines base on the value of the second field:
如果要根据第二个字段的值打印行:
kent$  cat test
Raspberry Jam,1216,7
Blueberry Jam,96,195
Strawberry Compote,49,621
Raspberry Compote,1937,624
Blueberry Compote,200,625
Frozen Strawberries,130,1941
kent$  awk -F, '<100' test
Blueberry Jam,96,195
Strawberry Compote,49,621
回答by Kent
test
测试
##代码##
