0

The following perl one liner checks if $CURRENT_VERSION matches $NEW_VERSION exactly, and prints it if it does.

Example:

CURRENT_VERSION=223.3.12.4.5.3
NEW_VERSION=223.3.12.4.5.3
DATE=17.3.2013
echo $CURRENT_VERSION  | \
  perl -ne 'BEGIN { $STR = shift(@ARGV); } print if /\Q$STR\E/; ' $NEW_VERSION 
223.3.12.4.5.3

What do I need to add to the perl one liner to print the $DATE value at the end of the line?

For example:

CURRENT_VERSION=223.3.12.4.5.3
NEW_VERSION=223.3.12.4.5.3
DATE=17.3.2013
echo $CURRENT_VERSION  | \
  perl -ne'BEGIN { $STR = shift(@ARGV); } print if /\Q$STR\E/; ' \
  $NEW_VERSION < add syntax to  print $DATE > 
223.3.12.4.5.3  17.3.2013

2 Answers 2

1
perl -F= -lane '$v{$F[0]}=$F[1]; if ($F[0] eq "DATE") {$v{CURRENT_VERSION} eq $v{NEW_VERSION} ? print $v{NEW_VERSION}, " ", $v{DATE} : exit(1)}'

Or, still a one-liner, but readable

perl -F= -lane '
    $v{$F[0]} = $F[1];
    if ($F[0] eq "DATE") {
        $v{CURRENT_VERSION} eq $v{NEW_VERSION} 
            ? print $v{NEW_VERSION}, " ", $v{DATE} 
            : exit(1)
    }' << END
CURRENT_VERSION=223.3.12.4.5.3
NEW_VERSION=223.3.12.4.5.3
DATE=17.3.2013
END

outputs

223.3.12.4.5.3 17.3.2013
0

This should work:

echo $CURRENT_VERSION | \
    perl -e '($nv,$d)=(shift,shift); /\Q$nv/ and chomp,print "$_ $d\n" while <>' $NEW_VERSION $DATE

However, it might be better to include $CURRENT_VERSION as one of the parameters as well:

perl -e '($nv,$d,$cv)=@ARGV; $cv=~/\Q$nv/ and print "$cv $d\n"' $NEW_VERSION $DATE $CURRENT_VERSION
2
  • what about the "Q$STR\E" , I dont see \E in your syntax ? Commented Mar 17, 2013 at 12:58
  • @yael: If there is nothing after the \E, you can ommit it. Commented Mar 17, 2013 at 13:09

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.