The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help needed in sed Alecs Shell Programming and Scripting 1 06-14-2008 01:46 PM
Help Needed in SED raghav1982 Shell Programming and Scripting 4 04-07-2008 03:04 AM
SED Help Needed Tide UNIX for Dummies Questions & Answers 2 01-28-2008 06:14 AM
Help Needed -sed ravi.sadani19 Shell Programming and Scripting 9 10-06-2006 02:54 AM
Scp Help Needed !!!! scooter17 UNIX for Dummies Questions & Answers 3 09-20-2006 12:50 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-23-2008
findprakash findprakash is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 31
Awk help needed..

I have to add number in column 1 by month.

1531 Feb 1 2007 pgaps_p007_27909.trc
1529 Feb 1 2007 pgaps_p006_27907.trc
1533 Feb 1 2007 pgaps_p005_27905.trc
635 Apr 7 2007 pgaps_smon_2347.trc
634 Apr 7 2007 pgaps_lgwr_2343.trc
634 Apr 8 2007 pgaps_lgwr_2311.trc
613 Apr 10 2007 pgaps_p004_402.trc
615 Apr 10 2007 pgaps_p002_7500.trc
698 May 2 2007 pgaps_smon_2339.trc
634 May 5 2007 pgaps_lgwr_2335.trc
656 Mar 1 2007 pgaps_smon_8006.trc
634 Mar 3 2007 pgaps_lgwr_8002.trc
634 Mar 4 2007 pgaps_lgwr_2383.trc
641 Mar 8 2007 pgaps_qmn0_20351.trc

Output should be :
Feb 4593
Apr 3131
May 3897

When I run below awk command I am not getting desired output.

ls -ltr |awk 'BEGIN { OFS = "\t"; ORS = "\n\n" } { print $1, $2, $3, $4 }'| awk '{tot=tot+$1}END{print tot}'

Any help would be greatly appreciated.
  #2 (permalink)  
Old 08-23-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
I don't get that kind of output with ls -l but assuming you do, the following ought to work.

Code:
ls -l | awk '{ tot[$2] += $1 } END { for (t in tot) printf "%s\t%i\n", t, tot[t] }'
Requesting ls to sort the output is unnecessary, if you require chronological output order, some additional tricks will be needed. The array loop in awk for (x in y) traverses the keys of y in unpredictable order.

Also the sample output you posted doesn't seem to agree with the input. I get the following

Code:
Feb     4593
May     1332
Apr     3131
Mar     2565
Did I misunderstand your requirement, or is the sample output wrong? Looks like you accidentally summed Mar and May into the same category.
  #3 (permalink)  
Old 08-23-2008
findprakash findprakash is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 31
Era,
Thanks for the update. Your observation is absolutely correct. I removed first few colums of ls -ltr output as they are not needed in my script. When I run command provided by you I do not get intended output.

ls -l | awk '{ tot[$2] += $1 } END { for (t in tot) printf "%s\t%i\n", t, tot[t] }'

354272 %ii
1 %ii

Thanks,
Prakash
  #4 (permalink)  
Old 08-23-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The real column numbers are $5 and $6 then, if your ls gives the same output format as mine.
  #5 (permalink)  
Old 08-24-2008
findprakash findprakash is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 31
Era,
Thanks for your help. I am able to get output as desired.
Sponsored Links
Closed Thread

Bookmarks

Tags
sum by column, sum by month

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:14 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0