# Using printf (or other?) to create variable fixed width text

**URL:** <https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247>\
**Category:** Shell Programming and Scripting\
**Created:** [July 24, 2013, 4:00pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247 "2013-07-24T16:00:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![farrenthorpe](https://community.unix.com/letter_avatar/farrenthorpe/32/6_449c533f586939b6e5f246632a46421f.png) [@farrenthorpe](https://community.unix.com/u/farrenthorpe)\
**Post date:** [July 24, 2013, 4:00pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/1 "2013-07-24T16:00:29Z")

</div>

I would like to use printf (or something else?) to create a line of text that has varying column widths. This will be used to create a fixed width file (with varying column widths). For example, consider variables $1 $2 $3 are equal to a, b, c respectively and they should be printed in column positions 1, 5, 15 respectively so the output would be:  
a\_\_\_b\_\_\_\_\_\_\_\_\_c  
(ignore the underlines)

I was thinking I could use awk for this but it isn't clear to me how I could use awk without a file input (I just want to be able to pass text or variables).

Any ideas would be greatly appreciated... maybe there's a simple perl command, a combination of multiple print commands, or something else I should be using?

---

<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:** [July 24, 2013, 4:13pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/2 "2013-07-24T16:13:02Z")

</div>

Try regular shell:

```nohighlight
printf "%-4s%-10s%s\n" "$1" "$2" "$3"

```

---

<div class="post-metadata">

**Author:** ![farrenthorpe](https://community.unix.com/letter_avatar/farrenthorpe/32/6_449c533f586939b6e5f246632a46421f.png) [@farrenthorpe](https://community.unix.com/u/farrenthorpe)\
**Post date:** [July 24, 2013, 4:15pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/3 "2013-07-24T16:15:44Z")

</div>

I just figured it out just as you posted. Thanks!!!

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/6_449c533f586939b6e5f246632a46421f.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [July 24, 2013, 5:09pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/4 "2013-07-24T17:09:29Z")

</div>

Less negative "%s%4s%10s\n" ?

---

<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:** [July 24, 2013, 5:12pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/5 "2013-07-24T17:12:02Z")

</div>

Sorry, but that (post #2) won't yield a FIXED width file - which was part of the request - should the contents of `$3` vary. You need to specify the third column output width as well, then.

EDIT: same applies to DGPickett's proposal, should $1 vary in length.

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/6_449c533f586939b6e5f246632a46421f.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [July 24, 2013, 5:16pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/6 "2013-07-24T17:16:08Z")

</div>

I guess it depends on whether he wants it to spill over right or left.

```nohighlight
$ printf '%s%4s%10s\n' 1 2 3
1 2 3
$ printf '%s%4s%10s\n' 1 22 33
1 22 33
$ 

```

'%1.1s%4.4s%10.10s\n' truncates, freezes last column to pos ?  
'%-4.4s%-10.10s%s\n' truncates, freezes first column to pos ?

---

<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:** [July 24, 2013, 5:20pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/7 "2013-07-24T17:20:23Z")

</div>

@RudiC: OP will figure it out, it was just an example. The post was more about using shell's `printf` instead of an external utility. Besides what if a value is wider than the intended width?

---

<div class="post-metadata">

**Author:** ![farrenthorpe](https://community.unix.com/letter_avatar/farrenthorpe/32/6_449c533f586939b6e5f246632a46421f.png) [@farrenthorpe](https://community.unix.com/u/farrenthorpe)\
**Post date:** [July 24, 2013, 6:32pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/8 "2013-07-24T18:32:31Z")

</div>

I use cshell and for this task it writes out values to specific column positions and this is what I ended up with:

```nohighlight
setenv ST 06 #Position 1
setenv CTY 001 #Position 3
setenv FIREID CS000000001.1 #Position 6
setenv PTID 1 #Position 21
setenv STACKID 1 #Position 36
setenv SCC 2801500000 #Position 102
setenv LAT 33.19200012 #Position 231
setenv LON -93.7975012 #Position 240
printf "%-2s%-3s%-15s%-15s%-66s%-129s%-9.9s%-9.9s%s\n" $ST $CTY $FIREID $PTID $STACKID $SCC $LAT $LON

```

Although now I wonder if it is possible to align output to the right of the field rather than the left. For instance if ST is 6 (rather than 06), the first two spaces of output are "6 "rather than " 6". Is there a way to make it align right instead?

Thank you everyone for your very informative posts.

---

<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:** [July 24, 2013, 6:36pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/9 "2013-07-24T18:36:30Z")

</div>

Just leave out the "-" sign and a field will right align...

---

<div class="post-metadata">

**Author:** ![farrenthorpe](https://community.unix.com/letter_avatar/farrenthorpe/32/6_449c533f586939b6e5f246632a46421f.png) [@farrenthorpe](https://community.unix.com/u/farrenthorpe)\
**Post date:** [July 24, 2013, 6:39pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/10 "2013-07-24T18:39:44Z")

</div>

Hah! I wondered what that negative sign was doing... Thank you very much!!

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/6_449c533f586939b6e5f246632a46421f.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [July 25, 2013, 3:12pm UTC](https://community.unix.com/t/using-printf-or-other-to-create-variable-fixed-width-text/332247/11 "2013-07-25T15:12:06Z")

</div>

The printf thinks all strings are numbers, so everything is right jusified unless '-'.
