Skip to content
Advertisement

Using awk command to go through a text file and incrementing counters for morning, afternoon, and night sections of the script

I am using awk to go through a text file that has information of finished scripts then says complete (morning) then informaton of finished scripts then says complete (afternoon) then information of finished scripts then says complete (night).

I am trying to keep track of the finished scripts for each block.

The way I am doing it is…

JavaScript

However the values I get for morn, after, and night is the same.

JavaScript

so I would want 3, 2, and 1 as my output – finishes for (morning, afternoon, and night)

Advertisement

Answer

This should work

JavaScript

BEGIN block executes at the start of the script. The split just creates an array like

JavaScript

x is incremented every time Finished is seen
When completed is seen, y is incremented and value from that position in a is printed with the value in x.
x is reset
repeat


Output from your example

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement