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!

Need a help for awk 1

Status
Not open for further replies.

ndon

IS-IT--Management
Aug 7, 2003
7
FR
Hello,

I need to write a script in awk to check the total in my ascii file.

Thanks for any help

Example of my file :

967 ./export_cdc/20060326-0000106124-12
967 ./export_cdc/20060326-0000106127-12
----
xxx Total

968 ./exp_courbe_jour.old/20040404-0000102140-11
968 ./exp_courbe_jour.old/20040404-0000102140-11
----
xxx Total

972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
-----
xxx Total

973 ./export_cdc/20040101-0000104552-11
973 ./export_cdc/20040101-0000104555-11
973 ./export_cdc/20040101-0000104518-11
-----
xxx Total
 
Try this:

Code:
awk '
{print}
$2 == "Total" {
        if (tot==$1) print "Total is correct"
        else print "Total should be " tot
        tot=0
}
$1 ~ /^[0-9]+$/ { tot+=$1 }
' inputfile

Annihilannic.
 
grep Total < filename | awk '{s += $1}END {print s}'

Should do it

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Sorry mis-understood your requirement; must learn to read!

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
I am sorry for my bad english. My text file like this one :

967 ./export_cdc/20060326-0000106124-12
967 ./export_cdc/20060326-0000106127-12
968 ./exp_courbe_jour.old/20040404-0000102140-11
968 ./exp_courbe_jour.old/20040404-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
973 ./export_cdc/20040101-0000104552-11
973 ./export_cdc/20040101-0000104555-11
973 ./export_cdc/20040101-0000104518-11
 
And the expected result ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
This is my expected result. Il like to get the total.

Thanks


967 ./export_cdc/20060326-0000106124-12
967 ./export_cdc/20060326-0000106127-12
----
xxx Total

968 ./exp_courbe_jour.old/20040404-0000102140-11
968 ./exp_courbe_jour.old/20040404-0000102140-11
----
xxx Total

 
With your posted samples, what is the expected value(s) of xxx ?
BTW, what have you tried so far ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
I guess xxx is a line count, so here a starting point:
awk 'NR>1 && f!=$1{print t,"Total\n";t=0}{++t;f=$1;print}END{print t,"Total"}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Do you mean total of

967 + 967 = 1934

968 + 967 = 1936

or

967 lines = 2
968 lines = 2
972 lines = 6
973 lines = 3

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant."

 
Hi mrn,

Yes i mean total like "967 + 967 = 1934".

I have do to an Excel table with this results (size/type of file). There are 2 millions files in my jfs2 Unix file system.

Thanks again
 
You wanted something like this ?
awk 'NR>1 && f!=$1{print t,"Total\n";t=0}{t+=$1;f=$1;print}END{print t,"Total"}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
So in other words you want to calculate the totals for each directory?

Annihilannic.
 
Non Annihilannic,

Each directiry have included thousand of XML files ! For exemple :
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
972 ./exp_courbe_jour.old/20040403-0000102140-11
====
5832 Total


Thanks
 


PHV,
I can try your solution tomorrow at the office. It's 11.15 p.m here.

Thanks
 
I guess we're on the same time zone.
Bonne nuit.
 
Hi PHV,

It's works for me. Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top