LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 07-15-2010, 04:55 AM   #16
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15

one more question..

i wonder if is it possible if the date can be set as : 11 Jul 2010

based on script below,

datetime=${msg##*ran on } # remove "ran on " and all proceeding
datetime=${datetime%% SST *} # remove " SST " and all following
time=${datetime##* } # remove last space and all proceeding
date=${datetime% *} # remove last space and all following

the output is : "Tue Jul 11"

is there any other easier way to make the date as 11 Jul 2010 instead of i need to do step by step and combine the all the variable for example : echo "$day" "$month" "$year" ??

in addition, can someone guide me on how to eliminate day in front of the month " Tue Jul 11" as the day is changing from monday-sunday..

thanks.
 
Old 07-15-2010, 06:23 AM   #17
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,203

Rep: Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797
This should get you going:
Code:
date -d "Tue Jul 11 23:15:00 2010" "+%d %b %Y"
The only thing removed here is SST from date and time portion of string
 
Old 07-15-2010, 07:34 PM   #18
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
Well looks like it's pretty close now, what you want to do is get rid of the " is" on the end, and "ERROR: " on the font (right ?).

So replace:
Code:
name=${msg%% Failed *}
with
Code:
name=${msg%% is Failed *}
name=${name#ERROR: }
See, we are deleting from " is Failed " to the end and then removing "ERROR: " from the front.

To swap your date around use:

Code:
date=$(date -d "$date" "+%d %b %Y")
It's unfortunate that the variable is called "date" as it makes the above hard to read if your variable was renamed to logdate it would read better:

Code:
logdate=$(date -d "$logdate" "+%d %b %Y")
Using the "date -d" command grail suggested to reformat the string.
 
1 members found this post helpful.
Old 07-15-2010, 08:11 PM   #19
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
sorry,my bad..

the input from test.log are:

if [success]: report_name successfully ran on Tue Jul 11 23:15:00 SST 2010.

if [failed]: ERROR: report_name is Failed on Tue Jul 11 23:15:00 SST 2010. ErrorCode : 500


the script should be like this:

#!/bin/sh
dir=/home/user/input/test.log

msg=`tail -1 $dir`
name=${msg%% Failed *}


status=Failed
if [ ${#name} -eq ${#msg} ]
then

name=${msg%% successfully *}
status=Success
fi

datetime=${msg##*ran on } # remove "ran on " and all proceeding
datetime=${datetime%% SST *} # remove " SST " and all following
time=${datetime##* } # remove last space and all proceeding
date=${datetime% *}




date=${datetime% *} # remove last space and all following
echo "$msg"
echo "$name"
echo "$status"
echo "$date"


the script output:


if success:

report_name successfully ran on Tue Jul 11 23:15:00 SST 2010.
report_name
Success
23:15:00
Tue Jul 11


if failed:

ERROR: report_name is Failed on Tue Jul 11 23:15:00 SST 2010. ErrorCode : 500

ERROR: report_name is
Failed
23:15:00
Tue Jul 11

BUT the output for name should be only "report_name" instead of "ERROR: report_name is" if failed and for date, i need to make it in dd/mm/yyyy format, thus i need to eliminate the "Tue" (in real situation, the day is changing from monday to sunday everyday)..How could i solve this problem?
 
Old 07-15-2010, 09:43 PM   #20
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,203

Rep: Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797
So how is this for a slightly different attack:
Code:
#!/bin/bash

log=/home/user/input/test.log

OLD_IFS=$IFS
IFS="|"

output($(sed -n -r -e '$ {/\[success\]/s/[^:]*: (.*) succ.*on (.*) SST ([0-9]+).*/\1|success|\2 \3/p;/ERROR/s/.*R: (.*) is.*on (.*) SST ([0-9]+).*/\1|failed|\2 \3/p}' $log))

IFS=$OLD_IFS

echo "Filename is ${output[0]}"
echo "Status is ${output[1]}"
eval echo 'Date is $(date -d "${output[2]}" "+%d %b %Y")'
 
Old 07-15-2010, 10:10 PM   #21
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
i edited the script and the name part works well...Really happy..Thanks!!!
But the date part still got error..

#!/bin/sh
dir=/home/input/test.log

msg=`tail -1 $dir`
type=Report
name=${msg%% is Failed *}
name=${name#ERROR: }

status=Failed
if [ ${#name} -eq ${#msg} ]
then



name=${msg%% successfully *}
status=Success
datetime=${msg##*ran on }

fi
datetime=${msg##* on }
datetime=${datetime%% SST *}
time=${datetime##* }

#date=${date -d "$date" "+%d %b %Y"}
#date=date -d "$datetime" "+%d %b %Y"

date=$(date -d "$date" "+%d %b %Y")
logdate=${date -d "$logdate" "+%d %b %Y"}


#date=${datetime% *}

#date=date -d "$datetime" "+%d %b %Y"


echo "$msg"
#echo "$datetime"
#echo "$name"
#echo "$type"
#echo "$status"
#echo "$time"
echo "$date"
echo "$logdate"


when i run the script, i got this message:

date: illegal option -- d
Usage: date [-u] [+format]
date [-u] [mmddhhmm[[cc]yy]]
date [-a [-]sss.fff]
./test2.sh[28]: logdate=${date -d "$logdate" "+%d %b %Y"}: The specified substitution is not valid for this command.

How could i solve this??
 
Old 07-15-2010, 11:05 PM   #22
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
Looks like your date program dosn't support the -d option it's not POSIX so some flavours of unix dont have it. Here is an updated script that swaps the fields around but month will still be 3 letter name (not month number).

Code:
#!/bin/sh
dir=/home/input/test.log

msg=`tail -1 $dir`
status=Failed
name=${msg%% is Failed *}
name=${name#ERROR: }

if [ ${#name} -eq ${#msg} ]
then
    status=Success
    name=${msg%% successfully *}
fi
df=(${msg##* on })
logdate=${df[2]}-${df[1]}-${df[5]%.}

echo "$msg"
echo "$name"
echo "$status"
echo "$logdate"

Last edited by simon.sweetman; 07-15-2010 at 11:24 PM.
 
1 members found this post helpful.
Old 07-15-2010, 11:24 PM   #23
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
simon, i did try the script but i got the message

./simon.sh[19]: Syntax error at line 19 : `(' is not expected.


then i tried to edit the script;

df={$datetime} or df=${datetime}

but the logdate appear like above..

report_name successfully ran on Tue Jul 11 23:15:00 SST 2010.
report_name
Success
--

Last edited by tedy2808; 07-15-2010 at 11:25 PM.
 
Old 07-16-2010, 12:01 AM   #24
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
Change first line to:
Code:
#!/bin/bash
 
1 members found this post helpful.
Old 07-16-2010, 12:40 AM   #25
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
thanks...it works great!!!

currently i'm working on another step and hoping that you're willing to guide me..
Really appreciate it!

Last edited by tedy2808; 07-16-2010 at 12:45 AM.
 
Old 07-16-2010, 02:11 AM   #26
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
i'm trying to set all day to 2 decimal point so that if the input for date for example

if [success]: report_name successfully ran on Tue Jul _7 23:15:00 SST 2010.
if [success]: report_name successfully ran on Tue Jul 11 23:15:00 SST 2010.

the output for date is :

7 Jul 2010
11 Jul 2010

thus, i wanted the result to be:

07 Jul 2010
11 Jul 2010


so i did the script like this:

if [ ${df[2]} < "10" ]; then

logdate=`echo 0${df[2]}`\ ${df[1]}\ ${df[5]%.}

or

if [x={df[2]}; x <10 ]; then

logdate=echo"0"${df[2]}\ ${df[1]}\ ${df[5]%.}

or

df=(${msg##* on})
if [${df[2]}< 10]; then
$df[2]= 0${df[2]}
fi


but nothing changed..
someone please guide me on this..

* "_" to replace space

thanks

Last edited by tedy2808; 07-16-2010 at 04:41 AM.
 
Old 07-16-2010, 03:22 AM   #27
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
Another problem,
Now i'm facing problem to assign AM and PM in time..

for example the input is

if [success]: report_name successfully ran on Tue Jul 11 00:00:01 SST 2010. (time = 00:00:01)
if [success]: report_name successfully ran on Tue Jul 11 03:15:00 SST 2010. (time = 03:15:00)
if [success]: report_name successfully ran on Tue Jul 11 18:15:00 SST 2010. (time = 18:15:00)
if [success]: report_name successfully ran on Tue Jul 11 24:15:00 SST 2010. (time = 23:15:00)


so, i want the output to be like this:

00:00:01 AM
03:15:00 AM
06:15:00 PM
11:15:00 PM

so,i'm thinking of breaking the time and assign the first 2 digit as hours so that i can compare the value,


hours=${time##* }
hours=${hours%% :*}

if [$hours <=12]; then
time=$time AM

else
time=$time -12 PM

fi

but it doesn't work..anyone pls guide me on how to solve this problem..
Really appreciate your help..i have no idea on this
thanks

Last edited by tedy2808; 07-16-2010 at 03:47 AM.
 
Old 07-16-2010, 04:44 AM   #28
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,203

Rep: Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797Reputation: 1797
Well you would need to do more calculations than just the AM or PM because:

18:15:00 PM <- - this is incorrect

6:15:00 PM

If you then place a zero prior to the 6 that will also be incorrect as 24 hour format does not require / use the AM / PM setting.
 
Old 07-18-2010, 08:36 PM   #29
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
Code:
hours=${time##* }
hours=${hours%%:*}

if [ $hours -le 12 ]
then
    time=${time:0} AM
else
    time="$((hours-12)):${time#*:} PM"
fi
Use -lt -le -gt -ge when doing numeric tests.
"[" and "]" must has space before and after.
AM/PM format should not have zero before hours.

For shell evaluation of equations (like hours - 12) put within $(( and ))
 
Old 07-18-2010, 09:47 PM   #30
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
I managed to solve the 2 decimal place for day but still having problem on assigning the AM and PM..

this is my latest script:

#!/bin/bash
dir=/home/devusr02/input/test.log

msg=`tail -1 $dir`


status=Failed
name=${msg%% is Failed *}
name=${name#ERROR: }


typeset -i dday


if [ ${#name} -eq ${#msg} ]
then

date=${msg##*on }
date=${date%%. *}

status=Success
name=${msg%% successfully *}
datetime=${msg##*ran on }

fi

datetime=${msg##* on }
datetime=${datetime%% SST *}
time=${datetime##* }


hours=${time}
hours=${hours%%:}
dftime=(${hours})

if [[ $dftime[1] -lt 12 ]]
then
new_time="${dftime[1]} $dftime[2] $dftime[3] AM "
else
new_time="expr ${dftime[1]-12} $dftime[2] $dftime[3] PM"
fi


date=${msg##*on }
date=${date%%. ErrorCode *}

df=(${msg##* on})
dday=${dfarray[2]}

if [[ $dday -lt 10 ]]
then
myday="0${df[2]}"
else
myday="${df[2]}"
fi

logdate="${myday} ${df[1]} ${df[5]%.}"

echo "$msg"
echo "$name"
echo "$status"
echo "$new_time"
echo "$logdate"



the output i got is:

./test2.sh: line 35: 23:15:00: syntax error in expression (error token is ":15:00")
./test2.sh: line 37: [[: [1]: syntax error: operand expected (error token is "[1]")

report_name successfully ran on Tue Jul 1 23:15:00 SST 2010.
report_name
Success
12 [2] [3] PM
03 Jul 2010

someone pls help me on this asap..really need your guidance

Last edited by tedy2808; 07-18-2010 at 09:50 PM.
 
  


Reply

Tags
date, day, manipulating, sed, set


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH: if Variable -eq String not working worm5252 Programming 2 01-24-2010 03:07 PM
Get value for variable name defined as string webquinty Programming 4 11-18-2009 05:30 AM
Help: removing a variable substring from a string variable in sh script gnparsons Programming 2 06-04-2008 05:21 PM
variable to string x2000koh Programming 4 07-30-2003 02:23 AM
Getting a variable name based on a string. jtshaw Programming 7 10-08-2002 02:06 PM


All times are GMT -5. The time now is 10:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration