Print multiple fields with awk

so its common knowledge one can print multiple fields with simple commands like this:

echo 12 44 45 552 24 | awk '{print $1,$4,$3}'

but suppose i want to avoid specifying the "$" symbol. is that possible? can something like this be done:

echo 12 44 45 552 24 | awk '{print $(1,4,3)}'

when i run the above, i get this:

awk: cmd. line:1: {print $(1,4,3)}
awk: cmd. line:1:           ^ syntax error
awk: cmd. line:1: {print $(1,4,3)}
awk: cmd. line:1:               ^ syntax error

So you want awk to be re-written because you don't want to follow the published syntax?

Is this perhaps because you want to pass in/deduce the fields you want as variables rather than hard coding them?

Please can you elaborate a bit more.

Thanks, in advance,
Robin

No, that cannot be done that way. What do you want to achieve? What do you mean with avoiding the "$" symbol?

Hi,

Not sure your intention.

I doubt it is possible to get fileds without $ in awk.

If you are sure with your input content, below experimental one works:

echo "12 44 45 552 24" | sed -e 's/^\([0-9]\{2\}\) \([0-9]\{2\}\) \([0-9]\{2\}\) \([0-9]\{2,3\}\) \([0-9]\{2\}\)/\1 \4 \3/'

Gives output:

so the fields to be grabbed from an output has to be provided by the user.

if i'm following the regular way of specifying the fields, it leaves no room for the users to specify the field.

for instance -

enter the fields you wish to grab: 1,3,4,8
read usersresponse

echo 4 2 45 48 25 98 100 87 372 731 93 | awk '{print $('${usersresponse}')}'

basically, im looking for the simplest way to get the fields provided by the user into a recognizable format to awk.

echo 12 44 45 552 24 | awk -vCOLS="1,4,3" '{for (i=1; i<=split(COLS,T,","); i++) printf "%s" OFS, $T; printf RS}'
12 552 45 

suppose i want to also be able to specify what field to grab, counting from the right hand side?

for instance, if i want to grab the value in the last column, i'll use NF. But suppose i want to grab the value in column that is 2 columns from the right:

echo 12 44 45 552 24 | awk -vCOLS="1,4,3,NF-2" '{for (i=1; i<=split(COLS,T,","); i++) printf "%s" OFS, $T; printf RS}'
12 552 45 
echo 12 44 45 552 24 | awk -vCOLS="1,4,3,-2" 'BEGIN {n=split(COLS,T,",")} {for (i=1; i<=n; i++) printf("%s%s%s", (i==1)?"":OFS, (T<0)?$(NF+T) : $T, (i==n)?ORS:"")}'

Or

echo 12 44 45 552 24 | awk -vCOLS="1,4,3,-2" 'BEGIN {n=split(COLS,T,",")} {for (i=1; i<=n; i++) printf "%s%s", $((NF+T)%NF), i==n?ORS:OFS}'
12 552 45 45

Edit: You can't specify $NF except using its absolute numerical value; 0 or -0 won't work in neither this nor vgersh99's proposal.

RudiC and I were working on similar solutions, but he posted first... The following code performs some range checking that RudiC's code does not attempt, and includes a little bit of discussion on why you can't reference the awk NF variable in another awk variable.

Variables contain strings or numbers; not indirect references to other variables. But, if you just use positive values to reference fields 1 up to NF inclusive and numbers less than 1 to represent fields NF down to 1 ( 0 representing NF ; -1 representing NF - 1 ; -2 representing NF - 2 ; etc.), you can use something like:

#!/bin/ksh
printf '%s\n' "12 44 45 252 24" "1 2 3 4 5 6 7 8 9 10" "A B C D E F G H I J" |
awk -v COLS="${1:-"-1,4,3"}" '
BEGIN {	npf = split(COLS, pf, ",")
}
{	for(i = 1; i <= npf; i++)
		printf("%s%s", (pf > NF || pf < 1 - NF) ? "OutOfRange" : \
		    (pf < 1) ? $(NF + pf) : $pf,
		    (i == npf) ? ORS : OFS)
}'

which, if invoked with no arguments prints the next to the last, the 4th, and the 3rd fields by default for each line read from the three lines printed by the printf command:

252 252 45
9 4 3
I D C

And, if invoked with the operand:
./tester -5,6,0,1
which prints the 5th from the last, the 6th, the last, and the 1st fields:

OutOfRange OutOfRange 24 12
5 6 10 1
E F J A

This was written and tested with a Korn shell, but will work with any shell that performs the standard variable expansions required by the POSIX standard shell.

If you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk .