# Assigning a specific format to a specific column in a text file using awk and printf

**URL:** <https://community.unix.com/t/assigning-a-specific-format-to-a-specific-column-in-a-text-file-using-awk-and-printf/264868>\
**Category:** Shell Programming and Scripting\
**Created:** [April 29, 2010, 3:56pm UTC](https://community.unix.com/t/assigning-a-specific-format-to-a-specific-column-in-a-text-file-using-awk-and-printf/264868 "2010-04-29T15:56:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![goodbenito](https://community.unix.com/letter_avatar/goodbenito/32/5_5575768a8748004e209b776fc1b2916d.png) [@goodbenito](https://community.unix.com/u/goodbenito)\
**Post date:** [April 29, 2010, 3:56pm UTC](https://community.unix.com/t/assigning-a-specific-format-to-a-specific-column-in-a-text-file-using-awk-and-printf/264868/1 "2010-04-29T15:56:46Z")

</div>

Hi,

I have the following text file:

8 T1mapping\_flip02 ok 128 108 30 1 665000-000008-000001.dcm  
9 T1mapping\_flip05 ok 128 108 30 1 665000-000009-000001.dcm  
10 T1mapping\_flip10 ok 128 108 30 1 665000-000010-000001.dcm  
11 T1mapping\_flip15 ok 128 108 30 1 665000-000011-000001.dcm  
12 T1mapping\_flip30 ok 128 108 30 1 665000-000012-000001.dcm  
15 T1mapping\_flip02 ok 128 108 30 1 665000-000015-000001.dcm  
16 T1mapping\_flip05 ok 128 108 30 1 665000-000016-000001.dcm  
17 T1mapping\_flip10 ok 128 108 30 1 665000-000017-000001.dcm  
18 T1mapping\_flip15 ok 128 108 30 1 665000-000018-000001.dcm  
19 T1mapping\_flip30 ok 128 108 30 1 665000-000019-000001.dcm

Using the following code, I've managed to get this far:

```nohighlight
while read line ; 
do echo $line | awk '{print $2 "/" $1 "/" $8}' ; 
done < file

```

...to produce this output:

T1mapping\_flip02/8/665000-000008-000001.dcm  
T1mapping\_flip05/9/665000-000009-000001.dcm  
T1mapping\_flip10/10/665000-000010-000001.dcm  
T1mapping\_flip15/11/665000-000011-000001.dcm  
T1mapping\_flip30/12/665000-000012-000001.dcm  
T1mapping\_flip02/15/665000-000015-000001.dcm  
T1mapping\_flip05/16/665000-000016-000001.dcm  
T1mapping\_flip10/17/665000-000017-000001.dcm  
T1mapping\_flip15/18/665000-000018-000001.dcm  
T1mapping\_flip30/19/665000-000019-000001.dcm

The problem is, I would like the middle number (field #3) to be a 3-digit number, e.g. 008, 009, 010...019.

I've tried this:

```nohighlight
while read line ; 
do echo $line | awk '{print $2 "/" {printf "%03d\n" $1} "/" $8}' ; 
done < file

```

...but obviously I don't know what I'm doing because I get a big ugly syntax error.

Any help would be greatly appreciated as always.  
Thanks!

---

<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:** [April 29, 2010, 4:17pm UTC](https://community.unix.com/t/assigning-a-specific-format-to-a-specific-column-in-a-text-file-using-awk-and-printf/264868/2 "2010-04-29T16:17:02Z")

</div>

```nohighlight
awk '{printf "%s/%03d/%s\n",$2,$1,$8 } ' file

```

---

<div class="post-metadata">

**Author:** ![goodbenito](https://community.unix.com/letter_avatar/goodbenito/32/5_5575768a8748004e209b776fc1b2916d.png) [@goodbenito](https://community.unix.com/u/goodbenito)\
**Post date:** [April 30, 2010, 9:25am UTC](https://community.unix.com/t/assigning-a-specific-format-to-a-specific-column-in-a-text-file-using-awk-and-printf/264868/4 "2010-04-30T09:25:34Z")

</div>

That was a perfect solution. Thanks!
