# awk script help

**URL:** <https://community.unix.com/t/awk-script-help/350071>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [February 2, 2015, 2:04am UTC](https://community.unix.com/t/awk-script-help/350071 "2015-02-02T02:04:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Indra2011](https://community.unix.com/letter_avatar/indra2011/32/6_449c533f586939b6e5f246632a46421f.png) [@Indra2011](https://community.unix.com/u/Indra2011)\
**Post date:** [February 2, 2015, 2:04am UTC](https://community.unix.com/t/awk-script-help/350071/1 "2015-02-02T02:04:21Z")

</div>

Dear Help,

I have an i/p file which looks like :

```nohighlight
1 24 980
10 97 345
100 67 345
2 945 325
20 8 234

```

Is there a way to print it like, below format

```nohighlight
  1 24 980
 10 97 345
100 67 345
  2 945 325
 20 8 234

```

Thx

---

<div class="post-metadata">

**Author:** ![anbu23](https://community.unix.com/user_avatar/community.unix.com/anbu23/32/351_2.png) [@anbu23](https://community.unix.com/u/anbu23)\
**Post date:** [February 2, 2015, 2:15am UTC](https://community.unix.com/t/awk-script-help/350071/2 "2015-02-02T02:15:40Z")

</div>

Check format specifiers in printf()

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [February 2, 2015, 2:41am UTC](https://community.unix.com/t/awk-script-help/350071/3 "2015-02-02T02:41:33Z")

</div>

Hello Indra2011,

Could you please try following and let me know if this helps.  
This will only work for 3 fields.

```nohighlight
awk 'FNR==NR{A=length($1)>A?length($1):A;D=length($2)>D?length($2):D;C=length($3)>C?length($3):C;next} {printf "%"A"d %"D"d %d\n",$1, $2, $3}' Input_file Input_file

```

Output will be as follows.

```nohighlight
  1 24 980
 10 97 345
100 67 345
  2 9845 325
 20 8 234

```

Thanks,  
R. Singh

---

<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:** [February 2, 2015, 3:45am UTC](https://community.unix.com/t/awk-script-help/350071/4 "2015-02-02T03:45:46Z")

</div>

As anbu23 said, `printf` (builtin in recent shells) will do what you want:

```nohighlight
while read A B C; do printf "%3d %3d %3d\n" $A $B $C; done < file
  1 24 980
 10 97 345
100 67 345
  2 945 325
 20 8 234

```
