Counting a string between 2 strings...

I have been working on this for a little while and have been unable to come to a solution. Any help would be appreciated. I am working on a UNIX workstation and have a 30-40 meg text file that I am working with. In my real file there is hundreds of Jobs. Example of input file;

misc logging data
Job 1 start
Task start
Task stop
Task start
Task stop
Job 1 stop
Other misc logging data
Job 2 start
Task start
Task stop
Job 2 stop
Other misc logging data
Job 3 start
Task start
Task stop
Task start
Task stop
Task start
task stop
Job 3 stop

My desired output is:

Job 1, 2 Tasks
Job 2, 1 Tasks
Job 3, 3 Tasks

Thanks again...

With what you have shown us, the following simple awk script seems to do what you want. Since you sometimes have "Task" and sometimes have "task", this script also recognizes "Job" and "job" as synonyms and "Stop" and "stop" as synonyms:

awk '
$1 ~ "[Tt]ask" && $2 ~ "[Ss]top" {t++;next}
$1 ~ "[Jj]ob" && $3 ~ "[Ss]top" {printf("Job %s, %d Tasks\n", $2, t);t=0}
' input

If you try this on a Solaris/SunOS system, use /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk instead of awk .

1 Like