- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Awk problem - addition?
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
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
тАО09-11-2008 04:32 AM
тАО09-11-2008 04:32 AM
Would someone here know how to use awk to do an addition?
Here's what I'm trying to do:
if you do an ls -l, it prints out direcctory/file sizes. Right? So, let's say the column in which the directory falls is the 9th? I forget.. but its around the 9th,
so, I now want to add the TOTAL file sizes/sub directory sizes in a directory, that may contain 200 files or sub folders.
ls -l | swk '{print $9}' |
Thanks
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 04:38 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 04:40 AM
тАО09-11-2008 04:40 AM
Re: Awk problem - addition?
# ls -l | awk '/^total/ {next};{SUM+=$5};END{printf "%10d\n",SUM}'
...note that this skips the "total" line returned by 'ls -l'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 04:53 AM
тАО09-11-2008 04:53 AM
Re: Awk problem - addition?
ls -l |grep ^- | awk...
Note that in the "ls -l" listing for a subdirectory line the size number does NOT represent the contents of the subdirectory but rather its file table. You may or may not want to exclude these. If you want to sum up the contents of subrirectories you have to use the df commnad as noted above.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 05:02 AM
тАО09-11-2008 05:02 AM
Re: Awk problem - addition?
To find
a) The total number of files in a directory
b) The total filesize
#cd /dir
#ls -l | awk '$6{b+=$5;a+=1}END{print a,b}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 05:08 AM
тАО09-11-2008 05:08 AM
Re: Awk problem - addition?
I agree with TTr insofar as you probably want to exclude the space used for directories and for symbolic links. You don't need to add another process, namely 'grep', though since 'awk' easily does this too:
# ls -l | awk '!/^-/ {next};{SUM+=$5};END{printf "%10d\n",SUM}'
Now, if you truly want the size of all *files* in all of the subdirectories in your 'ls' path, you can add the '-R' switch to 'ls' to do:
# ls -lR | awk '!/^-/ {next};{SUM+=$5};END{printf "%10d\n",SUM}'
Regards!
...JRF...
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 05:09 AM
тАО09-11-2008 05:09 AM
Re: Awk problem - addition?
ls -l | awk '{sum += $5};END{printf "%d\n", sum"
It prints out the same result?? no? And then why did you skip the "Total" word.. how would awk "add" a word ?? I thought it would just skip it?
But you're in inspiration, and I'm giving u 8 points, and the nice lady above 2 points.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 05:18 AM
тАО09-11-2008 05:18 AM
Re: Awk problem - addition?
It doesn't seem to be stable when adding...
Why is that??
Try it out..
Here's my way:
ls -l | awk '{SUM += $5};END{printf "%d\n", SUM}'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 05:26 AM
тАО09-11-2008 05:26 AM
Re: Awk problem - addition?
> But I'm just curious, what's wrong with this?
> ls -l | awk '{sum += $5};END{printf "%d\n", sum"
> It prints out the same result?? no? And then why did you skip the "Total" word.. how would awk "add" a word ?? I thought it would just skip it?
The answer is that there is no fifth (5th) column for the "total" heading line, so 'awk' adds zero as it were. I chose to explicitly skip the "total" line simply to be pure and rigorous.
> I'm just curious about one thing. In a directory where there are long listings (i.e. 100's of files), whether I use my way, or your way, the figures change.
I suspect that you have entries special files (character and block devices). Sockets have a size of zero so those wouldn't matter. My second suggestion confined the totals to only *regular* files --- not directories nor special files nor symbolic links.
If we want to be pedantic we should probably account for hard-linked files too :-))
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-11-2008 05:45 AM
тАО09-11-2008 05:45 AM