所以问题是:
使用 awk 显示文件中库存少于 100 件的所有行。数量是第二个字段。
我试过
awk '$2 < 100' inventory
但这仅在第二个字段中显示小于 100 的行,这是要求我显示所有行,我不知道如何显示,有人可以帮我吗?
草莓酱,300,4
覆盆子果酱,1216,7
蓝莓果酱,96,195
草莓果盘,49,621
覆盆子蜜饯,1937,624
蓝莓果盘,200,625
冷冻草莓,130,1941
草帽162047
^ 库存文件内容
这应该这样做:
awk 'BEGIN{FS=OFS=","} $2 < 100 {print}' inventory
说明::
awk
: 调用 awk 命令BEGIN{FS=OFS=","}'
:用作输入和输出的分隔符,因为我仍然记得您的其他问题,
中输入文件的格式$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
如果您只想打印所有行,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
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