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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to show first 0 using seq or +1 count?
# 1  
Old 02-28-2012
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.

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?
# 2  
Old 02-28-2012
Try:
Code:
printf "%s %02d:\n" "$DATE" "$i"

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-28-2012
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 Smilie

Last edited by Sekullos; 02-28-2012 at 12:49 PM..
# 4  
Old 02-28-2012
Hi Sekullos, I changed the echo to printf in your script and got this:
Code:
$ 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?
# 5  
Old 02-28-2012
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 :

Code:
######################
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

# 6  
Old 02-28-2012
Capture the output of printf for your string then.

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

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

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-28-2012
Quickly flung together :
Code:
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


Last edited by Scrutinizer; 02-29-2012 at 03:09 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Easy seq Question

Hi! I'm trying to do this: 1 - 2 - 3 - 4 - 5 - I'm using seq for this: seq 1 20 > filename.txt How do I get the "-"? I've tried -f per man but can't get anything to work. Also, is there an easier or better way than using sequence? Thanks! (6 Replies)
Discussion started by: TonyBe
6 Replies

2. Shell Programming and Scripting

awk command to find seq lines

I have a below file FILE.cfg JAN_01 VAR1=4 VAR2=SUM VAR3=PRIVATE JAN_10 VAR1=44 VAR2=GUN VAR3=NATUR JAN_20 VAR1=3 VAR2=TQN VAR3=COMMA code: (JAN_10 is argument passed from script) (6 Replies)
Discussion started by: Roozo
6 Replies

3. UNIX for Dummies Questions & Answers

RNA-seq analysis

I am processing RNA-seq data files that have been aligned using RUM. One of the output files is a *.sam that includes: Unique alignments Non-unique alignments original read files I want to extract only the unique alignments by pulling out alignments that have "IH:i:1" (indicates this read... (2 Replies)
Discussion started by: genGirl23
2 Replies

4. Shell Programming and Scripting

Using backspace in echo to show process count

i have something like this tablesName="abc def hij akn ... etc etc" count=0 for i in $tablesName do echo -en "\b\b\b\b\b\b\b\b\b\b\b\b\bTableCount: $count" count=`expr $count + 1` done the above is just a description wha i need is when the loop executes the... (1 Reply)
Discussion started by: vivek d r
1 Replies

5. Shell Programming and Scripting

compare 2 files and show count same content.

$ cat File1 Non HTTP response code:java.net.ConnectException225073X 000000005143329Load time: 402335410224 Non HTTP response code: ava.net.ConnectException206423X 000000005143330Load time: 402305687161 Non HTTP response code: ava.net.ConnectException290212X 000000005143331Load time:... (1 Reply)
Discussion started by: ooilinlove
1 Replies

6. UNIX for Dummies Questions & Answers

Help with seq (print a series of dates)

Assuming one does not have such luxuries as bash, zsh, jot, rs, perl, etc. what is the most elegant way to print out a formatted date series like this: 01-01-2010 01-02-2010 01-03-2010 ... 02-01-2010 02-02-2010 ... Can I accomplish this with just basic shell builtins and seq, or... (3 Replies)
Discussion started by: uiop44
3 Replies

7. Shell Programming and Scripting

Using seq (Or alternative)

I usually just browse the forum/google for answers, however I've been stuck on a problem for a number of hours now and I've decided to join up and actually ask I've searched the forum ad naseum in an attempt to find answer to my query, however so far I have been unsuccessful. I'm no expert... (3 Replies)
Discussion started by: gtc
3 Replies

8. Shell Programming and Scripting

Using Seq As A Variable With Padded Digits

Hi all. Im trying to use a sequence in a while loop like this below. I need it for navigating a year, month, day folder structure where a user can input the start date and have it go to the desired end date. The script will grab a certain file on each day then move onto the next. Ive got all that... (3 Replies)
Discussion started by: Grizzly
3 Replies

9. Shell Programming and Scripting

declaring variable with for $(seq)

Hi guys. i have the following script: 1 #!/bin/bash 2 linkcount=$(grep "/portal" tickets | wc -l) 3 grep "/portal" tickets > links 4 for i in $(seq 1 $linkcount); do 5 echo "BLYAT" 6 let link$i=$(sed -n "$i"p links) 7 echo $ 8 done the problem is, that "let" can`t... (1 Reply)
Discussion started by: neverhood
1 Replies

10. Shell Programming and Scripting

script to loop and check jumping seq.

Hi, Normally, I will manually to use "ll" command to list the following file from \FILE\CACHE\ directory and check the jump seq. Can I write a script to loop or/and check jump seq file (if jumped seq and show "missing seq no" message for me) -rw-rw----+ 1 user develop 14012 Sep 4... (1 Reply)
Discussion started by: happyv
1 Replies
Login or Register to Ask a Question