Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looping over input fields as array

Tags:

bash

awk

Is it possible to do something like this:

$ 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 

I know you could do this:

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

But then the result is not in order.

like image 294
Tyilo Avatar asked Aug 09 '11 14:08

Tyilo


1 Answers

Found out myself:

$ awk '{ for(i = 1; i <= NF; i++) { print $i; } }' foo.txt 
like image 199
Tyilo Avatar answered Oct 06 '22 12:10

Tyilo