- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Using awk and variables
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
Forums
Discussions
Discussions
Discussions
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
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-19-2007 03:20 AM
03-19-2007 03:20 AM
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)
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 03:26 AM
03-19-2007 03:26 AM
Solution- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 03:29 AM
03-19-2007 03:29 AM
Re: Using awk and variables
ls -l | awk -v "myvar=${x}" '$9 == myvar { sum += $5 } END { print sum }'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 03:55 AM
03-19-2007 03:55 AM
Re: Using awk and variables
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 03:55 AM
03-19-2007 03:55 AM
Re: Using awk and variables
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 03:59 AM
03-19-2007 03:59 AM
Re: Using awk and variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:06 AM
03-19-2007 04:06 AM
Re: Using awk and variables
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:12 AM
03-19-2007 04:12 AM
Re: Using awk and variables
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:19 AM
03-19-2007 04:19 AM
Re: Using awk and variables
Thanks.
Manuales.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:21 AM
03-19-2007 04:21 AM
Re: Using awk and variables
# 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:28 AM
03-19-2007 04:28 AM
Re: Using awk and variables
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:30 AM
03-19-2007 04:30 AM
Re: Using awk and variables
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 ..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:30 AM
03-19-2007 04:30 AM
Re: Using awk and variables
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:31 AM
03-19-2007 04:31 AM
Re: Using awk and variables
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:36 AM
03-19-2007 04:36 AM
Re: Using awk and variables
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2007 04:42 AM
03-19-2007 04:42 AM
Re: Using awk and variables
THANKS TO ALL!!!!!
THANKS FOR YOUR HELP !!!
Talk you later, in my next doubt .
:D