LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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
 
Search this Thread
Old 07-18-2010, 11:56 PM   #31
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041

Try using

Code:
set -xv
as the 2nd line of your script. It'll show you exactly what it's doing
 
Old 07-19-2010, 12:56 AM   #32
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
i did try this and managed to obtain the hours for the time..but i failed to obtained the rest of the time which is the min and sec..



time=${datetime##* }
#dftime=(${time%%:})
hours=${time}
hours=${time%%:*}

min_sec=${time##:*}

so i decided to replace the ":" in time to " "(space) so that it will be easy for me to assign the value..
example: 23:00:15 to 23 00 15

really appreciate your help..
 
Old 07-19-2010, 01:43 AM   #33
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
it's working!

thanks a lot! ..at first there's problem with the script but after i analyzed it back, what i need to do is to assign variable to hours in order for the if statement to process the time whether in am or pm..thanks again

Last edited by tedy2808; 07-19-2010 at 03:35 AM.
 
Old 07-19-2010, 11:53 PM   #34
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
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
 
Old 07-20-2010, 04:27 AM   #35
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
another problem..
my script failed to identify the status of the input..

the input are:

[if success]

KL/SSL process successfully completed.

[if failed]

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`

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


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?

Last edited by tedy2808; 07-20-2010 at 04:29 AM.
 
Old 07-20-2010, 04:53 AM   #36
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
simon.sweetman..

hope you still remember on this code:

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

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
 
Old 07-20-2010, 05:24 AM   #37
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
It's hard to work out from the thread what your current script is. Maybe useful to post the whole script in code tags (that's a link to instructions or you may prefer to use "Advanced Edit" mode which has a # button for code tags). Assuming you are still using
Code:
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
then you can use printf to get the leading 0 when required, something like this (not tested)
Code:
suffix=AM
if [[ $dftime[1] -ge 12 ]]; then
    dftime[1]=((dftime[1]-12))
    suffix=PM
fi
new_time="$( printf '%02i' ${dftime[1]} ) $dftime[2] $dftime[3] $suffix"
 
Old 07-20-2010, 06:27 AM   #38
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
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



inputtest.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
 
Old 07-20-2010, 06:42 AM   #39
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Yeek! You need that to run in a non-Bash shell (I'll assume a POSIX shell)? I believe you have a lot of converting to do.

First, POSIX shells do not allow arrays[].

Second, shell substitutions such as df=(${msg1##* on}) (for example) are less widely accepted in sh shells, with many of them being Bash-specific.

Third, some POSIX shells do not accept the `typeset` command, though that's easy to fix since you really don't need it anyway, or you can just say var=0 or var=$((0)) to guarantee it is defined as an integer.

Fourth, the [[ ... ]] construct is not accepted in some shells, so you may need to replace that with the old-fashioned [ ... ] method -- not a big deal, if you take care to ensure the logic within the [ ] is correct and anything questionable is safely "quoted" when necessary.

Some suggestions:

1) Use code tags when posting long chunks of code. See here: http://www.phpbb.com/community/faq.php?mode=bbcode#f2r1

2) Install a sh shell comparable to the one you will be using, for testing your script (unless you are testing in the real environment, in which case you already have the shell.) If you can, tell us exactly what shell it is - that might help someone help you determine what that shell will accept.

3) Run the script on the sh shell, and when an error is encountered (such as on those shell substitutions) repair all of the same type of substitution and test again. You may need to use `tr` and `cut` and `sed` instead of shell substitution - it adds overhead, but you may not have a choice.

4) Finally, ensure that the /bin/sh on the destination machine is not simply a symlink to /bin/bash as it often is. If it is, your work is perhaps much less.

Last edited by GrapefruiTgirl; 07-20-2010 at 06:43 AM.
 
Old 07-20-2010, 06:43 AM   #40
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
It would help people to help you if you used CODE tags and if you posted a sample log for input.
 
Old 07-20-2010, 09:22 PM   #41
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
this is my code in code tag..

Code:
#!/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 "

i don't know how to attach the input file..
i just copy and paste here..

below are the exact information in input file:



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.
report_name started at Thu Jul 9 00:01:00 SST 2010.
KL/SSL procedure successfully completed.
report_name successfully ran on Thu Jul 9 15:15:00 SST 2010.
report_name started at Thu Jul 10 00:01:00 SST 2010.
KL/SSL procedure successfully completed.
report_name successfully ran on Thu Jul 10 15:15:00 SST 2010.


(based on the script, it will read the last 3 line (report for 1 day) and will output:

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

really need your help..guide me on this..thanks
 
Old 07-20-2010, 10:51 PM   #42
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
hi all, i'm trying to fix my script..

i need to extract the first word in a line as output..

below are my script:

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

msg1=`tail -1 $dir`

ret_str=`echo "$msg1"| tr -d '.'|awk ' { print $NF } ' |tr ":" " "`
name=`echo "$msg1"|awk ' { print $NF } '`


echo $msg1
echo $ret_str
echo $name


the input:

report_name successfully ran on Tue Jul 11 18:00:01 SST 2010.


the script output:

report_name successfully ran on Tue Jul 11 18:00:01 SST 2010.
2010


i tried to edit the script in order for the script to output file_name but i encountered problem in understanding
|awk ' { print $NF } ' |tr ":" " "`

can someone help me on this?

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

it's ok...solved!

Last edited by tedy2808; 07-21-2010 at 06:07 AM.
 
Old 07-21-2010, 12:25 AM   #43
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
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:

Code:
#!/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.
PL/SQL 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..

thanks in advanced

Last edited by tedy2808; 07-21-2010 at 06:11 AM.
 
Old 07-21-2010, 04:32 AM   #44
simon.sweetman
Member
 
Registered: Mar 2009
Posts: 32

Rep: Reputation: 22
This should get the year for you:

Code:
year=`echo "$msg1"|sed 's/.* \(.*\)\.ErrorCode:.*/\1/'`
Extract 1st word:

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

msg1=`tail -1 $dir`
name=`echo $msg1 | cut -f1 -d' '`
 
Old 07-21-2010, 06:09 AM   #45
tedy2808
Member
 
Registered: Jul 2010
Posts: 34

Original Poster
Rep: Reputation: 15
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??
 
  


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


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


All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
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