使用 Awk 命令行打印以逗号分隔的列

我有麻烦了。我必须使用 awk 打印文本文件中的一列。但是,列之间根本没有空格,只使用了一个逗号。看起来像这样:

column1,column2,column3,column4,column5,column6

如何打印出第三列使用 awk?

239424 次浏览

Try:

awk -F',' '{print $3}' myfile.txt

Here in -F you are saying to awk that use , as the field separator.

Try this awk

awk -F, '{$0=$3}1' file
column3
  • , Divide fields by ,
  • $0=$3 Set the line to only field 3
  • 1 Print all out. (explained here)

This could also be used:

awk -F, '{print $3}' file

A simple, although -less solution in :

while IFS=, read -r a a a b; do echo "$a"; done <inputfile

It works faster for small files (<100 lines) then as it uses less resources (avoids calling the expensive fork and execve system calls).

EDIT from Ed Morton (sorry for hi-jacking the answer, I don't know if there's a better way to address this):

To put to rest the myth that shell will run faster than awk for small files:

$ wc -l file
99 file


$ time while IFS=, read -r a a a b; do echo "$a"; done <file >/dev/null


real    0m0.016s
user    0m0.000s
sys     0m0.015s


$ time awk -F, '{print $3}' file >/dev/null


real    0m0.016s
user    0m0.000s
sys     0m0.015s

I expect if you get a REALY small enough file then you will see the shell script run in a fraction of a blink of an eye faster than the awk script but who cares?

And if you don't believe that it's harder to write robust shell scripts than awk scripts, look at this bug in the shell script you posted:

$ cat file
a,b,-e,d
$ cut -d, -f3 file
-e
$ awk -F, '{print $3}' file
-e
$ while IFS=, read -r a a a b; do echo "$a"; done <file


$

If your only requirement is to print the third field of every line, with each field delimited by a comma, you can use cut:

cut -d, -f3 file
  • -d, sets the delimiter to a comma
  • -f3 specifies that only the third field is to be printed

You can also use sed, for example:

echo '"A","B","C"'| sed -e 's/\([^,]*\),\([^,]*\),\([^,]*\)/\3 \2 \1/' will output "C" "B" "A"