How do you subtotal lines in a file? Awk?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do you subtotal lines in a file? Awk?
# 1  
Old 02-17-2011
How do you subtotal lines in a file? Awk?

I have a file with 8 fields. I need the subtotals for fields 7 & 8 when field 5 changes.

cat wk1
Code:
01/02/2011/18AB/17/18/000000071/000000033
01/02/2011/18AB/17/18/000000164/000000021
01/02/2011/18AB/17/18/000000109/000000023
01/02/2011/28FB/04/04/000000000/000000000
01/09/2011/11AB/08/03/000000237/000001153
01/09/2011/21AB/03/03/000000263/000001725
01/09/2011/21AB/03/03/000000273/000000430

Desired output

Code:
01/02/2011/18AB/17/18/000000344/000000077
01/02/2011/28FB/04/04/000000000/000000000
01/09/2011/21AB/03/03/000000237/000001153
01/09/2011/21AB/03/03/000000536/000002155

How do you do this using awk?
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by vgersh99; 02-17-2011 at 05:05 PM.. Reason: code tags, please!
# 2  
Old 02-17-2011
Do you need to retain original order of those lines?
Code:
awk -F/ '{a[$5]+=$7;b[$5]+=$8;c[$5]=$1FS$2FS$3FS$4FS$5FS$6}END{for (i in a){printf "%s/%07d/%07d\n",c[i],a[i],b[i]}}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 02-17-2011
Code:
awk -F \/ '
{s=$1FS$2FS$3FS$4FS$5FS$6;a[s]+=$7;b[s]+=$8}
END{for (i in b) printf "%s/%07d/%07d\n", i, a[i], b[i]|"sort -t / -k3.3n -k2.2n -k1.1n"}' infile

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reorder lines in file

The output of an awk script is the below file. Line 1,3 that starts with the Ion... need to be under line 2,4 that starts with R_. The awk runs but no output results. Thank you :). file IonXpress_007 MEV37 R_2016_09_20_12_47_36_user_S5-00580-7-Medexome IonXpress_007 MEV40... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

awk remove/grab lines from file with pattern from other file

Sorry for the weird title but i have the following problem. We have several files which have between 10000 and about 500000 lines in them. From these files we want to remove lines which contain a pattern which is located in another file (around 20000 lines, all EAN codes). We also want to get... (28 Replies)
Discussion started by: SDohmen
28 Replies

3. Shell Programming and Scripting

How to add subtotal and total according 3rd field mentioned below table?

111AKKK|SHA|20|25 111|AKKK|SHA|15|35 112|ABC|FL|25|45 112|ABC|FL|15|15 115|AKKK|ASH|10|15 115|AKKK|ASH|20|20 112|ABC|FL|25|20 115|AKKK|ASH|30|35 111|AKKK|SHA|10|45 112|ABC|KL|15|15 112|ABC|KL|20|25 115|AKKK|ASH|30|35 please write a shell script output should be below mentioned... (26 Replies)
Discussion started by: udhal
26 Replies

4. Shell Programming and Scripting

awk last n lines of file

Just my second week working on awk I need a hint for the following tasks. I want to limit my logfile from the very outset to 200 lines. All I do until now is head -c 10K >> /home/uplog.txt | awk 'END{print NR " swap " NF$5; exit}' /home/uplog.txt; After being read it shall print the very... (27 Replies)
Discussion started by: 1in10
27 Replies

5. Shell Programming and Scripting

Subtotal in UNIX

Please help me on below req Data in file ARIZONA HCPAZ 47 ARIZONA HCPAZCONT 3056 ARIZONA AZA 20 CALIFORNIA HC06 878 CALIFORNIA LC04 51 CALIFORNIA LC06 4039 CALIFORNIA HCPCACONT 4960 THE CAMDEN GROUP CAM 83... (7 Replies)
Discussion started by: skchevva
7 Replies

6. Shell Programming and Scripting

Read a file using awk for a given no of lines.

Hi, how do i read a file using awk for a given no of line? e.g 1. read only first 50 line. 2. read starting from line 20 to line 60.. thanks in advance. -alva (8 Replies)
Discussion started by: alvagenesis
8 Replies

7. Shell Programming and Scripting

Reducing file lines in awk

Hi, Here i have to check first record $3 $4 with second record $1 $2 respectively. If match found, then check first record $2 == second record $4 , if it equals , then reduce two records to single record like as desired output. Input_file 1 1 2 1 2 1 3 1 3 1 4 1 3 1 3 2 desired... (3 Replies)
Discussion started by: vasanth.vadalur
3 Replies

8. Shell Programming and Scripting

awk print lines in a file

Dear All, a.txt A 1 Z A 1 ZZ B 2 Y B 2 AA how can i use awk one line to achieve the result: A Z|ZZ B Y|AA Thanks (5 Replies)
Discussion started by: jimmy_y
5 Replies

9. Shell Programming and Scripting

Select some lines from a txt file and create a new file with awk

Hi there, I have a text file with several colums separated by "|;#" I need to search the file extracting all columns starting with the value of "1" or "2" saving in a separate file just the first 7 columns of each row maching the criteria, with replacement of the saparators in the nearly created... (4 Replies)
Discussion started by: capnino
4 Replies

10. Shell Programming and Scripting

subtotal columns

Hello I have a file that has two (or more) different types of records I want to total. How would I do this using awk? The file may contain several dozen records. The records are sorted on the database column - what I want to do is get the amount of space that each table has in that database... (5 Replies)
Discussion started by: stonemonolith
5 Replies
Login or Register to Ask a Question