# using AWK printf with userdefine variables

**URL:** <https://community.unix.com/t/using-awk-printf-with-userdefine-variables/227726>\
**Category:** Shell Programming and Scripting\
**Created:** [January 14, 2009, 4:40pm UTC](https://community.unix.com/t/using-awk-printf-with-userdefine-variables/227726 "2009-01-14T16:40:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmauer](https://community.unix.com/letter_avatar/danmauer/32/6_449c533f586939b6e5f246632a46421f.png) [@danmauer](https://community.unix.com/u/danmauer)\
**Post date:** [January 14, 2009, 4:40pm UTC](https://community.unix.com/t/using-awk-printf-with-userdefine-variables/227726/1 "2009-01-14T16:40:31Z")

</div>

seems simple but i've not been successfull in adding the value of a Variable to a every line of a file using AWK & printf  
i've come up with the following, but it's not working.:mad:

--- mydatafile.dat

e.g. of data in file:

cau\_actvty\_fa\_lrf.ksh  
fan\_soco\_fa.ksh  
ny\_sum\_lst.ksh  
etc....

myvar\_path=$PWD

awk -v PATH=$myvar\_path '{printf("%s\nPATH",$0)}' mydatafile.dat

output is to be: value of $PWD before each file name

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [January 14, 2009, 10:54pm UTC](https://community.unix.com/t/using-awk-printf-with-userdefine-variables/227726/2 "2009-01-14T22:54:11Z")

</div>

```nohighlight
awk -v PATH="$myvar_path" '{printf( PATH "%s\n",$0)}' mydatafile.dat

```

---

<div class="post-metadata">

**Author:** ![danmauer](https://community.unix.com/letter_avatar/danmauer/32/6_449c533f586939b6e5f246632a46421f.png) [@danmauer](https://community.unix.com/u/danmauer)\
**Post date:** [January 15, 2009, 1:04pm UTC](https://community.unix.com/t/using-awk-printf-with-userdefine-variables/227726/3 "2009-01-15T13:04:14Z")

</div>

thanks...

but how would i then put a / after the value of PATH which will be between the filename and it's path 😕 it doesn't seem that PATH gets a formating expression since it's comming before the formatting expressions.... bear with me as i'm still learning the nuances of awk...🙂

awk -v PATH="$myvar\_path" '{printf( PATH "%s\n",$0)}' mydatafile.dat

i figured out another way too, but a bit more involved

awk '{printf("%s/%s\n","'"${myvar\_path}"'",$1)}' mydatafile.dat

this way i was able to insert a / after the value of ${myvar\_path}

Thanks again for your time.

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [January 15, 2009, 4:34pm UTC](https://community.unix.com/t/using-awk-printf-with-userdefine-variables/227726/4 "2009-01-15T16:34:12Z")

</div>

> [@danmauer](#):
>
> thanks...
> 
> but how would i then put a / after the value of PATH which will be between the filename and it's path

Just put one there:

```plaintext
awk -v PATH="$myvar_path" '{printf( PATH "/%s\n",$0)}' mydatafile.dat

```

> [@](#):
>
> 😕 it doesn't seem that PATH gets a formating expression since it's comming before the formatting expressions....

PATH is part of the format string.

---

<div class="post-metadata">

**Author:** ![danmauer](https://community.unix.com/letter_avatar/danmauer/32/6_449c533f586939b6e5f246632a46421f.png) [@danmauer](https://community.unix.com/u/danmauer)\
**Post date:** [January 15, 2009, 4:37pm UTC](https://community.unix.com/t/using-awk-printf-with-userdefine-variables/227726/5 "2009-01-15T16:37:58Z")

</div>

Awesome.... i didn't realize you could put something before the %s

Thanks again, little by little , my little understanding is getting bigger.

thanks.
