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

Count pattern occurances per line? 2

Status
Not open for further replies.

octar

Technical User
Oct 21, 2002
28
AU
Hi, I have the following file:

DRIVE10 DRIVE1 DRIVE2 DRIVE3 DRIVE4 DRIVE5 DRIVE6 DRIVE7 DRIVE8 DRIVE9 Date
T50253 T30544 T30624 T10164 T50245 T50252 T10276 T10287 T10402 T50197 200501100000
T50253 T30559 T50245 T50252 T10276 T10287 T10402 200501100010
...
...

note: this list what tape is located in what drive at 10 minute intervals.

What I need from this is to summarize the results into how many tape types are mounted at the point in time.

eg.

Date 3590 3592
200501100000 6 4
200501100010 4 3

To get this data all tapes starting with T5 are of type 3592 and tapes starting with T1,T2,T3 are of type 3590

I know this is probably a simple one line piece of code but I can't get it working... any suggestions?
 
A starting point:
awk '
BEGIN {print "Date 3590 3592"}
NR>1{
a=0;b=0
for(i=1;i<NF;++i){a+=($i~/^T[123]/);b+=($i~/^T5/)}
print $NF,a,b
}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Code:
BEGIN { fmt="%-12s %4s %4s\n"
  printf fmt, "Date", "3590","3592" }

1==NR { next }

NF \
{ type[0] = type[1] = 0
  for ( i=1; i<NF; i++ )
    type[ match($i, /^T5/) ]++
  printf fmt, $NF, type[0], type[1]
}

Let me know whether or not this helps.

If you have nawk, use it instead of awk because on some systems awk is very old and lacks many useful features. Under Solaris, use /usr/xpg4/bin/awk.

For an introduction to Awk, see faq271-5564.

 
futurelet,
nice use of 'match' in arrays!

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top