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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Report formatting query

Status
Not open for further replies.

ranjit

Technical User
Apr 14, 2000
131
GB
I have an input file (refer to extract below) which I am
attempting to convert into an output file as set-out below.

Is this a task suitable for awk to work on? I have attempted
using an approach which searches for the expression "pool"
and gets the subsequent TAPE lines - counting each occurance
under the respective pool heading.

I've ran into problems and do not get the desired result - in particular the "zero" tape occurances are proving problematic to work around.

Can any one help? Help would be appreciated.

Thanks in advance.


=====================================
INPUT DATA FILE:

TAPE SILO MEDIA INVENTORY
---------------------------------

SALES_BACKUP pool

TAPE0050
TAPE0059
TAPE0072

DATABASE_LOG pool

FINANCE pool

TAPE0426
TAPE0021

SCRATCH pool

TAPE0153
TAPE0155
TAPE0159
TAPE0162

DEVELOPMENT pool

TEST pool

====================================

DESIRED OUTPUT FILE:

SALES_BACKUP pool: 3
DATABASE_LOG pool: 0
FINANCE pool: 2
SCRATCH pool: 4
DEVELOPMENT pool: 0
TEST pool: 0

====================================
 
I haven't tested this - and I have to confess that it is unusual for my scripts to run straight out of the box - but something like this ought to work (where "input.file" is the name of your data file):

BEGIN {
tcount = 0
while ((getline < &quot;input.file&quot;) > 0) {
if ($0 ~ /pool$/) { # lines ending with 'pool'
if (pcount != &quot;&quot;) {
printf(&quot;%6s&quot;,tcount&quot;\n&quot;)
tcount = 0
}
pcount++
printf(&quot;%-20s&quot;,$0)
}
else if ($0 != &quot;&quot;) {
tcount++
}
}
close(&quot;input.file&quot;)
} # --------------------close BEGIN

You will have to do something about the title line - this will confuse the program. You may also want to tidy-up the printf statement. If you need any help, I can thoroughly recomend the excellent Gawk info file which can be found all over the place on the net but you might like to look at:


a very comprehensive document with lots of worked examples.
 
Some tips I have learned on this forum and CLA..

1) If you are going to use a BEGIN action for this
and load the file without invoking gawks powerful
main() defined behavior then it is best to load the
file into an array and do your processing:

function processIt(arr,c,max, u,rec) {
if (arr[c] ~ /.*pool/) {
u = c + 1
while (arr !~ /.*pool/ && u <= max) {
if (arr ~ /TAPE.*/) {
rec++
}
u++
}
return &quot;Matches for &quot; arr[c] &quot;=&quot; rec
}
return &quot;NOMATCH&quot;
}

BEGIN {
fname = ARGV[1]
cnt = 1
while ((getline < fname) > 0) {
array[cnt++] = $0
}
close(fname)

for (i=1 ; i <= cnt ; i++) {
print processIt(array,i,cnt)
}
}

OUTPUT:
Matches for SALES_BACKUP pool=3
Matches for DATABASE_LOG pool=
Matches for FINANCE pool=2
Matches for SCRATCH pool=4
Matches for DEVELOPMENT pool=
Matches for TEST pool=


real 0m0.025s
user 0m0.000s
sys 0m0.010s

2) OTOH there are literally half a hundred examples
of sequential searches through a file using gawk/nawks
defined main() behavior in situations like this if you
search the forum archives. The only time you really need
to use the BEGIN action for a search is when you need
the data searched in a non-sequential manner.
Search for Cakiwi especially on this as his examples
on this topic are numerous and functional.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top