简体   繁体   English

为什么这个 awk 命令不起作用

[英]Why isn't this awk command working

So the question is:所以问题是:

Display all of the lines in the file showing only the quantity and product name, in that order, using awk.使用 awk 按顺序显示文件中仅显示数量和产品名称的所有行。 Product name is the first field and quantity is the second field.产品名称是第一个字段,数量是第二个字段。 The data file is "inventory"数据文件是“库存”

The contents to 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

My command was我的命令是

awk '$2 $1' inventory

That should work right?那应该行得通吗? I also tried like我也试过

awk '{print $2 $1}' inventory
awk '$2, $1' inventory

and a lot of variations of that, but none works!以及很多变体,但没有一个有效! Can anyone help figure out why?任何人都可以帮助找出原因吗?

That is a csv (comma seperated values) file so the field separator needs to be specified as , using the -F option.这是一个csv (逗号分隔值)文件,使field separator中指定的需求为,使用-F选项。

$ awk -F, '{print $2,$1}' inventory

300 Strawberry Jam
1216 Raspberry Jam
96 Blueberry Jam
49 Strawberry Compote
1937 Raspberry Compote
200 Blueberry Compote
130 Frozen Strawberries
16 Straw Hats

开始了:

awk 'BEGIN{FS=OFS=","} {print $2, $1}' inventory

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM