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
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
  • what about the "Q$STR\E" , I dont see \E in your syntax ? – yael Mar 17 '13 at 12:58
  • @yael: If there is nothing after the \E, you can ommit it. – choroba Mar 17 '13 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.