Sponsored Content
Top Forums Shell Programming and Scripting help sum columns by break in first column with awk or sed or something. Post 302282620 by syadnom on Sunday 1st of February 2009 03:09:36 AM
Old 02-01-2009
actual data

here is some actual data since I dont understand how this works.
Quote:
MIL 000067 1 .030 X 1.125 CONTACT TIP EACH 0 2 072-08 A
MIL 000067 1 .030 X 1.125 CONTACT TIP EACH 131 0 053-09 H088D4
MIL 000068 1 .035 X 1.125 CONTACT TIP EA 0 10 072-07 A
MIL 000068 1 .035 X 1.125 CONTACT TIP EA 327 0 053-10 H088D5
MIL 000069 1 .045 X 1.125 CONTACT TIP EA 0 6 072-09 A
MIL 000069 1 .045 X 1.125 CONTACT TIP EA 176 0 053-11 H088D6
this is the actual data I am working with. lets call it file 'data'. the numbers line up perfectly on the command line.

here is my script
Quote:
gawk '
{ FIELDWIDTHS = " 58 10 11 8 " }

$2 != $3
{ sum+=$2 }
{ print sum }' $1
I set fieldwidths as there are random spaces that mess up the process.

so $1 = all the detail up to the first number, $2 is on number and $3 is the other. $4 is the junk on the end.

I run this like
./script data

my output
Quote:
0
MIL 000067 1 .030 X 1.125 CONTACT TIP EACH 0 2 072-08 A
0
MIL 000067 1 .030 X 1.125 CONTACT TIP EACH 131 0 053-09 H088D4
131
MIL 000068 1 .035 X 1.125 CONTACT TIP EA 0 10 072-07 A
131
MIL 000068 1 .035 X 1.125 CONTACT TIP EA 327 0 053-10 H088D5
458
MIL 000069 1 .045 X 1.125 CONTACT TIP EA 0 6 072-09 A
458
MIL 000069 1 .045 X 1.125 CONTACT TIP EA 176 0 053-11 H088D6
634
here is the output. I see that it is adding up column $2 and printing it after the line but it does not reset the number on part number changes. I took the END out as I wanted the numbers after each part number.

How do I have the sum reset after each change in part number? (change in $1)

also, what is the appropriate way to sum both $2 and $3?

thanks!
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sum columns

can anyone help me how do i add the colums using awk seperated by character @. for eg i have 3@4 2@9 5@1 the result should be 10 14 i tried using { sum+= $1 } END { print sum } but it just gives the result 10. can anyone help me with this one thank you and best regards (7 Replies)
Discussion started by: phone_book
7 Replies

2. Shell Programming and Scripting

sum multiple columns based on column value

i have a file - it will be in sorted order on column 1 abc 0 1 abc 2 3 abc 3 5 def 1 7 def 0 1 -------- i'd like (awk maybe?) to get the results (any ideas)??? abc 5 9 def 1 8 (2 Replies)
Discussion started by: jjoe
2 Replies

3. Shell Programming and Scripting

Need help with awk statement to break nth column in csv file into 3 separate columns

Hello Members, I have a csv file in the format below. Need help with awk statement to break nth column into 3 separate columns and export the changes to new file. input file --> file.csv cat file.csv|less "product/fruit/mango","location/asia/india","type/alphonso" need output in... (2 Replies)
Discussion started by: awk-admirer
2 Replies

4. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

5. UNIX for Dummies Questions & Answers

Want the UNIX code - I want to sum of the 1st column wherever the first 2nd and 3rd columns r equal

I have the code for the below things.. File1 has the content as below 8859 0 subscriberCreate 18 0 subscriberPaymentMethodChange 1650 0 subscriberProfileUpdate 7668 0 subscriberStatusChange 13 4020100 subscriberProfileUpdate 1 4020129 subscriberStatusChange 2 4020307 subscriberCreate 8831... (5 Replies)
Discussion started by: Mahen
5 Replies

6. Shell Programming and Scripting

Sum of Columns Base on First Column

Input :- Hd1;Hd2:hd3;Hd4;Hd5 X;1;2;3;4 Y;2;3;5;6 Z;3;5;6;7 X;10;11;24;16 Y;11;23;21;1 Z;10;13;14;15 X;0;1;2;0 K;0;0;0;0 K;0;0;0;0 I want Sum Data base on first column; Hd1;Hd2:hd3;Hd4;Hd5 X;11;14;29;20 Y;12;26;26;7 Z;13;18;20;22 K;0;0;0;0 (4 Replies)
Discussion started by: pareshkp
4 Replies

7. Shell Programming and Scripting

Sum of columns using awk

Hello everyone I am a beginner in Shell scripting. Need your help to achieve desired result. I have a file (sample format below) 001g8aX0007jxLz xxxxxxxxxxxxxxx 9213974926411 CO-COMM-133 CO-L001-DLY 7769995578239 44938 1 1 ... (1 Reply)
Discussion started by: Rohit Mallah
1 Replies

8. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

9. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (12 Replies)
Discussion started by: as7951
12 Replies

10. UNIX for Beginners Questions & Answers

Copy columns from one file into another and get sum of column values and row count

I have a file abc.csv, from which I need column 24(PurchaseOrder_TotalCost) to get the sum_of_amounts with date and row count into another file say output.csv abc.csv- UTF-8,,,,,,,,,,,,,,,,,,,,,,,,, ... (6 Replies)
Discussion started by: Tahir_M
6 Replies
sum(1B) 					     SunOS/BSD Compatibility Package Commands						   sum(1B)

NAME
sum - calculate a checksum for a file SYNOPSIS
/usr/ucb/sum file... DESCRIPTION
sum calculates and displays a 16-bit checksum for the named file and displays the size of the file in kilobytes. It is typically used to look for bad spots, or to validate a file communicated over some transmission line. The checksum is calculated by an algorithm which may yield different results on machines with 16-bit ints and machines with 32-bit ints, so it cannot always be used to validate that a file has been transferred between machines with different-sized ints. USAGE
See largefile(5) for the description of the behavior of sum when encountering files greater than or equal to 2 Gbyte ( 2^31 bytes). ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWscpu | +-----------------------------+-----------------------------+ SEE ALSO
sum(1), wc(1), attributes(5), largefile(5) DIAGNOSTICS
Read error is indistinguishable from EOF on most devices; check the block count. NOTES
sum and /usr/bin/sum (see sum(1)) return different checksums. This utility is obsolete. SunOS 5.11 8 Nov 1995 sum(1B)
All times are GMT -4. The time now is 12:29 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy