Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 1 down vote accepted
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
share|improve this answer

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
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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