So here I have the following code:
#!/bin/bash
awk \'BEGIN {f=4} {printf($f\" \"); f=f-1}\'
Which will take input such as:
awk '!i{i=NF}{printf "%s ",$i;i--}'
EDIT: Since Ed has already exploited all the nice and proper awk
solutions I'll throw in a native bash
solution:
$ cat t.sh
#!/bin/bash
while read -ra numbers; do
: ${i:=${#numbers[@]}}
diag+=("${numbers[--i]}")
done < test.txt
echo "${diag[@]}"
.
$ ./t.sh
4 7 1 4
The problem is, NF
at BEGIN
section is not the number of tokens on the first line. The following works:
awk '{if (!f) f = NF; printf($f" "); f=f-1}'
Edit: as per suggestions in the comments, a cleaner and safer way is:
awk '{if (!f) f = NF; printf("%s ", $f); f--}'
$ awk '{print $(NF+1-NR)}' file
4
7
1
4
$ awk -v ORS=" " '{print $(NF+1-NR)}' file
4 7 1 4
or if you want to avoid adding a space to the end of your output line and to have a terminating newline:
$ awk '{printf "%s%s", (NR>1?FS:""), $(NF+1-NR)} END{print ""}' file
4 7 1 4