From: Dean on
I want to invoke the following from a Bash script:

START='[14/Jun/2010:00:00:00]'
FINISH='[14/Jun/2010:23:59:59]'
perl -ne ' print if ( $_ ge "$START" && $_ le "$FINISH" ) ' $LOGFILE

I want Bash to substitute the $START, $FINISH & $LOGFILE variables and leave
the $_ to perl.

How should I quote/escape that perl command?

Thanks for any help.

From: Owen Rees on
On Mon, 14 Jun 2010 18:38:22 +0100, Dean <de(a)spamfree.com> wrote in
<xtGdnXypBNaG9IvRnZ2dnUVZ8r2dnZ2d(a)brightview.co.uk>:

>I want to invoke the following from a Bash script:
>
>START='[14/Jun/2010:00:00:00]'
>FINISH='[14/Jun/2010:23:59:59]'
>perl -ne ' print if ( $_ ge "$START" && $_ le "$FINISH" ) ' $LOGFILE

Why? What problem are you really trying to solve? [*]

>
>I want Bash to substitute the $START, $FINISH & $LOGFILE variables and leave
>the $_ to perl.
>
>How should I quote/escape that perl command?

This quoting and escaping will make it run...

perl -ne " print if ( \$_ ge '$START' && \$_ le '$FINISH' ) " $LOGFILE

.... but there are a lot of problems with it as a way to select lines
from a logfile between given start and finish times. The question marked
[*] above is there because unless you have a very strange problem to
solve, this is not likely to be taking you in the direction of a good
answer.

--
Owen Rees
[one of] my preferred email address[es] and more stuff can be
found at <http://www.users.waitrose.com/~owenrees/index.html>
From: chris on
On 14/06/10 18:38, Dean wrote:
> I want to invoke the following from a Bash script:
>
> START='[14/Jun/2010:00:00:00]'
> FINISH='[14/Jun/2010:23:59:59]'
> perl -ne ' print if ( $_ ge "$START"&& $_ le "$FINISH" ) ' $LOGFILE
>
> I want Bash to substitute the $START, $FINISH& $LOGFILE variables and leave
> the $_ to perl.
>
> How should I quote/escape that perl command?

Not sure explicitly, but if you stored START and FINISH as environment
variables you could use $ENV{} instead:

export START='[14/Jun/2010:00:00:00]'
export FINISH='[14/Jun/2010:23:59:59]'
perl -ne ' print if ( $_ ge "$ENV{START}"&& $_ le "$ENV{FINISH}" ) '
$LOGFILE

However, why do it all within perl? Why do you need the bash
substitutions for START and FINISH?

perl -ne 'BEGIN { $start = "[14/Jun/2010:00:00:00]"; $finish =
"[14/Jun/2010:23:59:59]";} print if ( $_ ge "$start"&& $_ le "$finish"
) ' $LOGFILE
From: Dean on
On Tue, 15 Jun 2010 09:17:15 +0100, chris wrote:

>On 14/06/10 18:38, Dean wrote:
>> I want to invoke the following from a Bash script:
>>
>> START='[14/Jun/2010:00:00:00]'
>> FINISH='[14/Jun/2010:23:59:59]'
>> perl -ne ' print if ( $_ ge "$START"&& $_ le "$FINISH" ) ' $LOGFILE
>>
>> I want Bash to substitute the $START, $FINISH& $LOGFILE variables and leave
>> the $_ to perl.
>>
>> How should I quote/escape that perl command?
>
>Not sure explicitly, but if you stored START and FINISH as environment
>variables you could use $ENV{} instead:
>
>export START='[14/Jun/2010:00:00:00]'
>export FINISH='[14/Jun/2010:23:59:59]'
>perl -ne ' print if ( $_ ge "$ENV{START}"&& $_ le "$ENV{FINISH}" ) '
>$LOGFILE
>
>However, why do it all within perl? Why do you need the bash
>substitutions for START and FINISH?
>
>perl -ne 'BEGIN { $start = "[14/Jun/2010:00:00:00]"; $finish =
>"[14/Jun/2010:23:59:59]";} print if ( $_ ge "$start"&& $_ le "$finish"
>) ' $LOGFILE

I should have been clearer about what I am trying to do: I have a set of
scripts for running rsnapshot. One being a cron job that runs rsnapshot
against a series of rsnapshot config files.

My script creates a timestamp just before the backup jobs run, then another
timestamp when they finish. This gives me the time window to later extract
lines from the rsnapshot log. The timestamp is the same format as rsnapshot
log lines.

So I have something like this:

DATE_FORMAT='[%d/%b/%Y:%H:%M:%S]'
FILE=~/rsnapshot.run

# Run each backup config
echo `date +"${DATE_FORMAT}"` > $FILE
for f in ${CONFIG_FILES[@]}; do
if (grep -q "^interval.*${SCHEDULE}" $f); then
$rsnapshot $RSNAPSHOT_ARGS -c $f $SCHEDULE
fi
done
echo `date +"${DATE_FORMAT}"` >> $FILE


Then, later I can read those times and extract the log lines of interest
from /var/log/snapshot. That is where I hoped to use the perl code or
anything that achieves the same.

I looked for methods to extract log lines between two time points. I
Couldn't do it with grep & I don't know awk. I was under the impression
that that line of perl somehow achieved the expected behaviour. It seems
that is not the case.

Still, I'd like to know a way, with the flexibility of timestamp format, I
can select log lines between two times. It seems a useful technique to know
for future use.

Cheers

From: Chris Davies on
Dean <de(a)spamfree.com> wrote:
> perl -ne ' print if ( $_ ge "$START" && $_ le "$FINISH" ) ' $LOGFILE

> I want Bash to substitute the $START, $FINISH & $LOGFILE variables and leave
> the $_ to perl.

Single quotes for literals and double quotes for variable
interpolation. If you sit them next to each other, the shell won't
introduce an extraneous space:

perl -ne 'print if ($_ ge "'"$START"'" && $_ le "'"$FINISH"'")' "$LOGFILE"

Chris