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
# 15  
Old 11-03-2008
Hi
Can you pls let me know why cant i get the 6 lines with the same code?
# 16  
Old 11-03-2008
Have you tried nawk? Anyhow try to change the second line like this:

Code:
awk '
{if((/^5/ || /^7/) && t) {printf("%s%010d%s\n",substr(s,1,20),t,substr(s,31));t=0}}
/^6/ {t+=substr($0,21,10);s=$0;next}
{print}' file

# 17  
Old 11-03-2008
Hi
nawk doesn't work for me. I still dont get the 6 lines.
# 18  
Old 11-04-2008
Hi
The script works only if the data file is that format. My datafile is sometime in this format too. How to do this. Pls help. Thanks

101 3234344443433230430021170810060810A094101CITIZEN AMERICAN
5200AMERICAN 144892 4333332117CCDOCT06 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
705RMR*11*379426274762004*0000852630\
705RMR*11*378531408781001*0000259602\
705RMR*11*378538061761006*0000093900\
705RMR*11*379424325743008*0000286610\
62201100123409-1960 000013337960000526 AMERICAN EXP
5200AMERICAND 144892 3232302117CCDOCT07 2008
62201100123409-1960 000023768460000526 AMERICAN
62201100123409-1960 000025263060000526 AMERICAN
62201100123409-1960 000024188960000526 AMERICAN
62201100123409-1960 000025960260000526 AMERICAN
62201100123409-1960 000029390060000526 AMERICAN
62201100123409-1960 000028661060000526 AMERICAN
705RMR*11*379477306771000*0000133379\
820000018601023114390000000000000000139365873043002117
9000001000020000001860102311439000000000000000013936587
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
# 19  
Old 11-04-2008
I'm getting this output and that seems to be correct:

Code:
$ cat file
101 3234344443433230430021170810060810A094101CITIZEN AMERICAN
5200AMERICAN 144892 4333332117CCDOCT06 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
705RMR*11*379426274762004*0000852630\
705RMR*11*378531408781001*0000259602\
705RMR*11*378538061761006*0000093900\
705RMR*11*379424325743008*0000286610\
62201100123409-1960 000013337960000526 AMERICAN EXP
5200AMERICAND 144892 3232302117CCDOCT07 2008
62201100123409-1960 000023768460000526 AMERICAN
62201100123409-1960 000025263060000526 AMERICAN
62201100123409-1960 000024188960000526 AMERICAN
62201100123409-1960 000025960260000526 AMERICAN
62201100123409-1960 000029390060000526 AMERICAN
62201100123409-1960 000028661060000526 AMERICAN
705RMR*11*379477306771000*0000133379\
820000018601023114390000000000000000139365873043002117
9000001000020000001860102311439000000000000000013936587
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
$
$
$ awk '
> (/^5/ || /^7/) && t {printf("%s%010d%s\n",substr(s,1,20),t,substr(s,31));t=0}
> /^6/ {t+=substr($0,21,10);s=$0;next}
> {print}' file
101 3234344443433230430021170810060810A094101CITIZEN AMERICAN
5200AMERICAN 144892 4333332117CCDOCT06 2008
62201100123409-1960 000167231560000526 AMERICAN
705RMR*11*379426274762004*0000852630\
705RMR*11*378531408781001*0000259602\
705RMR*11*378538061761006*0000093900\
705RMR*11*379424325743008*0000286610\
62201100123409-1960 000013337960000526 AMERICAN EXP
5200AMERICAND 144892 3232302117CCDOCT07 2008
62201100123409-1960 000157231560000526 AMERICAN
705RMR*11*379477306771000*0000133379\
820000018601023114390000000000000000139365873043002117
9000001000020000001860102311439000000000000000013936587
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999$
$

Regards
# 20  
Old 11-04-2008
Hi
Thanks very much for ur help.The line in red should not be there as there should be only one 6 line for every 5 line. so the total should include to that total too. Pls help

101 3234344443433230430021170810060810A094101CITIZEN AMERICAN
5200AMERICAN 144892 4333332117CCDOCT06 2008
62201100123409-1960 000167231560000526 AMERICAN
705RMR*11*379426274762004*0000852630\
705RMR*11*378531408781001*0000259602\
705RMR*11*378538061761006*0000093900\
705RMR*11*379424325743008*0000286610\
62201100123409-1960 000013337960000526 AMERICAN EXP
5200AMERICAND 144892 3232302117CCDOCT07 2008
62201100123409-1960 000157231560000526 AMERICAN
705RMR*11*379477306771000*0000133379\
820000018601023114390000000000000000139365873043002117
9000001000020000001860102311439000000000000000013936587
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999$
# 21  
Old 11-04-2008
Could you please help

Thanks

Last edited by appsguy616; 11-04-2008 at 05:11 PM..
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