Issue on Suming up the values in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue on Suming up the values in awk
# 1  
Old 06-06-2009
Issue on Suming up the values in awk

Hi

I have a data file like a.dat

item_no Item_id qty_ordered qty_delivered
123 axxx 1 1
456 axxx 2 2
899 axxx 2 2
789 bxxx 1 1
101 bxxx 2 2
111 bxxx 2 2


I want to split these single file in 2 file for the 2 different item_id like and get the totals of qty_order and qty_delivered.

item_no Item_id qty_ordered qty_delivered
123 axxx 1 1
456 axxx 2 2
899 axxx 2 2
--------------------------------------
total5 5 5

I tried to fetch those information in Spool file from data base and
put a grep -i "axxx" > c.dat
grep -i "bxxx" > d.dat

But i write a awk scrip to sum up and tried get the expected result...but am not able to..
Please suggest me some idea's to solve this.
Thanks in Advance!!
# 2  
Old 06-06-2009
Code:
sort -k2,2 filename| awk 'NR==1{ord=$3;del=$4;s=$2;print $0 > s ;getline}
$2 !~ s {print "--------" > s;print "total",ord,del > s ;s=$2;ord=0;del=0}
$2 ~ s {ord+=$3;del+=$4;print $0 > s ;s=$2;next}END{print "--------" > s;print "total",ord,del > s}'


-Devaraj Takhellambam
# 3  
Old 06-06-2009
Hi Devaraj
thanks for the reply...

Am getting error like ..

awk NR==1{ord=$5;del=$6;s=$4;print $0 > s ;getline}
$4 !~ s {print "--------" > s;print "total",ord,del > s ;s=$4;ord=0;del=0}
$4 ~ s {ord+=$5;del+=$6;print $0 > s ;s=$4;next}END{print "--------" > s;print "total",ord,del > s}
+ sort -k2,2 /ab/cd/item.dat
awk: A print or getline function must have a file name.
The input line number is 1.
The source line number is 1.
+ echo logout
logout
# 4  
Old 06-06-2009
How are you running the script..Use the sane that I had posted earlier by replacing filename with your actual filename


-Devaraj Takhellambam

-----Post Update-----

Code:
sort -k2,2 /ab/cd/item.dat |  awk 'NR==1{ord=$3;del=$4;s=$2;print $0 > s ;getline}
$2 !~ s {print "--------" > s;print "total",ord,del > s ;s=$2;ord=0;del=0}
$2 ~ s {ord+=$3;del+=$4;print $0 > s ;s=$2;next}END{print "--------" > s;print "total",ord,del > s}'


-Devaraj Takhellambam

Last edited by devtakh; 06-06-2009 at 06:49 AM..
# 5  
Old 06-06-2009
Use gawk, nawk or /usr/xpg4/bin/awk on Solaris:

Code:
awk 'END { 
  for (ID in id) 
    printf "%s\n%s\n------------------\ntotal: %.2f %.2f\n", \
    h, id[ID], ord[ID], dlv[ID] > ID 
  }
NR == 1 { h = $0; next }
{ 
  ord[$2] += $3; dlv[$2] += $4
  id[$2] = id[$2] ? id[$2] RS $0 : $0
}' infile

# 6  
Old 06-08-2009
perl:

Code:
while(<DATA>){
	chomp;
	my @tmp=split;
	$hash{$tmp[1]}->{content}.=$_."\n";
	$hash{$tmp[1]}->{order}+=$tmp[2];
	$hash{$tmp[1]}->{deliver}+=$tmp[3];
}
foreach my $key (keys %hash){
	my $file=sprintf("file_%s.txt",$key);
	open $fh,">","$file" or die "Can not open file";
	print $fh $hash{$key}->{content};
	print $fh "--------------------\n";
	print $fh "total: $hash{$key}->{order} $hash{$key}->{deliver}\n";
	close $fh;
}
__DATA__
123 axxx 1 1
456 axxx 2 2
899 axxx 2 2
789 bxxx 1 1
101 bxxx 2 2
111 bxxx 2 2

Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue using awk to print min values for unique ids

I am using the following script to search for and print minimum values for each individual Fields (3-14) for each unique id (Field 1). But when the field contains a "-99.99" ( I am ignoring "-99.99") and when the minimum value is the first line of a new id (Field 1), the output does not print Field... (13 Replies)
Discussion started by: ncwxpanther
13 Replies

2. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

3. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

4. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

5. Shell Programming and Scripting

error in shell script while returning values-- urgent issue plz help.

Hi, I have initailized a varaible EBID as typeset Long EBID=0 i am calculating value of EBID using certian formula as below: (( CURR_EBID= ($BANDINDEX << 27) | ($CURR_FREQ << 16) | ($CURR_CELLID << 4) | $CURR_SECTOR_VALUE )) return $CURR_EBID The output is as below: + (( CURR_EBID=... (6 Replies)
Discussion started by: kasanur
6 Replies

6. UNIX for Dummies Questions & Answers

Issue with use of Configuration file instead of hardcoded values inside the script

Hi, My code works perfectly fine. But, $my $min_to_add = 1 * 1 * 60; and my $hr_to_sub = 1 * 1 * 86400; i may need to change the values in future. so am keeping them in a separate configuration file like MIN = 1 * 1 * 60 HR = 24 * 60 * 60 in the script, i use a package use et_config... (3 Replies)
Discussion started by: irudayaraj
3 Replies

7. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

8. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies
Login or Register to Ask a Question