- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- easy! bash, awk, sed or perl or whatelse to sum re...
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
02-22-2006 07:13 AM
02-22-2006 07:13 AM
Hi, I would like to count from a text file recurrences (more then one recurrence per single record) and if the string is a number, sum .
i.e. "red" is the recurrence and we have two different records.
red=4,blu=6,red=7
gray=5,red=2
---
red=3,13 (4+7+2=13)
p.s. file can be large and sum also.
Thanks
Leo.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2006 02:27 PM
02-22-2006 02:27 PM
Re: easy! bash, awk, sed or perl or whatelse to sum recurrrences results
I understand the example you provided, but I'm unclear on your actual request.
Correct me if I'm wrong...
You have a potentially large file containing records with multiple name/value pairs in which the "value" (after the equal sign) may or may not be an integer?
You want a script to which you can input a string (name) and the script should output the number of occurences of that string along with the sum of the corresponding values (assuming they're numeric)?
In short, please elaborate on the requirements of this "challenge".
Jared
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2006 06:09 PM
02-22-2006 06:09 PM
Re: easy! bash, awk, sed or perl or whatelse to sum recurrrences results
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2006 07:27 PM
02-22-2006 07:27 PM
Solutionperl -lan -F/,/ -e 'for ($i=0;$i<=$#F;$i++) { @arr=split (/=/,$F[$i]); $count{"$arr[0]"}++;$color{"$arr[0]"}+=$arr[1];}
END{foreach $key (keys(%count)){ print $key . "=" . $count{"$key"} . "," . $color{"$key"};}}'
Output:
blue=1,6
gray=1,5
red=3,13
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2006 07:49 PM
02-22-2006 07:49 PM
Re: easy! bash, awk, sed or perl or whatelse to sum recurrrences results
Thank you!
Leo.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2006 09:02 PM
02-22-2006 09:02 PM
Re: easy! bash, awk, sed or perl or whatelse to sum recurrrences results
What Muthukumar has given you already is the basis of it, you just need to select which of the key values you want to print out, instead of just looping through all values and printing them.
Trust me when I say that it will be easier in the long run, rather than trying to swap back-and-forth between shell and perl scripts.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-22-2006 09:36 PM
02-22-2006 09:36 PM
Re: easy! bash, awk, sed or perl or whatelse to sum recurrrences results
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-23-2006 09:52 AM
02-23-2006 09:52 AM
Re: easy! bash, awk, sed or perl or whatelse to sum recurrrences results
I've hacked mine up a bit to give me some more interesting statistics, but the basic script (which is long) works great as an example - I'm not much of a Perl guy, but it's close enough to C that anyone with some programming background should be able to grok the needed changes.
LogWatch comes already with breakdowns of messages sizes (how many messages 0-10k, 10-20k, etc.), total bytes transfered, biggest relays, etc.
Good stuff - I highly recommend it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-13-2006 07:34 PM
03-13-2006 07:34 PM
Re: easy! bash, awk, sed or perl or whatelse to sum recurrrences results
Though it's almost three weeks old, but it's intriguing enough to take a stab at it. You'ave probably solved it already but just in case here's my attempt...although belated :)
Save the attached file (containing awk commands) as "myawkscr" and run them against your input file as follows:
# awk -f myawkscr inputfile
cheers!