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.
Hello all. I'm new to scripting and wondered if I could get a little help. I would like to execute a script that will extract or display the past 7 days of a log file. The log file is /var/log/messages and below is an example of the date format:
May 11 06:01:40 hostname rtvscand: New virus definition file loaded. Version: 130510c.
May 11 06:01:40 hostname rtvscand: Download of virus definition file from LiveUpdate server succeeded.
May 12 06:01:27 hostname rtvscand: New virus definition file loaded. Version: 130511b.
May 12 06:01:27 hostname rtvscand: Download of virus definition file from LiveUpdate server succeeded.
A friend wrote a script but it is not pulling the last 7 days (please see below)
#!/bin/bash
FILE=/var/log/messages
# Get the julian time for this second
N=`date '+%s'`
# Test: March 1st
#N=`date -j 03010000 '+%s'`
# Create a regular expression to match the last 3 days
REGEXP=""
for I in 1 2 3
do
if [ ! -z "$REGEXP" ]; then
REGEXP="$REGEXP|"
fi
# Get the Month/Day for time N
DAY=`date -r $N '+%b %e'`
REGEXP="${REGEXP}${DAY}"
# Subtract one day
N=`expr $N - 86400`
done
# If today were the 10th of March,
# REGEXP should be "(Mar 10|Mar 9|Mar 8)"
grep -E "^($REGEXP)" $FILE
exit 0
#!/bin/bash
#Author: Travis Smith
#Filename:weeklog.sh
#Date Created: 6-27-2013
#Revision: 1.0.0
#Notes: make sure that what ever you name the file
#make sure to 'chmod +x <filename>.sh' so that its executable
#%a returns the abbreviated weekday name
#%A returns the full weekday name
#%d returns the day of the month
#%Y returns the full 4 digit year
#%y returns the 2 digit year
#adjust to match your required criteria
#for more options
#http://linux.about.com/od/commands/l/blcmdl1_date.htm
#
#Must use the Double qutoes around the date formatting
#to get the use of blank spaces but after the '+' sign
#set the date for the log name
TODAY=`(date +"%Y-%m-%d")`
#set the number of days back you want to go
for i in 1 2 3 4 5 6 7
do
WEEKOLD=`(date +"%a %b %d" --date="$i days ago")`
#This echo is used for testing the variable output
#echo "$WEEKOLD"
cat /var/log/messages | grep "$WEEKOLD" >> "$TODAY".txt
done
Do use 'grep' on its own here (also search for "the useless use of cat award"), maybe "anchor" grep search terms with "^"? and finally output will be appended to a file stored in the CWD which may cause unexpected results if the file enlarges beyond capacity of the CWD.
After running your script I get a file that is blank. I ensured that a it ended with .sh and chmod the file to 770. I exectuted it as root. If you run the file do you get the last 7 days of \var\log\messages?
Again, not nothing about scripting. I simply ran your orgianl posting and observed the file created with today's date and there is no data in the file. Below your original posting comments were made but I'm not sure what to make of them or how to utilizing them to edit the script.
Sorry for the confusion. Still don't have a solution.
Jun 29 23:59:10 VCDweb1 systemd-logind[749]: Removed session 15975.
Jun 29 23:59:14 VCDweb1 sshd[10216]: Accepted keyboard-interactive/pam for root from 192.168.40.12 port 51843 ssh2
Jun 29 23:59:14 VCDweb1 systemd-logind[749]: New session 15976 of user root.
Jun 29 23:59:15 VCDweb1 systemd-logind[749]: Removed session 15976.
Jun 29 23:59:19 VCDweb1 sshd[10302]: Accepted keyboard-interactive/pam for root from 192.168.40.12 port 51845 ssh2
Jun 29 23:59:19 VCDweb1 systemd-logind[749]: New session 15977 of user root.
Jun 29 23:59:20 VCDweb1 systemd-logind[749]: Removed session 15977.
Jun 29 23:59:24 VCDweb1 sshd[10388]: Accepted keyboard-interactive/pam for root from 192.168.40.12 port 51850 ssh2
Jun 29 23:59:24 VCDweb1 systemd-logind[749]: New session 15978 of user root.
Jun 29 23:59:25 VCDweb1 systemd-logind[749]: Removed session 15978.
your permissions should should look like this
-rwxr-xr-x 1 root root 912 Jun 28 11:29 weeklog.sh*
use the "chmod +x weeklog.sh" command
next make sure if you have changed the directory path that you use the correct path '/' not '\'.
try this "tail -f /var/log/messages" to make sure you have data that is seven days old
Quote:
Originally Posted by johnmccarthy
After running your script I get a file that is blank. I ensured that a it ended with .sh and chmod the file to 770. I exectuted it as root. If you run the file do you get the last 7 days of \var\log\messages?
The problem the orginal script is that the date command does not interpet like BSD. The foundation of the script was based on the date command. Below is a perl script that works in linux and provides the current date and 8 previous days.
#!/usr/bin/perl
use POSIX;
my $log = "/tmp/messages"; # Log file name
my $n = 8; # Number of days to go back
# Get the julian time for this second
my $jultm = time();
# Create a regular expression to match the last 3 days
my $regexp = "";
for(my $I = $n; $I > 0; $I--)
{
if (length($regexp) > 0)
{
$regexp .= "|";
}
# Get the Month/Day for time N
my $day = POSIX::strftime("+%b %e", localtime($jultm));
$regexp .= $day;
# Subtract one day
$jultm -= 86400;
}
# If today were the 10th of March,
# regexp should be "(Mar 10|Mar 9|Mar 8)"
#print "REGEXP: $regexp\n";
my $cmd = "grep -E '^($regexp)' $log";
#print "CMD: $cmd\n";
system $cmd;
exit 0;
1) Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.
2) Since this is Linux Questions, we usually assume you're using Linux and its tools (i.e. the gnu coreutils) unless otherwise specified. So always clearly specify what your environment is if it's in any way non-standard.
3) I'd have probably suggested awk myself before learning about #2, since gawk has a good set of built-in time functions for this kind of thing. It could probably still be done with nawk or posix awk, but it would be more complex.
4) I'm starting to wonder if I shouldn't institute a ${Useless} ${Use} ${Of} ${Variable} ${Brackets} award, since I've been seeing them so often recently.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.