Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Shell variables in perl one liner

2,198 views
Skip to first unread message

Dean

unread,
Jun 14, 2010, 1:38:22 PM6/14/10
to
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.

Message has been deleted
Message has been deleted
Message has been deleted

Owen Rees

unread,
Jun 14, 2010, 5:31:09 PM6/14/10
to
On Mon, 14 Jun 2010 18:38:22 +0100, Dean <d...@spamfree.com> wrote in
<xtGdnXypBNaG9IvR...@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>

chris

unread,
Jun 15, 2010, 4:17:15 AM6/15/10
to
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

Dean

unread,
Jun 15, 2010, 5:53:37 AM6/15/10
to

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

Message has been deleted
Message has been deleted

Chris Davies

unread,
Jun 16, 2010, 8:27:13 AM6/16/10
to
Dean <d...@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

Brian Raven

unread,
Jun 16, 2010, 6:03:19 PM6/16/10
to
chris <ithi...@gmail.com> writes:

You could try using the Env module to import those environment variables:

perl -MEnv=START,FINISH -ne 'print if $_ ge $START && $_ le $FINISH' \
$LOGFILE

HTH

--
--
Brian Raven

0 new messages