- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: sh script to sort, group and sum a flat file
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
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
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
12-31-2013 12:05 PM
12-31-2013 12:05 PM
I have the following flat file that is basically DATE #
Looks like this:
130426 246
130611 3
131011 28
131012 26
131131 117
131228 7
131231 117
I can sum up total, that is no problem.
But what I also what to do is group and sum by month.
So it will display:
1304 246
1306 3
1310 54
1312 124
Any help would be appreicated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2013 02:53 PM - edited 01-01-2014 02:28 AM
12-31-2013 02:53 PM - edited 01-01-2014 02:28 AM
SolutionI'm assuming the data is aready sorted. If not, use "sort -k1.1,1.4" and pipe to this awk script.
Something like this:
awk '
BEGIN {
sum = 0
save_yymm = ""
}
{
yymm = substr($1, 1, 4)
if (yymm != save_yymm) { # new month
if (save_yymm != "")
print save_yymm, sum
save_yymm = yymm # save new
sum = 0
}
sum += $2
}
END {
print save_yymm, sum
}' input-file
- Tags:
- awk