Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sorry real life took me away from this for a while - looks like your going well.
Remember back a couple of pages grail suggested using sed to simplify the input, that was a realy good idea.
I've done up a little bourne shell script that uses this idea. I also defined a couple of internal functions getfield() and timeap() to make the code more readable.
The sed scripts just cut out the required fields and put them in a : seperated list.
msg1 is name:status:end_date:end_h:end_m:end_s
msg is name:status:start_date:start_h:start_m:start_s
After this is pretty easy to extract your required fields (I don't have access to a bourne shell so it's untested):
Code:
#!/bin/sh
dir=/home/input/test.log
type=Report
getfield () {
# echo Fields from : list
echo $1 | cut -d: -f$2 | tr ':' ' '
}
timeap () {
# Convert 24 hour time to AM/PM
if [ $1 -le 12 ]
then
hours=$1
ap=AM
else
hours=$(($1 - 12))
ap=PM
fi
printf "%02d:%02d:%02d %s" $hours $2 $3 $ap
}
msg1=`tail -1 $dir | sed -n -r -e '$ {/successfully/s/(.*) successfully ran on [^ ]* (.*) (.*) (.*) SST ([0-9]+).*/\1:success:\3 \2 \5:\4/p;/ERROR/s/.*R: (.*) is Failed on [^ ]* (.*) (.*) (.*) SST ([0-9]+).*/\1:failed:\3 \2 \5:\4/p}'`
msg=`tail -3 $dir | head -1 | sed -n -r -e 's/(.*) started at [^ ]* (.*) (.*) (.*) SST ([0-9]+).*/\1:status:\3 \2 \5:\4/p'`
name=`getfield "$msg1" 1`
status=`getfield "$msg1" 2`
logdate=`getfield "$msg1" 3 | sed -r -e 's/^([0-9]) /0\1 /'`
time_end=`getfield "$msg1" 4,5,6`
time_end=`timeap $time_end`
time_start=`getfield "$msg" 4,5,6`
time_start=`timeap $time_start`
echo "$name $logdate $time_start $time_end $type $status"
Last edited by simon.sweetman; 07-21-2010 at 01:02 PM.
Maybe this could be caused by my input file, which I made to contain the following stuff from Post #43 above:
Code:
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.
I started messing with the sed stuff, but thought better of that because I don't know what the input file is exactly supposed to look like.
If OP can post a real sample of what the input file should contain, I can test again in a bit. Also, please verify that dir=/home/input/test.log is actually supposed to point directly at the logfile, and not a directory, right?
EDIT:
I have changed the input file to contain only:
Code:
report_name successfully ran on Tue Jul 11 18:00:01 SST 2010.
#!/bin/sh
dir=/home/sasha/logfile
type=Report
getfield () {
# echo Fields from : list
echo $1 | cut -d: -f $2 | tr ':' ' '
}
timeap () {
# Convert 24 hour time to AM/PM
if [ $1 -le 12 ]
then
hours=$1
ap='AM'
else
hours=$(( ${1} - 12))
ap='PM'
fi
printf "%02d:%02d:%02d %s" $hours $2 $3 $ap
}
msg1=`tail -1 $dir | sed -n -r -e '$ {/successfully/s/(.*) successfully ran on [^ ]* (.*) (.*) (.*) SST ([0-9]+).*/\1:success:\3 \2 \5:\4/p;
/ERROR/s/(.*)R: (.*) is Failed on [^ ]* (.*) (.*) (.*) SST ([0-9]+).*/\1:failed:\3 \2 \5:\4/p}'`
msg=`tail -3 $dir | head -1 | sed -n -r -e 's/(.*) started at [^ ]* (.*) (.*) (.*) SST ([0-9]+).*/\1:status:\3 \2 \5:\4/p'`
name=`getfield "$msg1" 1`
status=`getfield "$msg1" 2`
logdate=`getfield "$msg1" 3 | sed -r -e 's/^([0-9]) /0\1 /'`
time_end=`getfield "$msg1" 4,5,6`
time_end=`timeap $time_end`
time_start=`getfield "$msg" 4,5,6`
time_start=`timeap $time_start`
echo "$name $logdate $time_start $time_end $type $status"
Back to 3 lines in the input file (after I noticed the `head -3` ) and now, with some small edits, it works. I dunno if it works "as it should", but it works error free in Dash shell. Here's what it outputs:
Nice work sasha, like the reformatted sed line makes it much easier to understand the two lines processed. I've learnt something new as well, as I never used dash before, looks like a nice simple/fast shell.
Thanks, nice work to you too, for coming up with those sed's - they can be mind-bending at times.
Yes, breaking up long lines really makes things easier to read. And since I began using nano editor a lot more, it's beneficial because unless you enlarge nano across multiple monitors, it doesn't show long lines properly, and is known to screw them up if one is not careful.
As for the sed's in your script before I edited it, there was only one minor problem with the first operation: a missing set of ()'s - otherwise it was fine. I can't recall now if there were any other things wrong, or if that one thing contributed to the other errors.
As for Dash, yes, it is noticeably faster than Bash, and significantly smaller. Plus, if you're interested in portability of your scripts, if they run in Dash, they'll run in most any shell, as Dash is POSIX compliant.
sed: illegal option -- r
sed: illegal option -- r
sed: illegal option -- r
./sasha.sh[2]: test: Specify a parameter with this command.
./sasha.sh[2]: test: Specify a parameter with this command.
Usage Usage: sed [-n] [-e script] [-f source_file] [file...] -12:80:00 -12:80:00 sed [-n] [-e script] [-f source_file] [file...]
Usage: sed [-n] [-e script] [-f source_file] [file...]
Usage: sed [-n] [-e script] [-f source_file] [file...]
i dun have any idea on how to solve the issue..can someone explain to me what should i do to solve it?
Of course we see this script is very dependant on the log file having the correct format. If your considering putting this onto a production system it should verify the format is OK and gracefully terminate with an error.
For example insert the following saftey test just after the 2 big sed lines. It tests for blank msg and msg1 and reports an error.
Code:
if [ -z "$msg" ]
then
echo "Invalid start message format: " `tail -3 $dir | head -1`
exit 1
fi
if [ -z "$msg1" ]
then
echo "Invalid finished message format: " `tail -1 $dir`
exit 2
fi
Last edited by simon.sweetman; 07-21-2010 at 09:36 PM.
i really apologize because not using the latest script that you guys suggested.This is because i encountered several problem in running the script..thus,i stayed with my original script.Currently my original script runs well as desired.But i wanted to upgrade my script,what i meant is that..based on the script, it will only get the specific line assigned in (voicemsg and voicemsg1)..thus,it is very risky if the line is not the exact line it want to take..i'm planning to use grep(the grep function will identify unique value which what i can see here is the date, but the date is not in exact format)i've tried and i couldn't solve the error..i need your help to guide me on how to edit the script in order for it to read the exact line using grep function, not tail that had been assigned specific number on it.
the way you code is very inefficient. A lot of cuts/awk/tr on each line..
I haven't looked at all the posts, but since you provided test.log, can you show me again clearly what is your expected output ?
ok..below are the output i got from the script..i want the same output but need to change the script using grep instead of assigning specific line to print the output.
report_name 27 Mar 2010 00:10:00 AM 00:10:00 AM Report Success
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.