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
# 22  
Old 11-04-2008
Could you help please.

Thanks
# 23  
Old 11-05-2008
help please

Thanks
# 24  
Old 11-05-2008
Please don't bump up your post, read the rules.

If you want to totalize this line with the previous series you should use another approach.
To totalize this line with the next series the code should be:

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

# 25  
Old 11-05-2008
Hi
Sorry abt the post. The line in red is not being included in the total of first group 6 lines. It should be processed as first group total as it is before second 5 line. In the new script it is not including this line. Could u please help. Thanks very much.

In ur script it is including this line in the 2nd group total but it should be in the first group total. I want to totalize with the previous series as it is occuring before 2nd 5 group line.

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$
# 26  
Old 11-05-2008
New question: Get fields from line 90 and replace the line 6 field:

Hi
Can you please let me know how to do with the previous series approach. I also have another question. Didn't knew if this should be a new thread? If I want to to pick a field from the line starting with 90 and replace field at position 21-30 on first line 6 and delete remaining 6 lines for that 5 group line. This should happen for each 5 group lines.

The fields in red should be replaced with the Bold on line 6

101 3234344443433230430021170810060810A094101CITIZEN AMERICAN
5200AMERICAN 144892 4333332117CCDOCT06 2008
62201100123409-1960 000013768460000526 AMERICAN
62201100123409-1960 000085263060000526 AMERICAN
705RMR*11*379426274762004*0000852630\
705RMR*11*378531408781001*0000259602\
705RMR*11*378538061761006*0000093900\
705RMR*11*379424325743008*0000286610\
62201100123409-1960 000013337960000526 AMERICAN EXP
820000018601023114390000000000000000139365873043002117
9000001000020000001440102311439000000000000000013936587
5200AMERICAND 144892 3232302117CCDOCT07 2008
62201100123409-1960 000023768460000526 AMERICAN
62201100123409-1960 000025263060000526 AMERICAN
705RMR*11*379477306771000*0000133379\
62201100123409-1960 000024188960000526 AMERICAN
820000018601023114390000000000000000139365873043002117
90000010000200000018601023114390000000000000000143936587
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

so the required output will be

101 3234344443433230430021170810060810A094101CITIZEN AMERICAN
5200AMERICAN 144892 4333332117CCDOCT06 2008
62201100123409-1960 013936560000526 AMERICAN
705RMR*11*379426274762004*0000852630\
705RMR*11*378531408781001*0000259602\
705RMR*11*378538061761006*0000093900\
705RMR*11*379424325743008*0000286610\
820000018601023114390000000000000000139365873043002117
9000001000020000001440102311439000000000000000013936587
5200AMERICAND 144892 3232302117CCDOCT07 2008
62201100123409-1960 014393660000526 AMERICAN
705RMR*11*379477306771000*0000133379\
820000018601023114390000000000000000139365873043002117
90000010000200000018601023114390000000000000000143936587
# 27  
Old 11-05-2008
Hi
Could you help please. I know i am not supposed to bump up the post but i am in a urgent situation.
# 28  
Old 11-05-2008
help please.
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