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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How To Calculate krishna_sicsr Shell Programming and Scripting 3 03-21-2009 01:49 PM
calculate 13 months ago andrea_mussap Shell Programming and Scripting 4 11-21-2007 08:14 PM
calculate 13 months ago andrea_mussap Shell Programming and Scripting 3 11-16-2007 03:18 PM
Count No of Records in File without counting Header and Trailer Records guiguy Shell Programming and Scripting 2 06-07-2007 12:15 PM
How to calculate with awk whatisthis Shell Programming and Scripting 4 11-09-2005 12:39 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 Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 06-10-2005
sickboy sickboy is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 62
How to calculate a sum of certain records?

Hi,
i have a file where the records are like this.

vt100 2048 D402 MG0010 0
586 262144 D403 MG0011 1000
486 8192 D404 MG0012 270
386 8192 A423 CC0177 40
586 65536 A424 CC0182 670
486 16384 A423 CC0183 100
486 16384 A425 CC0184 80
65000 4096 B407 EE1027 80

I want firstly,
to count how many times each combination of the first 2 letters from the 4th field exist and print it like this:
3 MG or MG 3
5 CC

I tried this :
Code:
awk -F"\t" '{print $4}' pcs.txt | uniq -c
but I didn't know how to count only the first two letters of the fied.

Secondly,
I want to sum the second field for each differrent entry of the 3rd field and print them. Like this
A423 24576
A425 16384

I tried this :
Code:
awk -F"\t" '{a[$3]=$3; 
for (i = 1; i <= NR; i++){
for (i = 1; i <= NR; i++){
if(a[++i] = $3){
size+=$2}
}
print a[i]" "size
}' pcs.txt
but somewhere I have a mistake and I can't find it and also i am not sure if this is going to give me what I want.

Please help me
  #2 (permalink)  
Old 06-10-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
This should do it for your first counting part...

Code:
sed -n -e 's/.*\([A-Z][A-Z]\)[0-9]*.*/\1/p' pcs.txt | uniq -c
For your input, it gave,

3 MG
4 CC
1 EE

Basically, the scripts strips out every character except for the two upper-case characters. And the script assumes you always have input which obey the format you provided.

Vino
  #3 (permalink)  
Old 06-10-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Oops...

Got the wrong message in the wrong thread.

Vino

Last edited by vino; 06-10-2005 at 10:11 AM.. Reason: Posted in the wrong thread....
  #4 (permalink)  
Old 06-10-2005
sickboy sickboy is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 62
Maybe I didn't explained it right. I need to find the sum for every different record of the 3rd field. So I need to sum the two numbers of the A423 and print the total.


PS Sorry for my English
  #5 (permalink)  
Old 06-10-2005
sickboy sickboy is offline
Registered User
  
 

Join Date: Jun 2005
Posts: 62
Quote:
Originally Posted by vino
This should do it for your first counting part...

Code:
sed -n -e 's/.*\([A-Z][A-Z]\)[0-9]*.*/\1/p' pcs.txt | uniq -c
It is working for the part of the file i gave in the beginning but it is not working in the whole file right. Probably uniq -c is counting the same lines that are one next to the other and not spread in the file. What I did to get the result I wanted is the use of sort.

Code:
sed 's/.*\([A-Z][A-Z]\)[0-9]*.*/\1/p' pcs.txt |sort| uniq -c
Do you know if instead of sed I can use awk to get the same result?
  #6 (permalink)  
Old 06-10-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
I think for uniq to work properly, it needs a sort'ed list.

Not sure, tho'.

Vino
  #7 (permalink)  
Old 06-10-2005
r2007's Avatar
r2007 r2007 is offline
Registered User
  
 

Join Date: Jun 2005
Location: China
Posts: 73
for 2nd question:
Code:
awk -F"\t" '{a[$3]+=$2}END{for (i in a) print i,a[i]}'
[not test]
Sponsored Links
Closed Thread

Bookmarks

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 09:09 PM.


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