LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2007, 09:15 PM   #1
jiffyette
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Rep: Reputation: 0
scripting question on freebsd


ok im running freebsd 6.2,
okey dokey, now ive worked out the date command, i want the script now to take the 2 dates given ($old_date and $new_date) and search the myerrors.txt file and output all errors that are listed from the time between the 2 dates inclusive of them to a new file, im struggling a little. worthwile note - i cannot use pearl.

cheers

#!/usr/local/bin/bash
# Script to take errors out of the apache log
# and put it in a seperate file.
# 2007-7-6
# john
old_date=`date -v -30d`
new_date=`date`
read -p "what is your unix login name?" name
grep $name /usr/local/apache2/logs/error_log > myerrors.txt
 
Old 06-13-2007, 09:39 PM   #2
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by jiffyette
ok im running freebsd 6.2,
okey dokey, now ive worked out the date command, i want the script now to take the 2 dates given ($old_date and $new_date) and search the myerrors.txt file and output all errors that are listed from the time between the 2 dates inclusive of them to a new file, im struggling a little. worthwile note - i cannot use pearl.

cheers

#!/usr/local/bin/bash
# Script to take errors out of the apache log
# and put it in a seperate file.
# 2007-7-6
# john
old_date=`date -v -30d`
new_date=`date`
read -p "what is your unix login name?" name
grep $name /usr/local/apache2/logs/error_log > myerrors.txt

Code:
sed -n -e "/$old_date/,/$newdate/{p;d;};/$newdate/p" myerrors.txt
You may have to adjust the format of date's output to match that in the log file.
 
Old 06-14-2007, 07:42 PM   #3
jiffyette
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Original Poster
Rep: Reputation: 0
k thanx for the command but still no luck, the dates between my script and the log file are almost right except that the dates in my script have the EST in them. whats the correct switch to take that out?

cheers
 
Old 06-14-2007, 07:47 PM   #4
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by jiffyette
k thanx for the command but still no luck, the dates between my script and the log file are almost right except that the dates in my script have the EST in them. whats the correct switch to take that out?

Try the +%c format string.

 
Old 06-14-2007, 08:05 PM   #5
jiffyette
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Original Poster
Rep: Reputation: 0
got that, still now working though

script :

#!/usr/local/bin/bash
# Script to take errors out of the apache log
# and put it in a seperate file.
# 2007-7-6
# john
old_date=`date -v -30d +%c`
new_date=`date +%c`
read -p "what is your unix login name?" name
grep $name /usr/local/apache2/logs/error_log > myerrors.txt
sed -n -e "/$old_date/,/$new_date/{p;d;};/$new_date/p" myerrors.txt

i also tried to pipe it and it doesnt work

basically its not taking out the lines with those dates between and inclusive of those dates.

ive included below a list of some of em in the log file

[Thu Jun 07 12:44:02 2007] [error] [client 10.0.0.5] (13)Permission denied: exe
[Thu Jun 07 12:44:02 2007] [error] [client 10.0.0.5] Premature end of script he
[Thu Jun 07 12:45:30 2007] [error] [client 10.0.0.5] (13)Permission denied: exe

driving me nuts - any help be appreciated

john
 
Old 06-14-2007, 08:55 PM   #6
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by jiffyette
got that, still now working though

script :

#!/usr/local/bin/bash
# Script to take errors out of the apache log
# and put it in a seperate file.
# 2007-7-6
# john
old_date=`date -v -30d +%c`
new_date=`date +%c`
read -p "what is your unix login name?" name
grep $name /usr/local/apache2/logs/error_log > myerrors.txt
sed -n -e "/$old_date/,/$new_date/{p;d;};/$new_date/p" myerrors.txt

i also tried to pipe it and it doesnt work

basically its not taking out the lines with those dates between and inclusive of those dates.

ive included below a list of some of em in the log file

[Thu Jun 07 12:44:02 2007] [error] [client 10.0.0.5] (13)Permission denied: exe
[Thu Jun 07 12:44:02 2007] [error] [client 10.0.0.5] Premature end of script he
[Thu Jun 07 12:45:30 2007] [error] [client 10.0.0.5] (13)Permission denied: exe

So what is it doing?

What do you mean by "taking out"? Do you mean printing or not
printing?

Have you compared the output of the date commands with the format in the file?

Try this format string: +"%a %b %d %H:%M:%S %Y"
 
Old 06-14-2007, 09:11 PM   #7
jiffyette
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Original Poster
Rep: Reputation: 0
yeah sorry, its not printing
 
Old 06-14-2007, 09:16 PM   #8
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by jiffyette
yeah sorry, its not printing

Did you read the rest of my post?
 
Old 06-14-2007, 09:37 PM   #9
jiffyette
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Original Poster
Rep: Reputation: 0
i did. please remember im just starting out and am a n00b.
note im running freebsd 6.1

ile go back to basics this is what i need to do

Create a shell script

* that prompts you for your unix logon name.
* Opens the apache error log and finds all the lines where your name occurs in the last thirty days.
* Sends those lines to a file, called myerrors.txt, with the latest errors appearing at the top of the file and each line numbered.

obviously my script is above. sum1 on another forum said to try this :

Probably the programatically easiest, but inefficient way is to generate the regex for everyday within your date range and do a pass through the file for however many days you need and redirect the output to the end of a file at each pass. Alternatively you can try and generate a single regex that will match all the days, either way should work and not involve more than a while loop, a if statement and the date program together with grep or even awk to do the pattern matching.

The hardest part will be constructing the regex, but thats just text parsing and manipulation of the date program's output, which you can already do to an extent using the +FORMAT options.

and i have no clue. i have learnt a lot in the last couple of weeks, of trying to find the solution.

i mega appreciate the help and if u can walk thru it with me thats gr8 too *rem statements*

john if your wondering im in australia too
 
Old 06-14-2007, 10:09 PM   #10
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by jiffyette
i did. please remember im just starting out and am a n00b.
note im running freebsd 6.1

A properly written script will run on any system.
Quote:
ile go back to basics this is what i need to do

Create a shell script

* that prompts you for your unix logon name.
* Opens the apache error log and finds all the lines where your name occurs in the last thirty days.
* Sends those lines to a file, called myerrors.txt, with the latest errors appearing at the top of the file and each line numbered.

obviously my script is above. sum1 on another forum said to try this :

Which part of this script doesn't work?

Code:
old_date=`date -v -30d +"%a %b %d %H:%M:%S %Y"`
new_date=`date +"%a %b %d %H:%M:%S %Y"`
read -ep "what is your unix login name?" name
grep "$name" /usr/local/apache2/logs/error_log > myerrors.txt

## Diagnostics
echo
printf "old_date=%s\n" "$old_date"
printf "new_date=%s\n" "$new_date"
printf "%s\n" "" "Examining myerrors.txt" "============================="
head myerrors.txt
printf "%s\n" "-----------------------------"
tail myerrors.txt
printf "%s\n" "============================="

sed -n -e "/$old_date/,/$new_date/{p;d;};/$new_date/p" myerrors.txt > output.txt

## Diagnostics
printf "%s\n" "" "Examining output.txt" "============================="
head output.txt
printf "%s\n" "-----------------------------"
tail output.txt
printf "%s\n" "============================="
If if doesn't work, examine each part of it to see if you are getting what you expect.

Does the ouput of the date commands match the format in the file? Spaces and leading zeroes are important.

Does the output of grep (myerrors.txt) contain what you expect?

What about output.txt?

Quote:
i mega appreciate the help and if u can walk thru it with me thats gr8 too *rem statements*

john if your wondering im in australia too

Wherever you are, it would help if you posted in English.

 
Old 06-14-2007, 10:37 PM   #11
jiffyette
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Original Poster
Rep: Reputation: 0
thankyou for that. have run the script, it seems that the sed command isnt working because output.txt has nothing in it.

which means its falling over somewhere on the dates.....

Last edited by jiffyette; 06-14-2007 at 10:49 PM.
 
Old 06-14-2007, 10:46 PM   #12
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by jiffyette
thankyou for that. have run the script, it seems to be taking march instead of may. any ideas?

What "seems to be taking march instead of may"?

 
Old 06-14-2007, 10:53 PM   #13
jiffyette
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Original Poster
Rep: Reputation: 0
its not taking out the lines generated by the sed command

sed -n -e "/$old_date/,/$new_date/{p;d;};/$new_date/p" myerrors.txt > output.txt


john
 
Old 06-14-2007, 10:59 PM   #14
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mint, Mandriva
Posts: 221

Rep: Reputation: 31
Quote:
Originally Posted by jiffyette
its not taking out the lines generated by the sed command

sed -n -e "/$old_date/,/$new_date/{p;d;};/$new_date/p" myerrors.txt > output.txt

So what is it doing?

Are you getting any error messages?

What are the values of old_date and new_date?

Do the values of old_date and new_date match the dates in myerrors.txt?

 
Old 06-14-2007, 11:02 PM   #15
jiffyette
LQ Newbie
 
Registered: Jun 2007
Posts: 11

Original Poster
Rep: Reputation: 0
it runs fine.
with no error messages

below is the output of the file once run

what is your unix login name?portchc

old_date=Wed May 16 14:13:16 2007
new_date=Fri Jun 15 14:13:16 2007

Examining myerrors.txt
=============================
[Fri Mar 16 12:00:44 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/counter.pl denied
[Fri Mar 16 12:01:48 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/counter.pl denied
[Fri Mar 16 12:01:49 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/counter.pl denied
[Fri Mar 16 12:03:39 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/counter.pl denied
[Fri Mar 16 12:04:17 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/counter.pl denied
[Fri Mar 16 12:05:51 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/count.pl denied
[Fri Mar 16 12:10:21 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/count.pl denied
[Fri Mar 16 12:11:23 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/count.pl denied
[Fri Mar 16 12:13:44 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/count.pl denied
[Fri Mar 16 12:13:45 2007] [error] [client 10.0.0.5] (13)Permission denied: access to /~portchc/cgi-bin/count.pl denied
-----------------------------
[Thu Jun 07 12:43:11 2007] [error] [client 10.0.0.5] (13)Permission denied: exec of '/home/portchc/public_html/cgi-bin/myerror.pl' failed, referer: http://cybil/~portchc/error.html
[Thu Jun 07 12:43:11 2007] [error] [client 10.0.0.5] Premature end of script headers: myerror.pl, referer: http://cybil/~portchc/error.html
[Thu Jun 07 12:43:15 2007] [error] [client 10.0.0.5] (13)Permission denied: exec of '/home/portchc/public_html/cgi-bin/myerror.pl' failed, referer: http://cybil/~portchc/error.html
[Thu Jun 07 12:43:15 2007] [error] [client 10.0.0.5] Premature end of script headers: myerror.pl, referer: http://cybil/~portchc/error.html
[Thu Jun 07 12:43:19 2007] [error] [client 10.0.0.5] (13)Permission denied: exec of '/home/portchc/public_html/cgi-bin/myerror.pl' failed, referer: http://cybil/~portchc/error.html
[Thu Jun 07 12:43:19 2007] [error] [client 10.0.0.5] Premature end of script headers: myerror.pl, referer: http://cybil/~portchc/error.html
[Thu Jun 07 12:44:02 2007] [error] [client 10.0.0.5] (13)Permission denied: exec of '/home/portchc/public_html/cgi-bin/myerror.pl' failed, referer: http://cybil/~portchc/error.html
[Thu Jun 07 12:44:02 2007] [error] [client 10.0.0.5] Premature end of script headers: myerror.pl, referer: http://cybil/~portchc/error.html
[Thu Jun 07 12:45:30 2007] [error] [client 10.0.0.5] (13)Permission denied: exec of '/home/portchc/public_html/cgi-bin/myerror.pl' failed, referer: http://cybil/~portchc/error.html
[Thu Jun 07 12:45:30 2007] [error] [client 10.0.0.5] Premature end of script headers: myerror.pl, referer: http://cybil/~portchc/error.html
=============================

Examining output.txt
=============================
-----------------------------
=============================
 
  


Reply



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
What is the answer??? pxumsgdxpcvjm General 5 12-23-2005 10:00 PM
As a FreeBSD user, what is the best things you like about FreeBSD? t3gah *BSD 6 06-10-2005 02:38 PM
to answer the FreeBSD as desktop/at home OS question... mipia *BSD 0 10-25-2004 12:50 AM
Anyone have an answer?? waynevnc Linux - Hardware 8 07-01-2004 04:22 AM
$10 - if you can answer Jaffy Linux - General 5 07-30-2003 12:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 PM.

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
Open Source Consulting | Domain Registration