Get the total of a field in all the lines of a group


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the total of a field in all the lines of a group
# 1  
Old 10-31-2008
Get the total of a field in all the lines of a group

Hi
I have a Fixed format data file where I need to to get the total of the field at certain position in a file for a group of lines.

In this data file I need the total of all the field ats position 30:39 for each line starting with 6 and for each group startign with 5. Which means for every line starting with 5 I need the total of the fields in line 6 respecitvely. So I would get 2 totals in this case as I have two lines starting with 5.

With this script I am getting for all lines with 6 I need it to geoup by line 5.
How to do it? Please help

Code:
echo $(awk '{ if (substr($0,1,1)=="6") { entry_account=substr($0,30,10); s=sprintf("%s",entry_account);print s;}}' 101408)

101 12110825030430021170810060810A094101BANK OF AMERICA
5200AMERICAN EXP 144892 3043002117CCDOCT06 2008
62201100123409-1960 000013768460000526 AMERICAN
62201100123409-1960 000085263060000526 AMERICAN
62201100123409-1960 000004188960000526 AMERICAN
62201100123409-1960 000025960260000526 AMERICAN
62201100123409-1960 000009390060000526 AMERICAN
62201100123409-1960 000028661060000526 AMERICAN
62201100123409-1960 000006656060000526 AMERICAN
62201100123409-1960 000004047360000526 AMERICAN
62201100123409-1960 000002500060000526 AMERICAN
5200AMERICAN EXP 144892 3043002117CCDOCT07 2008
62201100123409-1960 000023768460000526 AMERICAN
62201100123409-1960 000025263060000526 AMERICAN
62201100123409-1960 000024188960000526 AMERICAN
705RMR*11*379477306771000*0000133379
# 2  
Old 10-31-2008
Base on your sample data what should be the expected output?
# 3  
Old 10-31-2008
Hi
The output should be like this. The totals should go where it is bold

101 12110825030430021170810060810A094101BANK OF AMERICA
5200AMERICAN EXP 144892 3043002117CCDOCT06 2008
62201100123409-1960 000013768460000526 AMERICAN
5200AMERICAN EXP 144892 3043002117CCDOCT07 2008
62201100123409-1960 000023768460000526 AMERICAN
705RMR*11*379477306771000*0000133379
# 4  
Old 10-31-2008
This code will give you the required output.
Code:
awk '{if(substr($0,1,1)==a) next;a=substr($0,1,1)}1' file

# 5  
Old 10-31-2008
Hi
I want the total of the field at position 30-39 on all Lines 6 to be displayed. so Every 5 line will have only one 6 line with the total of all the lines 6

Thanks
Pls help
so the total sum of of all the values in quotes " " should be dispalyed in the first 6 line and delete remaining. similarly for the other 5 line do the same.

101 12110825030430021170810060810A094101BANK OF AMERICA
5200AMERICAN EXP 144892 3043002117CCDOCT06 2008
62201100123409-1960 "0000137684"60000526 AMERICAN
62201100123409-1960 "0000852630"60000526 AMERICAN
62201100123409-1960 "0000041889"60000526 AMERICAN
62201100123409-1960 "0000259602"60000526 AMERICAN
62201100123409-1960 "0000093900"60000526 AMERICAN
62201100123409-1960 "0000286610"60000526 AMERICAN
62201100123409-1960 "0000066560"60000526 AMERICAN
62201100123409-1960 "0000040473"60000526 AMERICAN
62201100123409-1960 "0000025000"60000526 AMERICAN
5200AMERICAN EXP 144892 3043002117CCDOCT07 2008
62201100123409-1960 000023768460000526 AMERICAN
62201100123409-1960 000025263060000526 AMERICAN
62201100123409-1960 000024188960000526 AMERICAN
705RMR*11*379477306771000*0000133379

Last edited by appsguy616; 10-31-2008 at 02:32 PM..
# 6  
Old 10-31-2008
Your request is not very clear, please show me the required output.
# 7  
Old 10-31-2008
The required output should be. Basically I want only one line starting with 6 should appear but the field at positon 30:39 for the line should be the sum of all the fields at this position on all the lines starting with 6. But this should group by based on line starting with 5.

101 12110825030430021170810060810A094101BANK OF AMERICA
5200AMERICAN EXP 144892 3043002117CCDOCT06 2008
62201100123409-1960 "0001710448"60000526 AMERICAN
5200AMERICAN EXP 144892 3043002117CCDOCT07 2008
62201100123409-1960 "0000732203"60000526 AMERICAN
705RMR*11*379477306771000*0000133379

where "0001710448" is the total of all the fields in quotes lines starting with 6 for the first group line 5
"0000137684"
"0000852630"
"0000041889"
"0000259602"
"0000286610"
"0000066560"
"0000040473"
"0000025000"
"0001710448"


Smilarly 0000732203 is the total of all the lines starting with 6 for the second group line 5
"0000237684"
"0000252630"
"0000241889"
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 calculate total and percent off field in file

Trying to use awk to print the lines in file that have either REF or SNV in $3, add a header line, sort by $4 in numerical order. The below code does that already, but where I am stuck is on the last part where the total lines are counted and printed under Total_Targets, under Targets_less_than is... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Calculate the total 4 field based on the conditions

Please help me to write a script Match with ACNO & NAME if it matched calculate the total val1 val2 val3 and val4 and GT is total of ACNO wise.please check the output Table ----------------- 1005|ANDP|ACN|20|50|10|30 1005|ANDP|ACN|20|10|30|40 1001|AND|NAC|40|50|40|50... (22 Replies)
Discussion started by: kalia4u
22 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 repeat one field at all lines and modify field repetitions

Hello experts I have a file with paragraphs begining with a keeping date and ending with "END": 20120301 num num John num num A keepnum1 num num kathrin num num A keepnum1 num num kathrin num num B keepnum2 num num Pete num num A keepnum1 num num Jacob num... (2 Replies)
Discussion started by: phaethon
2 Replies

5. Shell Programming and Scripting

Transpose, Group, and Total

Need help with some data below. I would like to transpose rows of column c, h to columns with those labels(c,h.) I would then like to sort and group by the values in columns c then h. For column h I would like to further sort by the parenthesis values. After the sort I would like an... (3 Replies)
Discussion started by: ravzter
3 Replies

6. Shell Programming and Scripting

Select lines in which column have value greater than some percent of total file lines

i have a file in following format 1 32 3 4 6 4 4 45 1 45 4 61 54 66 4 5 65 51 56 65 1 12 32 85 now here the total number of lines are 8(they vary each time) Now i want to select only those lines in which the values... (6 Replies)
Discussion started by: vaibhavkorde
6 Replies

7. Shell Programming and Scripting

Adding total of first field for each number in the second field

Dears, I need a script or command which can find the unique number from the second filed and against that number it adds the total of first field . 17215630 , 0 907043 ,1 201050 ,10 394149 ,4 1964 ,9 17215630, 0 907043 ,1 201050, 10 394149 ,4 1964 ,9 1234234, 55 23 ,100 33 ,67 ... (2 Replies)
Discussion started by: shary
2 Replies

8. Shell Programming and Scripting

need to delete all lines from a group of files except the 1st 2 lines

Hello, I have a group of text files with many lines in each file. I need to delete all the lines in each and only leave 2 lines in each file. (3 Replies)
Discussion started by: script_op2a
3 Replies

9. Shell Programming and Scripting

Awk-Group count of field

Hi, Suppose if i am having a file with following records as given below. 5555 6756 5555 4555 4555 6767 how can i get the count of each record using AWK. Eg:5555 count should be 2 4555 count should be 2 6767 count should be 1 ... (5 Replies)
Discussion started by: tinivt
5 Replies

10. Shell Programming and Scripting

total number of lines

Hi have following file |abcd 2|abcd |sdfh |sdfj I want to find total number of files haivng nothing in feild 1 using awk will command awk -F "|" '( $1=="") {print NR}' test_awk will work??? (4 Replies)
Discussion started by: mahabunta
4 Replies
Login or Register to Ask a Question