sha1sum
December 21st, 2013, 06:33 PM
Hello guys, I need help with this awk command.
In the book sed & awk by O'reilly press, 1997, it says that the syntax of the for command is like this:
for ( set; test; increment)
action
So I tried this
me@computer:~$ echo "Alice Bob Charlie Daniel Erica" | awk '{ for ( i = 1; i <= NF-2 ; i++ ); print $i }'
Daniel
What I want to have the awk command print all the fields except the last two. But instead it only prints the penultimate field.
If I simplify it even more, I see this behavior:
me@computer:~$ echo "Alice Bob Charlie Daniel Erica" | awk '{ for ( i = 1; i < 4 ; i++ ); print $i }'
Daniel
I've did some testing and what it appears to do is the following: It works through the loop and it prints the first value of i for which the test fails to give true, after which it exits.
Why does it behave like that?
In the book sed & awk by O'reilly press, 1997, it says that the syntax of the for command is like this:
for ( set; test; increment)
action
So I tried this
me@computer:~$ echo "Alice Bob Charlie Daniel Erica" | awk '{ for ( i = 1; i <= NF-2 ; i++ ); print $i }'
Daniel
What I want to have the awk command print all the fields except the last two. But instead it only prints the penultimate field.
If I simplify it even more, I see this behavior:
me@computer:~$ echo "Alice Bob Charlie Daniel Erica" | awk '{ for ( i = 1; i < 4 ; i++ ); print $i }'
Daniel
I've did some testing and what it appears to do is the following: It works through the loop and it prints the first value of i for which the test fails to give true, after which it exits.
Why does it behave like that?