将输入字段作为数组循环

Tyi*_*ilo 27 bash awk

有可能做这样的事情:

$ cat foo.txt
1 2 3 4
foo bar baz
hello world
$ awk '{ for(i in $){ print $[i]; } }' foo.txt
1
2
3
4
foo
bar
baz
hello
world
Run Code Online (Sandbox Code Playgroud)

我知道你可以这样做:

$ awk '{ split($0,array," "); for(i in array){ print array[i]; } }' foo.txt
2
3
4
1
bar
baz
foo
world
hello
Run Code Online (Sandbox Code Playgroud)

但结果却不合适.

Tyi*_*ilo 59

发现自己:

$ awk '{ for(i = 1; i <= NF; i++) { print $i; } }' foo.txt
Run Code Online (Sandbox Code Playgroud)