Operating System - HP-UX
1821537 Members
2307 Online
109633 Solutions
New Discussion юеВ

sending parameter to awk command (urgent!!!)

 
SOLVED
Go to solution

sending parameter to awk command (urgent!!!)

Hello,

For example I have a command like

du -x .|awk '{ if($1 > 10) {print}}' as we mentioned the day before.

this command finds all the directories which are largen than 10kb.

I need to change the size everytime I run the program.

for example I run the program as
./program -D 20

-D stands for the program searches for only directories. I assign this to $1. 20 stands for the size and I assign it to $2.

now, how can I write the du..|awk.. command?
I tried
du -x .|awk '{ if($1 > $2) {print}}'
and
du -x .|awk '{ if($3 > $2) {print}}'

but it didn't work. (results were not correct) Can anyone help?
11 REPLIES 11
Court Campbell
Honored Contributor

Re: sending parameter to awk command (urgent!!!)

I am not a huge awk monger but i can offer you a different route. You could use the find command.

Example:

#find . -type d -size +500c

this will find print any directory that is greater than 500 bytes. This way you could pass a size in bytes to the script and pass it to the find command. Or find a tool thaat will do size conversions to bytes and then pass that to the find command.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Karsten L├╢perick
Valued Contributor
Solution

Re: sending parameter to awk command (urgent!!!)

Hi Osman,

write this:
set variable = "your choice"
du -x .|awk '{ if($1 > '$variable') {print}}'
Important are the quotings of $variable.

Greetings

Karsten
Nothing is impossible
Sundar_7
Honored Contributor

Re: sending parameter to awk command (urgent!!!)

# vi program
du -x . | awk -v INP=$1 '{ if ($1 > 10) {print}}'
#
Learn What to do ,How to do and more importantly When to do ?
James R. Ferguson
Acclaimed Contributor

Re: sending parameter to awk command (urgent!!!)

Hi:

You can pass a shell variable to the 'awk' program like this:

# SZ=1000
# du -x .|awk -v SZ=${SZ} '{ if($1 > SZ) {print}}'

Notice that 'awk' references the variable named "SZ" without the dollar sign whereas the *value* of the variable in the shell is obtained by specifying the dollar sign.

The '-v SZ=${SZ}' tells 'awk' to estabish as variable called "SZ" using as its value the value obtained from the shell.

Regards!

...JRF...

Re: sending parameter to awk command (urgent!!!)

thank you all.

I used Mr. Karsten's code and it worked with the ...'$variable'... quotes. Is there any specific manual that I can read about using variables with the right quotes?
A. Clay Stephenson
Acclaimed Contributor

Re: sending parameter to awk command (urgent!!!)

Did you bother to examine the example I posted yesterday for you? It told you exactly how to do what you wanted. You are being confused by the awk positional variables vs. shell arguments. Note that anything inside single quotes is not altered in any manner by the shell.

du -x .|awk '{ if($1 > $2) {print}}'

$1 and $2 here refer to awk variables because they are within single quotes.

Now assume that your script does this:


du -x .|awk -v "size=${1}" '{ if($1 > sz) {print}}'

${1} is a shell variable and because it is within double quotes, instantiation will occur so that if you passwd 200 into your script, -v "size=${1}" becomes -v "size=200"; the -v argument to awk says declare size as an awk variable and assign it the value 200. You can then test awk variables againt another awk variable, size.

If it ain't broke, I can fix that.

Re: sending parameter to awk command (urgent!!!)

Of course I examined it but it looked a little complicated for me so I tried to find a more simplified version. Thanks again for the explanations...
A. Clay Stephenson
Acclaimed Contributor

Re: sending parameter to awk command (urgent!!!)

As I said yesterday, a better approach to what you are trying to do is to use Perl's File::Find module which is like the find command on steriods. You can call a function as you enter a new directory, a function is called on each file within the directory, and finally you can call a when all the files in a directory have been read. This suggest a mechanism to do your totals quite nicely. I already had a Perl script that was close and I spent about 5 minutes throwing a working, rather robust example together.

For example,

du.pl -s 20000 /etc /usr

will scan /etc and /usr and display the directory names whose contents exceed 20000 bytes.

du.pl -s 20000 -v -i /etc /usr
will do the same except each directory will also print the total size (-v) and will indent the output (-i) to show the depth of the directory.

du.pl -u will display full usage.

Since you are starting out. I would suggest that you learn only a little shell and then concentrate on learning Perl because Perl will allow you to do everything that shell, awk, sed, grep, ... will do and quite a bit more. As a bonus, most Perl scripts (including this one) will work without modification on Windows if one of the Windows Perl implementations is installed.


If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: sending parameter to awk command (urgent!!!)

I should mention that you should do a man File::Find to understand how the find function in Perl works.
If it ain't broke, I can fix that.
Raj D.
Honored Contributor

Re: sending parameter to awk command (urgent!!!)

Osman,
Hope you are looking for something like this :(This is a nice one. shows filesystems values , if it cross the threshold limit)

bdf | awk 'a[split($5,a,"%")-1]>90'


Enjoy,
Raj.
" If u think u can , If u think u cannot , - You are always Right . "
Dennis Handly
Acclaimed Contributor

Re: sending parameter to awk command (urgent!!!)

>I used Mr. Karsten's code and it worked with the ...'$variable'... quotes.

Actually that's not what it is doing.
It is doing: 'abc ...' $variable 'def'
but without the spaces around $variable. You could rewrite it as:
'abc ...'"$variable"'def'

>Is there any specific manual that I can read about using variables with the right quotes?

You can use man sh-posix or ksh. Basically $variables are not expanded in single quotes.

I would recommend you use JRF's approach with -v. (The trouble with Karsten's code is I never thought about doing that before. ;-)

If I didn't use -v would have done:
awk "{ if(\$1 > $variable) {print}}"

This requires you rigorously quote the "$" for awk variables. This can be painful for long awk scripts I write like:
$ ... awk .... '
BEGIN { ... }
{
...
}
END { ... }' files ...