1832973 Members
2236 Online
110048 Solutions
New Discussion

Using awk and variables

 
SOLVED
Go to solution
Manuales
Super Advisor

Using awk and variables

Hi all, could you tell me how can i put a variable into awk sentence?
I mean, below you can see the script and
rule says that i have to use a string for 9 filed:
ls -l | awk '$9 == "file.name" { sum += $5 } END { print sum }'

but in this case, i do not know the name, script will know that name through "for" command, but where it says "?????" i do not know how to put "x" variable and not the string ...

THIS IS THE SCRIPT

for x in `ls -1 /oracle/log/ | grep .trc`
do
echo ------------------ $x
ls -l | awk '$9 == ????? { sum += $5 } END { print sum }'
done

Please let me know ...
Thanks !!! :0)
15 REPLIES 15
A. Clay Stephenson
Acclaimed Contributor

Re: Using awk and variables

I will answer your specific question although I am all but certain you aren't asking the correct question. The key to what you are trying to do is the -v option of awk.

ls -l | awk -v "myvar=${x}" '$9 == myvar { sum += $5 } END { print sum }'
If it ain't broke, I can fix that.
Manuales
Super Advisor

Re: Using awk and variables

thanks for links ..

Clay .. i run script but it did not work :'(

this is the script:
for x in `ls -1 /oracle/log/ | grep .trc`
do
echo ------------------ $x
ls -l | awk -v "myvar=${x}" '$9 == myvar { sum += $5 } END { print sum }'
done



this is the output

------------------ ora_17362_p09.trc

------------------ ora_17385_p09.trc

------------------ ora_17402_p09.trc

------------------ ora_17419_p09.trc

------------------ ora_17450_p09.trc

------------------ ora_17961_p09.trc

------------------ ora_18221_p09.trc

------------------ ora_18233_p09.trc

------------------ ora_18244_p09.trc

------------------ ora_18291_p09.trc

------------------ ora_18297_p09.trc

------------------ ora_18722_p09.trc

------------------ ora_19000_p09.trc

------------------ ora_19018_p09.trc

------------------ ora_19030_p09.trc

------------------ ora_19110_p09.trc

------------------ ora_19126_p09.trc

------------------ ora_19511_p09.trc

i mean, it did not work because i can not see the size growing ...
how can i do that?

Thanks.
James R. Ferguson
Acclaimed Contributor

Re: Using awk and variables

Hi Manuales:

The '-v' option for passing variables is the ideal method. Another variation you may encounter is some scripts is seen with older 'awk' versions that insist that the external assignment is made as part of the argument list passed. Compare these cases:

# awk -v VAR="world" 'BEGIN{print "hello",VAR}' /dev/null

# awk -v VAR="world" 'END{print "hello",VAR}' /dev/null

# awk 'BEGIN{print "hello",VAR}' VAR="world" /dev/null

# awk 'END{print "hello",VAR}' VAR="world" /dev/null

Note the difference in output in the third form [where only the word "hello" is output]. The advanage to the '-v arg=value' form is that variable assignment occurs *before* the 'BEGIN' rules are executed. To enable the third case to offer "hello world" we would have to change the 'BEGIN' to an 'END'.

Regards!

...JRF...
Manuales
Super Advisor

Re: Using awk and variables

it do not work :'(
A. Clay Stephenson
Acclaimed Contributor

Re: Using awk and variables

As I said, you aren't asking the right question -- but you are getting closer to the right question.

I will give you a hint. Your sum is only summing one file within a single awk invocation and you need a single awk invocation with multiple lines of input.

This will be closer (but still needs a little work):

Create (or build on the fly an awk script), my.awk:
--------------------------------
BEGIN {
sum = 0
}
{
sum += ($5 + 0)
}
END {
print sum
}
--------------------------------

Now:
ls -l /oracle/log/*.trc | awk -f my.awk

Note: The ($5 + 0) seems dumb but it is not and is intentional.
If it ain't broke, I can fix that.
Manuales
Super Advisor

Re: Using awk and variables

Clay, you are right ..
script works as follows, but only shows the size of every file :( and i need to adding every size of each one ..

for x in `ls -1 /oracle/log/ | grep .trc`
do
ls -l /oracle/log/ | awk -v "myvar=${x}" '$9 == myvar { sum += $5 } END { print sum }'
done
------------------ ora_17362_p09.trc
21667
------------------ ora_17385_p09.trc
21667
------------------ ora_17402_p09.trc
21667

OK, I WILL DO THE FOLLOWING, but ... is possible to put the following code into the script, i mean , in a line?
--------------------------------
BEGIN {
sum = 0
}
{
sum += ($5 + 0)
}
END {
print sum
}
--------------------------------

Please , let me know ..
Thanks.
Manuales
Super Advisor

Re: Using awk and variables

okok ... Clay, it worked , but i'd like to know if that code ("BEGIN....") can be written into the main script, for having only one script.

Thanks.
Manuales.
James R. Ferguson
Acclaimed Contributor

Re: Using awk and variables

Hi Manuales:

# ls -l /oracle/log | awk '$9~/\.trc/ {print;sum+=$5} END {print sum}'

Notice how we let 'awk' match the fields to sum. You don't need to 'grep' first!

Regards!

...JRF...
Manuales
Super Advisor

Re: Using awk and variables

THANKS A LOT !!!!!!!!
AND if i do not want to be shown the files then only i added last command (grep -v ora, i know that files beging with ora word):

ls -l /oracle/log/ | awk '$9~/\.trc/ {print;sum+=$5} END {print sum}' | grep -v ora


THANKS TO ALL ...
AND .. do you know a manual or tutorial on line on internet for studying more awk commnad?

Thanks.
Manuales
Super Advisor

Re: Using awk and variables

a last question ..
how do you that:
ls -l /oracle/log/ | awk '$9~/\.trc/ {print;sum+=$5} END {print sum}' | grep -v ora

if instead of being files they were folders with files????? i mean, how do i put a command for being recursive?

Thanks ..
A. Clay Stephenson
Acclaimed Contributor

Re: Using awk and variables

Yes, it is possible. However, it is much easier to get awk working exactly as you want it to as a separate file before worrying about building it on the fly or quoting it within a script.

So take that approach and finally I'll show you one technique for building the awk script on the fly.

---------------------------------------------
#!/usr/bin/sh

build_A1()
{
echo "BEGIN {"
echo " sum = 0"
echo "}"
echo "{"
echo " print \$NF"
echo " sum += (\$5 + 0)"
echo "}"
echo "END {"
echo "print sum"
echo "}"
return 0
} # build_A1

typeset A1=$(build_A1)
awk "${A1}"
---------------------------------------
You then invoke it as:
ls -l /oracle/log/*.trc | my.sh


If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Using awk and variables

Hi (again) Manuales:

> do you know a manual or tutorial on line on internet for studying more awk commnad?

http://www.gnu.org/software/gawk/manual/

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Using awk and variables

Hi (again!) Manuales:

> if instead of being files they were folders with files????? i mean, how do i put a command for being recursive?

Simply use 'find' and pipe its output to 'awk':

# find /oracle/log -type f -name "*.trc" -exec ls -l {} \;|awk '{print;sum+=$5};END {print sum}'

Notice that we let 'find' do the matching of filenames and execute the 'ls'. The 'awk' portion does the summation, only, now.

Regards!

...JRF...

Manuales
Super Advisor

Re: Using awk and variables

You are right James ....

THANKS TO ALL!!!!!
THANKS FOR YOUR HELP !!!

Talk you later, in my next doubt .
:D