Email backup log

Hi,
I have a server which appends to two different backup logs, a summary and a full log,
I want to write a script which will email out this mornings backup,
the problem is that in the log files the date is in the below format:

Fri May 31,2013 02:30

the summary log file I can just use Head to get the data, but in the full log because the log is a different length each day is proving difficult,
In the full summary each days log is delimited by

****************************************************************

I was trying to grep the full summary for the date and then using AWK print 500 lines below it, which works just not in the script,

#!/bin/ksh
DATE= `date | awk '{print $3, $2, $4}' | sed 's/\(.*\) /\1,/'`
cat /cfmtools/backups/logs/full | awk 'c-->0;/$DATE/{c=500; print}'

If i run it is get:

-bash-3.00$ ./backup_mail_V2
./backup_mail_V2: line 2: May: command not found

Can anyone help?

See he man page of the "date" command, it shouldn't be so complicated to get the date in this format to compare it to the log contents.

Then it is easy: use "sed"s range capability to print/delete only the lines in certain range:

mydate="$(date +'<appropriate format string here>')"

sed -n '/'"$mydate"'/,/^\*\{25\}/ p' /path/to/logfile

The sed-invocation will only print the lines starting with the first line containing your date to a line starting with 25 asterisks (i haven't bothered to count your line, correct the number if necessary). If you want to delete the files from the log use the same method but use "d" instead of "p" as a command.

I hope this helps.

bakunin

I have made some progress,
i changed the DATE variable to

DATE=  "`date | awk '{print $3, $2, $4}' | sed 's/\(.*\) /\1,/'`"

And now the only issue is it thinks the output

May 31,2013

is a command rather than text,

I want the variable to be text as im passing it into the next awk command as a string

You are doing this overly complicated. Again, read the man page of "date" and play around with the format strings you can supply to "date".

The reason why it thinks it is a command is:

variable=value      # correct
variable= value     # wrong
variable =value     # wrong too

There must not be any blanks between "=" and the value or the variables name.

I hope this helps.

bakunin

DATE=$(date +%b" "%d","%Y)

I do suggest you do not use back tics `` but $()

If you need it in awk , do it like this, no need to add date to wariable, when awk read it directly, eks

awk '{print $1,d}' d="$(date +%b" "%d","%Y)" infile

Edit
This should print the first 500 line from today date:

awk 't {print;--t} !f && $0~d {f=1;t=500}' d="$(date +%b" "%d","%Y) /cfmtools/backups/logs/full

Ok here is a stupid question but i can never remember how to do it,
the script is working fine but for some reason it is duplicating some of the lines

How do you remove duplicate lines?

---------- Post updated at 03:53 PM ---------- Previous update was at 12:58 PM ----------

This is really strange,
I tried running the script and I dont get any response,
just goes back to the prompt,
This made me think there are no entries in the log file for today,
but if i run the date command by its self it displays the date the way it should

-bash-3.00$ date +%b" "%d","%Y
May 31,2013

and if i then run:

cat /cfmtools/backups/logs/full | awk 'c-->0;/May 31,2013/{c=500; print}'

It again works fine but not in the script

Give some example from the full log file.

Why do you swap line around and using cat , when I show you how to do it correct?

Wrong!

cat /cfmtools/backups/logs/full | awk 'c-->0;/May 31,2013/{c=500; print}'

Correct :slight_smile:

awk 'c-->0;/May 31,2013/{c=500; print}' /cfmtools/backups/logs/full

Did you test this?

awk 't {print;--t} !f && $0~d {f=1;t=500}' d="$(date +%b" "%d","%Y) /cfmtools/backups/logs/full