Help with Display Shell Script for date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Display Shell Script for date
# 1  
Old 05-14-2012
Help with Display Shell Script for date

hi friends,
I am working on extracting data from sar logs for analysis.

What is want to do is insert the date. The way i am doing is incrementing the date if i see the hour gets reduced (i.e. from 23 hours to 00 hours). Somehow the script which i have made is not able to handle the logic w.r.t 0.
Can you please suggest where i am going wrong /provide alternate scripting suggestion?

Below is the source file
Code:
> cat tt1
00:52:32,disk2000,0.01,0.50,0,0,0.00,1.31
00:52:32,disk2001,0.00,0.50,0,0,0.00,0.53
00:52:32,disk2003,0.00,0.50,0,0,0.00,0.78
00:52:32,disk2008,0.02,0.50,0,0,0.00,5.85
01:01:59,disk30,2.57,14.58,8,125,38.78,11.19
01:01:59,disk50,2.96,13.54,9,131,36.71,11.47
01:01:59,disk745,0.00,0.50,0,0,0.00,0.66
01:01:59,disk746,0.02,0.50,0,0,0.00,10.58
01:01:59,disk747,0.02,0.50,0,0,0.00,9.47



What I want is below output:

Code:
09-05-12 00:52:32,disk2000,0.01,0.50,0,0,0.00,1.31
09-05-12 00:52:32,disk2001,0.00,0.50,0,0,0.00,0.53
09-05-12 00:52:32,disk2003,0.00,0.50,0,0,0.00,0.78
09-05-12 00:52:32,disk2008,0.02,0.50,0,0,0.00,5.85
10-05-12 01:01:59,disk30,2.57,14.58,8,125,38.78,11.19
10-05-12 01:01:59,disk50,2.96,13.54,9,131,36.71,11.47
10-05-12 01:01:59,disk745,0.00,0.50,0,0,0.00,0.66
10-05-12 01:01:59,disk746,0.02,0.50,0,0,0.00,10.58
10-05-12 01:01:59,disk747,0.02,0.50,0,0,0.00,9.47



What i am getting is:

Code:
> ksh a3
10-05-12 00:52:32,disk2000,0.01,0.50,0,0,0.00,1.31
10-05-12 00:52:32,disk2001,0.00,0.50,0,0,0.00,0.53
10-05-12 00:52:32,disk2003,0.00,0.50,0,0,0.00,0.78
10-05-12 00:52:32,disk2008,0.02,0.50,0,0,0.00,5.85
09-05-12 01:01:59,disk30,2.57,14.58,8,125,38.78,11.19
09-05-12 01:01:59,disk50,2.96,13.54,9,131,36.71,11.47
09-05-12 01:01:59,disk745,0.00,0.50,0,0,0.00,0.66
09-05-12 01:01:59,disk746,0.02,0.50,0,0,0.00,10.58
09-05-12 01:01:59,disk747,0.02,0.50,0,0,0.00,9.47



The content of my script is:

Code:
> cat a3
#!/usr/bin/ksh
d='05/09/12'
mm=${d%??????}
yy=${d#??????}
dd1=${d%???}
ddx=${dd1#???}
awk -v dd="$ddx" -v mm="$mm" -v yy="$yy" -F":" '{a=$1;} {print ($1 < $a && $a !=0)   ?  dd + 1 "-" mm "-" yy " " $0 : dd "-" mm "-" yy " " $0}'  tt1

I know my logic to input the date in the output is dirty , but atleast it should get me started Smilie..

Thanks,
Kunwar
# 2  
Old 05-15-2012
Try this:
Code:
awk -v dd="$ddx" -v mm="$mm" -v yy="$yy" -F":" '{print ((a&&$1<a)?++dd:dd) "-" mm "-" yy " " $0} {a=$1}'  tt1

Question: What happens on the 28th Feb?
# 3  
Old 05-15-2012
Thanks Chuber it works.

---------- Post updated at 11:22 PM ---------- Previous update was at 11:14 PM ----------

hi Chuber,
Regarding 28th Feb i am still thinking..
One question (a&&$1<a) i didnt understand the functioning of 'a' before the '&&' can u plz explain. If it is 1 (meaning true) then it is obvious , but if it is any other value how does it still make it true?

Last edited by kunwar; 05-15-2012 at 01:18 AM.. Reason: I didnt check it correctly.
# 4  
Old 05-15-2012
any non-zero/null value equates to true, so this test can be thought of as does a exist and is greater than zero.
Which comming to think of it will stop 0 flagging a change of date - ($1<a) is probably best here, as no hour should have a value less than 0 anyway.

another safe test might be (length(a)&&$1<a)
This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Compare Date to today's date in shell script

Hi Community! Following on from this code in another thread: #!/bin/bash file_string=`/bin/cat date.txt | /usr/bin/awk '{print $5,$4,$7,$6,$8}'` file_date=`/bin/date -d "$file_string"` file_epoch=`/bin/date -d "$file_string" +%s` now_epoch=`/bin/date +%s` if then #let... (2 Replies)
Discussion started by: Greenage
2 Replies

2. Linux

How to calculate the quarter end date according to the current date in shell script?

Hi, My question is how to calculate the quarter end date according to the current date in shell script? (2 Replies)
Discussion started by: Divya_1234
2 Replies

3. Shell Programming and Scripting

Shell script to compare two files of todays date and yesterday's date

hi all, How to compare two files whether they are same are not...? like i had my input files as 20141201_file.txt and 20141130_file2.txt how to compare the above files based on date .. like todays file and yesterdays file...? (4 Replies)
Discussion started by: hemanthsaikumar
4 Replies

4. UNIX for Dummies Questions & Answers

How to write a shell script to Run it the from Date A to Date B?

Hi , How would i write a shell script to run the code from one date to another date EXample 2014-01-01 to 2014-02-28, can i any provide some clue on this (4 Replies)
Discussion started by: vikatakavi
4 Replies

5. Shell Programming and Scripting

Need help in Shell Script comparing todays date with Yesterday date from Sysdate

Hi, I want to compare today's date(DDMMYYYY) with yesterday(DDMMYYYY) from system date,if (today month = yesterday month) then execute alter query else do nothing. The above requirement i want in Shell script(KSH)... Can any one please help me? Double post, continued here. (0 Replies)
Discussion started by: kumarmsk1331
0 Replies

6. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

7. Shell Programming and Scripting

Past date display ( today - N) in shell

Hi, Any idea to get display of date - n ( n=1,2,3,4 etc) ? For example , dtoudt will easily execute by dtoudt -3 and result is >dtoudt -3 1234852529 Date 2009-2-17 time 14:35:29 day 47 How to get it display in 20090217 instead ? Maybe you have other suggestion to display date... (0 Replies)
Discussion started by: rauphelhunter
0 Replies

8. Shell Programming and Scripting

Specify a previous date as start date in shell script

Hi, I am trying to modify a script which accepts date in format dd/mm/yy. I am trying to modify the script so that it retrieves the date that was 15 days earlier from today as start date. Eg.if today is 05/09/2006, the script should retrieve 21/08/2006 as start date. Is there any script/code to... (2 Replies)
Discussion started by: ritzwan0
2 Replies

9. UNIX for Dummies Questions & Answers

Shell Script Display?

I remember learning that there is a way to make a shell script display the script itself to standard output while the script is being executed but I can't find how to do that. Any pointers? (2 Replies)
Discussion started by: wmosley2
2 Replies

10. UNIX for Dummies Questions & Answers

Script to display last login date/time?

I was wondering if anyone had a script that would display the last time a user logged into a particular machine. I know about the "last" command, but it gives too much info.... I just wanted to know the last time a user used his/her id. ANy help would be greatly appreciated. Ryan (3 Replies)
Discussion started by: ryaneverett5
3 Replies
Login or Register to Ask a Question