repalce char/symbol to space in linux

Unitech Pt630d mobile computer, batch, 1...
July 19, 2010 at 20:53:11
Specs: Windows XP, 2gb
hi all,

someone please guide me on this..

i want to replace "-" to space in variable..

below are my script:

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

msg= 'tail -1 $dir`

date=${msg% * }
date={date%% *}

echo $date

but the output still " 09-JUL-2010"

i want the output to be 09 JUL 2010 (change "-" to "space")

i did search on the internet but i found that the sed command applied to all files..i don't want to apply it to whole file as it will change other information.I just wanted to apply it in the date variable.

the input in example.log is:

09-JUL-2010 00:09:16 : EXAMPLE_DATE Starting Elapsed: 00:00:00.01 XL/SSL process successfully completed. Elapsed: 00:23:18.12


See More: repalce char/symbol to space in linux

Report •


#1
July 19, 2010 at 21:36:26

$ d="09-JUL-2010"
$ echo ${d//-/ }
09 JUL 2010

reassign the result back to variable if desired

GNU win32 packages | Gawk


Report •

#2
July 19, 2010 at 21:40:27
thanks..it works! :)

Report •

#3
July 20, 2010 at 01:30:09
another problem..
my script failed to identify the status of the input..

the input are:

[I][if success][/I]

KL/SSL process successfully completed.

[I][if failed][/I]

KL/SSL process failed to complete.


so i need to extract the word successfully/failed in order for me to assign the status for the input..

if successfully, status=Success
if failed, status=Failed

below are my script:
c_status=`tail -1 $dir`

[I](the value for c_status might be: (based on log )
i)KL/SSL process successfully completed. [B]or[/B]
ii)KL/SSL process failed to complete. )[/I]

Stat=successfully
check_status=${c_status%%completed*}
check_status=${check_status#*procedure}
if[${check_status} = ${Stat}] then
status=Success
else
status=Failed
fi

the script able to extract the "successfully/failed"
BUT it fails to differentiate the status whether it is Success or Failed..any idea?


Report •

Related Solutions

#4
July 20, 2010 at 01:56:05
another question:
below are my script.. (assume that the hour retrieved from log file or user)

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

the code runs well...but i just realized that if the input is

00:00:12
23:00:12
03:00:12

so the output will be like this:

00:00:12 AM
11:00:12 PM
03:00:12 AM

But if my input is


20:00:12
16:00:12

the output are:

8:00:12
4:00:12

i wanted the output to be consistence where i need the output (hour) to be in 2 decimal place..

08:00:12
04:00:12

so, i did change my script like below:
start_hour=(${start_time%:*})
s_hour=${start_hour%:*}

if [ $s_hour -le 12 ];
then
start_time="${start_time:0} AM"
#else
else
if [ $((s_hour-12)) -lt 10 ];then
start_time="${s_hour:0} PM"
else
start_time="$((s_hour-12)):${start_time#*:} PM"
fi

but error..correct me please..
thanks


Report •

#5
July 20, 2010 at 03:28:54
I 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 •

#6
July 20, 2010 at 21:24:38
hi all,
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.


hope you can help me on this..

thanks in advanced


Report •

Ask Question