所以问题是:
使用awk显示库存中少于100个商品的文件中的所有行。数量是第二个字段。
我试过
awk '$2 < 100' inventory
但是只显示第二个字段中小于100的行,这是要求我显示所有行,我不知道如何显示,有人可以帮助我吗?
Raspberry Jam,1216,7
蓝莓果酱,96,195
Strawberry Compote,49,621
Raspberry Compote,1937,624
蓝莓蜜饯,200,625
冷冻草莓,130,1941
草帽,16,2047^库存文件内容
答案 0 :(得分:0)
这应该这样做:
awk 'BEGIN{FS=OFS=","} $2 < 100 {print}' inventory
<强>解释强>:
awk
:调用awk命令BEGIN{FS=OFS=","}'
:使用,
作为输入和输出的分隔符,因为我还记得your other question $2 < 100 {print}
:只要条件$2 < 100
为真,就打印整行。inventory
:输入文件的名称。<强>输入:强>
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
<强>输出:强>
Blueberry Jam,96,195
Strawberry Compote,49,621
Straw Hats,16,2047
答案 1 :(得分:0)
如果您只想用awk
打印所有行,那么:
awk '{print $0}'
如果要在第二个字段少于100个字符的位置打印行:
$ cat inventory
1,2,3
1,12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,0
像这样:
`awk -F, '$2 < 100 {print $0}' inventory`
1,2,3
如果要根据第二个字段的值打印行:
$ awk -F, '$2<100' file
Blueberry Jam,96,195
Strawberry Compote,49,621
Straw Hats,16,2047
$ awk -F, '$2>100' file
Strawberry Jam,300,4
Raspberry Jam,1216,7
Raspberry Compote,1937,624
Blueberry Compote,200,625
Frozen Strawberries,130,1941
答案 2 :(得分:0)
awk -F, '$2<100' file
测试
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, '$2<100' test
Blueberry Jam,96,195
Strawberry Compote,49,621