How to show first 0 using seq or +1 count?

Greetings,

Using linux based OS and KSH.

I m trying to make a simple script to parse some logs to show a count per hour on a specific alarm starting from midnight to the current hour.

So I format my "HOUR" variable to show the current time and so I can use it in the following bit of code.

DATE=$(date +"%b %d")
HOUR=$(date +"%H")
 for i in $(seq 01 $HOUR); do echo "$DATE $HOUR:"; done

Sadly, seq 01 24 (for exemple) will not show 01: 02: but 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:

I tried using a while loop adding a +1 to my HOUR varible but same problem (numerical, won't show first 0)

What would you do?

Try:

printf "%s %02d:\n" "$DATE" "$i" 
1 Like

Sweet works perfectly to show 0 on that $i variable using printf but I m still not able to use that $i to grep the current date.
The output would be like this : "grep Feb 28 1:" instead of "grep Feb 28 01:"

Gonna man those printf options anyway looks interesting :slight_smile:

Hi Sekullos, I changed the echo to printf in your script and got this:

$ for i in $(seq 0 $HOUR); do printf "%s %02d:\n" "$DATE" "$i"; done
Feb 28 00:
Feb 28 01:
Feb 28 02:
Feb 28 03:
Feb 28 04:
Feb 28 05:
Feb 28 06:
Feb 28 07:
Feb 28 08:
Feb 28 09:
Feb 28 10:
Feb 28 11:
Feb 28 12:
Feb 28 13:
Feb 28 14:
Feb 28 15:
Feb 28 16:
Feb 28 17:
Feb 28 18:

Is that not what you are after?

yep yep it works fine to show the timestamp correctly but after that i wish to do a grep on my log file counting XX alarms like so :

for i in $(seq 0 $HOUR); do printf "%s %02d:\n" "$DATE" "$i"; printf $(grep "$DATE $i:" $LOGFILE | grep -i alarm_type1 | wc-l); done

but if i do this, the grep line would use the $i variable as 1 instead of 01.
My logfile timestamp being "Feb 28 HH:MM:SS" It would grep nothing on "Feb 28 1:"

I wanted the following output :

######################
Alarm count on XXX alarm name
######################

Feb 28 00: 0
Feb 28 01: 0
Feb 28 02: 0
Feb 28 03: 5
Feb 28 04: 6
Feb 28 05: 10
Feb 28 06: 0
Feb 28 07: 90
Feb 28 08: 37000
Feb 28 09: 0 
Feb 28 10: 0 
Feb 28 11: 0

Capture the output of printf for your string then.

You can also avoid the wc -l by using grep -c.

D=$(printf "%s %02d\n" "$DATE") ; printf "%s: %d" $D $(grep "$D" $LOGFILE | grep -c -i alarm_type1)
1 Like

Quickly flung together :

for i in $(seq 0 $(date +"%H")); do printf "%s %02d:\n" "$(date +"%b %d")" "$i"; done | grep -f- "$LOGFILE" | grep -i alarm_type1 | wc-l
1 Like