# Print multiple fields with awk

**URL:** <https://community.unix.com/t/print-multiple-fields-with-awk/363443>\
**Category:** Shell Programming and Scripting\
**Created:** [November 8, 2016, 11:08am UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443 "2016-11-08T11:08:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [November 8, 2016, 11:08am UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/1 "2016-11-08T11:08:43Z")

</div>

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

```nohighlight
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:

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

```

when i run the above, i get this:

```nohighlight
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

```

---

<div class="post-metadata">

**Author:** ![rbatte1](https://community.unix.com/letter_avatar/rbatte1/32/6_449c533f586939b6e5f246632a46421f.png) [@rbatte1](https://community.unix.com/u/rbatte1)\
**Post date:** [November 8, 2016, 11:34am UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/2 "2016-11-08T11:34:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [November 8, 2016, 11:35am UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/3 "2016-11-08T11:35:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![greet\_sed](https://community.unix.com/letter_avatar/greet_sed/32/6_449c533f586939b6e5f246632a46421f.png) [@greet\_sed](https://community.unix.com/u/greet_sed)\
**Post date:** [November 8, 2016, 11:40am UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/4 "2016-11-08T11:40:08Z")

</div>

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:

```plaintext
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:

> [@](#):
>
> 12 552 45

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [November 8, 2016, 12:05pm UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/5 "2016-11-08T12:05:59Z")

</div>

> [@scrutinizer](#):
>
> No, that cannot be done that way. What do you want to achieve? What do you mean with avoiding the "$" symbol?

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 -

```plaintext
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.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 8, 2016, 12:13pm UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/6 "2016-11-08T12:13:38Z")

</div>

```nohighlight
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 

```

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [November 8, 2016, 12:31pm UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/7 "2016-11-08T12:31:21Z")

</div>

> [@rudic](#):
>
> ```plaintext
> 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:

```plaintext
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 

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [November 8, 2016, 12:46pm UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/8 "2016-11-08T12:46:02Z")

</div>

> [@skysmart](#):
>
> 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:
> 
> ```plaintext
> 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 
> 
> ```

```plaintext
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:"")}'

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [November 8, 2016, 1:08pm UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/9 "2016-11-08T13:08:14Z")

</div>

Or

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [November 8, 2016, 2:06pm UTC](https://community.unix.com/t/print-multiple-fields-with-awk/363443/10 "2016-11-08T14:06:27Z")

</div>

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:

```nohighlight
#!/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:

```nohighlight
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:

```nohighlight
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` .
