Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Numbering with a Sum...

Status
Not open for further replies.

segment

ISP
Jun 15, 2004
225
US
I have a file with 106 entries on it most look like this:

1 25 34 36 42

I want to be able to number each line (this is easy awk 'NF{$0=++a " :" $0};{print}') but I'd like to add the sum of all numbers at the end so eg the entry would look like (normally awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}')

1: 1 25 34 36 42 138
2: 1 2 3 4 5 15

awk '{s=0; for (i=1; i<=NF; i++) s=s+$i; print s};{s=0; for (i=1; i<=NF; i++) s=s+$i; print s}' didn't seem to work

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Hi

I think I am wrong, as you almost accomplished what I understand cound be your goal.
Code:
awk '{s=0;for(i=1;i<=NF;i++)s=s+$i;print NR": "$0"\t"s}' /input/file

Feherke.
 
grr dur... Almost.. I forgot I was using sed also. The numbers were like this 1-2-3-4-5 so I just sed 's/-/ /g' the file into another one.



perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Hi

Why the [tt]sed[/tt] ? Tell [tt]awk[/tt] to split on dash ( - ) instead of the default [tt]FS[/tt].
Code:
awk [red]-F-[/red] '{s=0;for(i=1;i<=NF;i++)s=s+$i;print NR": "$0"\t"s}' /input/file

Feherke.
 
utter laziness plus the lack to remember much right now :( thanks for the pointer though something I will keep in mind :D

perl -e 'print $i=pack(c5,(40*2),sqrt(7600),(unpack(c,Q)-3+1+3+3-7),oct(104),10,oct(101));'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top