- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: sending parameter to awk command (urgent!!!)
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 01:48 AM
тАО03-02-2007 01:48 AM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 02:00 AM
тАО03-02-2007 02:00 AM
Re: sending parameter to awk command (urgent!!!)
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 02:00 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 02:01 AM
тАО03-02-2007 02:01 AM
Re: sending parameter to awk command (urgent!!!)
du -x . | awk -v INP=$1 '{ if ($1 > 10) {print}}'
#
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 02:08 AM
тАО03-02-2007 02:08 AM
Re: sending parameter to awk command (urgent!!!)
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 02:12 AM
тАО03-02-2007 02:12 AM
Re: sending parameter to awk command (urgent!!!)
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 02:19 AM
тАО03-02-2007 02:19 AM
Re: sending parameter to awk command (urgent!!!)
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 03:09 AM
тАО03-02-2007 03:09 AM
Re: sending parameter to awk command (urgent!!!)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 07:56 AM
тАО03-02-2007 07:56 AM
Re: sending parameter to awk command (urgent!!!)
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 07:57 AM
тАО03-02-2007 07:57 AM
Re: sending parameter to awk command (urgent!!!)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 10:16 AM
тАО03-02-2007 10:16 AM
Re: sending parameter to awk command (urgent!!!)
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-02-2007 02:10 PM
тАО03-02-2007 02:10 PM
Re: sending parameter to awk command (urgent!!!)
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 ...