Using variable inside awk

Hi,

Please help me how to use variables inside awk in code below:

ll | awk -v "yr=`date '+%Y'`" -v "mnth=`date '+%m'`" -v Jan=1 -v Feb=2 -v Mar=3 -v Apr=4 -v May=5 -v Jun=6 -v Jul=7 -v Aug=8 ' !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s,", $9,$1,$5,$`$6`,$7} }'

Thanks.

You already appear to be doing so, but not doing anything with the variables you set. What are you actually trying to do?

Manubatham, per the awk man page, the variables that you supply with the -v arg, are available in the BEGIN section only:

Here is how you can use it:

awk -v "Jan=1" 'BEGIN { print Jan }'

This will produce the output - 1, which is the value of the variable Jan.

Hi Corona,

Here you can see that $6 will return the month... that will be in format of Jan, Feb, Mar, or ... Now I want to convert month to numeric. Like 1 for Jan, 2 for Feb, 3 for Mar...so on.

Regards,
Manu

passing -v Jan=1 makes the variable named Jan contain "1". You don't get the string "Jan" anywhere.

How about:

awk 'BEGIN { split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", MON, "|"); }
{ (rest of your code goes here) }'

That gives you an array MON where MON[1] gets you "Jan", MON[2] gets you "Feb", MON[$6] gets you the name of whatever number $6 is, and so forth.

[edit] Got it backwards. Hmm, thinking on it.

Am I using $Aug in correct way?

ll | awk -v "yr=`date '+%Y'`" -v "mnth=`date '+%m'`" -v Jan=1 -v Feb=2 -v Mar=3 -v Apr=4 -v May=5 -v Jun=6 -v Jul=7 -v Aug=8 ' !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s\n", $9,$1,$5,$Aug,$7} }'

Regards,
Manu

awk 'BEGIN { split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", MON, "|"); for(N=1; N<=12; N++) I[MON[N]]=N; }
{ (rest of your code goes here) }'

Now you have an array MON where MON[1]="Jan", and an array I where I["Jan"]=1.

---------- Post updated at 12:11 PM ---------- Previous update was at 12:10 PM ----------

You've already been told "no". $Aug will pick a different field depending on what number Aug is. Variables don't work that way in awk. print Aug; will print 8. Print $Aug will print field number 8, whatever it is. I don't think you can solve your problem with -v since you can't go from a number to a variable name.

You can use a number as an array index, which is what I have done. Try my above solution, which builds arrays out of a string of months.

Trying your solution, but $Aug always comes 8, I will never ask that question, see the output below:

ll | awk -v "yr=`date '+%Y'`" -v "mnth=`date '+%m'`" -v Jan=1 -v Feb=2 -v Mar=3 -v Apr=4 -v May=5 -v Jun=6 -v Jul=7 -v Aug=8 ' !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s\n", $9,$1,$5,$Aug,$7} }'

a.out                                   -rw-------          0           06:55 5
dir_str.txt                             -rw-------         35           10:23 19
dont_make_test                          ----------          0           10:03 23
get_dir_stru.sh                         -rwx------        599           11:02 5
prod_test                               -rwx------        521           13:27 23

Thanks,
Manu

$Aug will not come up 8. Aug will come up 8.

The -v solution will never work because variables don't work that way. You can't feed awk 8 and have awk decide "oh, there's a variable named Aug holding 8, you must mean it". It doesn't search for other variables with the same contents.

I may have stated it backwards, I["Jan"] will be 1 and MON[1] will be "Jan". Otherwise my solution does what you want.

1 Like

Hi Corona,

My current problem is related to the post

http://www.unix.com/shell-programming-scripting/165871-listing-files-particular-format-2.html

From there you can understand why I need this... and I am not getting how to implement your code in my solution. I have used $6 for month in mon format, but I want to convert it into numeric.

Thanks.

---------- Post updated at 01:15 AM ---------- Previous update was at 12:57 AM ----------

Thanks man.

Just clicked what you want to tell.

I have made my code like below--

!#/bin/sh
ll | awk -v "yr=`date '+%Y'`" -v "mnth=`date '+%m'`" 'BEGIN { split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", MON, "|"); for(N=1; N<=12; N++) I[MON[N]]=N; } !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s,", $9,$1,$5,$6,$7}; if(index($8,":")==3) { if(I[$6]>mnth) { print yr-1 } else { print yr }} else { print $8 } }'

Do you want to suggest any change in the code above?

Thanks a lot. :slight_smile:

The skills involved are 'copy' and 'paste'.

awk 'BEGIN { split("Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec", MON, "|"); for(N=1; N<=12; N++) I[MON[N]]=N; }
 !/^d/ { if(NR>1) {printf "%-29s\t\t%s\t%5s\t\t%s %s\n", $9,$1,$5,I[$6],$7} }'

I don't know what you're trying to do with the code you crossposted.