[comp.unix.questions] CAN AWK KEEP COUNT

vwa0201@marst2 (Larry Baca) (03/22/91)

In one portion of a script I have written (csh), I need to keep a running total
of certain fields within a record found by grep. My grep statement looks
something like this:

grep $pattern $infile | tee $outfile | cut -c49-100 | awk ......

My problem is in awk, I want to feed "awk" cols 49-100 of the greped record. 

The "cut" record consists of 5 numeric fields of 10, 12, 10, 10, and 10 cols 
in length, no spaces. 

I want awk to keep a running total for each field and spit the grand totals 
out at the end. As you can see I am also writing the entire record to $outfile.

I would also like "awk" to make the grand totals available to the rest of
the script.

I might add the file I am working with is around 900K 100 col rcds, so maybe
awk isn't the way to go.
-- 
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
LARRY BACA,                                                       marst2!lbaca
DAASO-VWA AIS, DEFENSE AUTOMATIC ADDRESSING OFFICE, WESTERN DIVISION
DDTC TRACY, TRACY CA. 95376-5057  AUTOVON 462-9391  COMERCIAL 832-9391

jik@athena.mit.edu (Jonathan I. Kamens) (03/22/91)

First of all, you don't have to use cut to pass awk the 49th through 100th
columns of the data; awk can figure out what to use itself.  Second, awk is
capable of both splitting strings at specified columns (using substr) and of
keeping a running total.  Something like this:

awk 'BEGIN {
	column1 = column2 = column3 = column4 = column5 = 0;
}
{
	column1 += substr($0,49,10);
	column2 += substr($0,59,12);
	column3 += substr($0,71,10);
	column4 += substr($0,81,10);
	column5 += substr($0,91,10);
}
END {
	printf("%d %d %d %d %d\n", column1, column2, column3, column4, column5);
}'

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

P.S. By the way, both substr and the math capabilities of awk are among its
basic functions that are documented in the man page.  If you needed to ask on
the net about them, I think perhaps you need to do a little bit more
documentation reading.

]) (03/23/91)

In article <356@marst2> vwa0201@marst2 (Larry Baca) writes:
>In one portion of a script I have written (csh), I need to keep a running total
>of certain fields within a record found by grep. My grep statement looks
>something like this:
>
>grep $pattern $infile | tee $outfile | cut -c49-100 | awk ......
>
>My problem is in awk, I want to feed "awk" cols 49-100 of the greped record. 
>
>The "cut" record consists of 5 numeric fields of 10, 12, 10, 10, and 10 cols 
>in length, no spaces. 
>
>I want awk to keep a running total for each field and spit the grand totals 
>out at the end. As you can see I am also writing the entire record to $outfile.
>
>I would also like "awk" to make the grand totals available to the rest of
>the script.
>
>I might add the file I am working with is around 900K 100 col rcds, so maybe
>awk isn't the way to go.

Well, I'd be approaching it like:

-- getstuff.sh --
:
# snag pattern $1 from file $2, write it to $3, and set the totals in
# shell variables $1 through $5.

pattern="$1"
infile="$2"
outfile="$3"

# test values assigned above, then....

#
# grep's still faster as our sieve that to ask awk to filter infile...
#
set -- `grep "$pattern" "$infile" |
	awk '		{
			print > outfile
			tot[1] += substr($0, 49, 10)
			tot[2] += substr($0, 59, 12)
			tot[3] += substr($0, 71, 10)
			tot[4] += substr($0, 81, 10)
			tot[5] += substr($0, 91, 10)
			}
		END	{
			close(outfile)
			print tot[1]+0, tot[2]+0, tot[3]+0, tot[4]+0, tot[5]+0
			}' \
		outfile="$outfile" -`

for i in 1 2 3 4 5
do
	eval "echo \"Total $i: \$$i\""
done
-- getstuff.sh --

...Kris
-- 
Kristopher Stephens, | (408-746-6047) | krs@uts.amdahl.com | KC6DFS
Amdahl Corporation   |                |                    |
     [The opinions expressed above are mine, solely, and do not    ]
     [necessarily reflect the opinions or policies of Amdahl Corp. ]