set day to two decimal point in linux

Unitech Pt630d mobile computer, batch, 1...
July 16, 2010 at 02:50:42
Specs: Windows XP, 2gb
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.

example for input is ($msg) :
report_name successfully ran on Tue Jul 13 00: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:

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

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

or
df=(${msg##* on})

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..


See More: set day to two decimal point in linux

Report •


#1
July 16, 2010 at 10:42:41
I have an older version of the bash shell, so I can not use the typeset -Z option. Let me know if you have any questions:

#!/bin/bash
declare -a dfarray # make it an array
typeset -i dday  # make it an integer

msg="report_name successfully ran on Tue Jul 7 00:15:00 SST 2010"

df=${msg##* on}
# set the array
dfarray=( $df )
dday=${dfarray[2]}
if [[ $dday -lt 10 ]]
then
   myday="0${dfarray[2]}"
else
   myday="${dfarray[2]}"
fi

logdate="$myday ${dfarray[1]} ${dfarray[5]}"
echo "$logdate"


Report •

#2
July 18, 2010 at 18:51:54
thanks nail..it really helps! :)

i did edit some of your code based on my understanding..

i need your help again..
below are my script.

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

msg=`tail -1 $dir`

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

#declare -a dfarray
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##* }


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 "$time"
echo "$logdate"

the output will be like this:

[if Failed]

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


is [success]

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


based on my script, the script will read the last input in log file..and the last input is the information that need to be processed to become the output for the script (see output above)..

the input have two possibilities which is failed or success..
if failed, the input will be:
ERROR: report_name is Failed on Tue Jul 11 23:15:00 SST 2010. ErrorCode : 500

if success:

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


Now i'm facing problem to assign AM and PM in time..

for example, let say the possibility for the input are

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..guide me pls

thanks :)


Report •

#3
July 18, 2010 at 19:52:35
this is my latest script for assigning AM and PM..


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


but i encountered error..

# i echoed [ echo "$new_time" ] after the "$status" echo..

the output i received:

./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
01 Jul 2010


correct me please..thanks


Report •

Related Solutions

#4
July 19, 2010 at 10:50:56
First, if you are going to use the expr command to do arithmetic, you can not embed in a string the way you are.
Second, instead of parsing the time string into an array, I choose to use the set command to do the parsing:

#!/bin/bash

msg="if [success]: report_name successfully ran on Tue Jul 11 18:00:01 SST 2010. (time = 18:00:01)"

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

# change : to " " and parse the string into hrs, min, sec
set - $(echo "$ttime"|tr ":" " ")
hrs=$1
min=$2
sec=$3

if [[ $hrs -lt 12 ]]
then
   echo "$hrs $min $sec AM "
else
   newhrs=$(( hrs - 12 ))
   echo "$newhrs $min $sec PM "
fi


Report •

#5
July 19, 2010 at 17:49:39
tanks nails...you're great!


but i wonder if the time is 20:00:25... the output will be 8:00:15 PM ...BUT i need the output to be in 2 decimal places... 08:00:15 PM..


Report •

#6
July 20, 2010 at 03:29:35
hi nails,

I just realized today that actually I have to use normal Bourne shell (bin/sh) and not bash(bin/bash).

Could anyone PLEASE help me to adjust my script.I'm just getting so desperate....

below are my script, input and output example..

#!/bin/bash

dir=/home/input/test.log

msg=`tail -3 $dir`
msg1=`tail -1 $dir`
type=Report

status=Failed

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


typeset -i dday


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

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

status=Success
name=${msg1%% successfully *}

datetime=${msg1##*ran on }
fi


datetime=${msg1##* on }
datetime=${datetime%% SST *}
time_end=${datetime##* }

hours1=(${time_end%:*})
hours2=${hours1%:*}

if [ $hours2 -le 12 ]
then
time_end="${time_end:0} AM"
else
time_end="$((hours2-12)):${time_end#*:} PM"
fi


datetime_start=${msg##* at }
datetime_start=${datetime_start%% SST *}
time_start=${datetime_start##* }

hours3=(${time_start%:*})
hours4=${hours3%:*}

if [ $hours4 -le 12 ]
then
time_start="${time_start:0} AM"
else
time_start="$((hours4-12)):${time_start#*:} PM"
fi

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

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

if [[ $dday -le 10 ]]

then
myday="0${df[2]}"
else
myday="${df[2]}"
fi

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

echo "$name $logdate $time_start $time_end $type $status "

output:

report_name 08 Jul 2010 00:01:00 AM 3:15:00 PM Report Success

input:(test.log in : dir=/home/input)

if input is success:

report_name started at Thu Jul 8 00:01:00 SST 2010.
KL/SSL procedure successfully completed.
report_name successfully ran on Thu Jul 8 15:15:00 SST 2010.

really need your response asap..thanks


Report •

#7
July 20, 2010 at 09:32:54
Sorry, but I really don't have time to rewrite your script for you. But I think you have to rethink your algorithm as Bourne does not support the parameter substitution you have been using:

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

For example, to get the string length in Bourne, use expr:

expr "$name" : '.*'

String parsing in Bourne is not fun. You can use the set command the way I demo'ed before or maybe expr which I don't like.

Personally, if I were doing it, I would use awk, but since this appears to be home work it looks like your tools are limited.


Report •

#8
July 20, 2010 at 12:02:38
For example, the last field has the time string ending with a right parenthesis. This stub uses the tr command to remove the parentheses, print the last field using awk and remove the colons. (I could have done the tr processing within the awk program, but decided to keep it simple). Finish it up by using the set command to parse the hrs, min, and sec:

#!/bin/sh

msg="if [success]: report_name successfully ran on Tue Jul 11 18:00:01 SST 2010. (time = 18:00:01)"
# get rid of the ), print the last field, replace : with a space
ret_str=`echo "$msg"| tr -d ')'|awk ' { print $NF } ' |tr ":" " "`
echo $ret_str
set - `echo "$ret_str"`
hrs=$1
min=$2
sec=$3
echo "hrs is $hrs"
echo "min is $min"
echo "sec is $sec"


Report •

#9
July 20, 2010 at 18:44:25
nails..i really new in linux..just heard about linux a week ago and i need to complete the system asap..when i got to know that i wrongly used bash that supposedly sh, i really stuck..i comfortable with bash and based on your example in sh, i have no idea..really confuse on the expression etc..please help me..i'm so desperate for your help..i'll wait for you and at the same time, i'll try to fix my script..thanks

regards,
desperate_gurls :'(


Report •

#10
July 20, 2010 at 19:45:32
hi nails,
i've converted my script from /bash to /sh..

luckily, i managed to overcome the problem in converting the script but i did facing problem for assigning the time in 12-hours format..

below are my script:

#!/bin/sh
dir=/home/input/test.log
msg=`tail -3 $dir`
msg1=`tail -1 $dir`
type=Report

test_name=`echo "$msg1"|awk ' { print $2 } '`

status=successfully

if [ $test_name = $status ]; then
name=`echo "$msg1"|awk ' { print $1 } '`

e_time=`echo "$msg1"|awk ' { print $8 } '`
month=`echo "$msg1"|awk ' { print $6 } '`
year=`echo "$msg1"| tr -d '.'|awk ' { print $NF } ' |tr ":" " "`
day=`echo "$msg1"|awk ' { print $7 } '`
date=`echo "$day" "$month" "$year"`
stat=Success
else
name=`echo "$msg1"|awk ' { print $2 } '`
e_time=`echo "$msg1"|awk ' { print $9 } '`
month=`echo "$msg1"|awk ' { print $7 } '`
year=`echo "$msg"|awk '{ print $9 }' |tr -d '.'`
year=`echo $year |cut -c1-4`


day=`echo "$msg1"|awk ' { print $8 } '`
date=`echo "$day" "$month" "$year"`

stat=Failed
fi

hour=`echo $e_time |cut -c1-2`
min=`echo $e_time |cut -c4-5`
sec=`echo $e_time |cut -c7-8`

if [ $hour -le 12 ];then

end_time="${e_time:0} AM"
else
end_time="${e_time} PM"

fi

s_time=`echo $msg|awk '{ print $7 }'`
start_hour=`echo $s_time |cut -c1-2`
start_min=`echo $s_time |cut -c4-5`
start_sec=`echo $s_time |cut -c7-8`

if [ $start_hour -le 12 ];then
start_time= "${s_time:0} AM"
else
start_time= "${s_time} PM"

fi
if [ $day -lt 10 ]
then
f_day="0$day"
else
f_day="$day"
fi

new_date=`echo "$f_day" "$month" "$year"`
echo $msg

echo $name $new_date $s_time $end_time $type $stat


thus the output is:
report_name 19 Jul 2010 00:01:00 15:13:00 PM Report Success


there are two problems that i would like to point out here..
first, the $s_time is not printing the AM/PM..
this is the comment i got from the command prompt : s_time="${start_time:0} AM": The specified substitution is not valid for this command.

and the second problem is,

i could not minus the hour, what i meant is let say the input for time are:

23:00:15
02:00:15
17:00:15

so,i wanted the output for time to be:

11:00:15 PM
12:00:15 AM
05:00:15 PM

(the bold meaning that i want the hour to be in 2 decimal places)

below are the previous script to output the time in 12 hours format,but it doesn't working in shell

if [ $hours2 -le 12 ]
then
time_end="${time_end:0} AM"
else
time_end="$((hours2-12)):${time_end#*:} PM"
fi

my input are;
report_name started at Fri Jul 9 00:01:00 SST 2010.
kL/SSL procedure successfully completed.
report_name successfully ran on Fri Jul 19 15:13:00 SST 2010.


#the first line will give info on time start and the third line will give name,time end,status and date

hope anyone can help me on this..

hope you can help me on this..

thanks in advanced


Report •

#11
July 21, 2010 at 03:11:19
for the 12 hour format, i did edit the script..below are the script:

if [ $start_hour -le 12 ] ;then
#start_time= "0:$start_hour: $start_min: $start_sec"
start_time=AM
st_time=`echo "$start_hour":"$start_min":"$start_sec" "$start_time" `
else
start_time=PM
st_time=`echo expr{$start_hour - 12}`
fi


it works well if the start time is less than 12...but if the time is 23:00:15, the command prompt will output: expr{23 - 12}
how could i solve the calculation in variable??


Report •

#12
July 21, 2010 at 08:54:52
Try using expr like this - with no curly braces:

st_time=`echo expr $start_hour - 12`


Report •

#13
July 22, 2010 at 03:06:12
dear nails,
currently i able to convert the script into shell format..thanks for helping me..hope you can help me in da future :)

Report •


Ask Question