-1

我有一个如下所示的文件:

app.version.code=1
app.version.major=1
app.version.minor=0
app.version.buildnumber=0

我想运行一个将打印出的 Perl 脚本<app.version.major>.<app.version.minor>.<app.version.buildnumber>。在这种情况下,它将打印出 1.0.0。我现在对 Perl 非常生疏,无法创建正则表达式和脚本来给我这个。有任何想法吗?

4

5 回答 5

2

你可以使用这样的东西:

perl -F= -lane '$h{$F[0]}=$F[1]; 
  END { print join ".", @h{ glob "app.version.{major,minor,buildnumber}" } }' filename
于 2012-12-06T21:35:58.537 回答
1
cat > ver.txt
app.version.code=1
app.version.major=1
app.version.minor=0
app.version.buildnumber=0
^d

cat> ver.pl
#!/usr/bin/perl -w

use strict;
use warnings;

my ($ma,$mi,$bi)=(0,0,0);
while(<>){ 
  if(/^app\.version\.(.*)$/) { 
    if( $1 =~ /^major=(\d+)$/ ){$ma=$1}
    elsif( $1 =~ /minor=(\d+)$/ ){$mi=$1}
    elsif( $1 =~ /buildnumber=(\d+)$/ ) {$bi=$1}
  }
} 

print "$ma.$mi.$bi\n";
^d

./ver.pl < ver.txt 
1.0.0
于 2012-12-06T21:27:53.480 回答
0

版本Xtrct.pl:

    #!/usr/bin/perl

    use strict;

    my $inFile = $ARGV[0];

    open(INHANDLE, "<$inFile") || die("Could not open file");

    my @fileLines = <INHANDLE>;

    my $fileText = join("", @fileLines);

    my $major;
    my $minor;
    my $build;

    if($fileText =~ m/app\.version\.major=(.+)\n/) { $major = $1; }

    if($fileText =~ m/app\.version\.minor=(.+)\n/) { $minor = $1; }

    if($fileText =~ m/app\.version\.buildnumber=(.+)\n/) { $build = $1; }

    my $version = $major.".".$minor.".".$build;

print "\nversion = ".$version;

    exit(0);

假设您的数据位于名为 input.txt 的文件中,请运行:

VersionXtrct.pl input.txt
于 2012-12-06T21:33:18.600 回答
0

在我之前的尝试中,我假设了非任意顺序。这应该按预期工作。

#!/usr/bin/env perl

use strict;
use warnings;

use 5.010;

my %version;

while (<DATA>) {
  given ($_) {
    $version{major}       = $1 when /app\.version\.major=(\d+)/;
    $version{minor}       = $1 when /app\.version\.minor=(\d+)/;
    $version{buildnumber} = $1 when /app\.version\.buildnumber=(\d+)/;
  }
}

say join ".", @version{qw/major minor buildnumber/};

__DATA__
app.version.code=1
app.version.major=1
app.version.minor=0
app.version.buildnumber=0
于 2012-12-06T21:33:29.583 回答
0

这是另一种选择:

use strict;
use warnings;

my %ver;

while (<>) {
    $ver{$1} = $2 if /(major|minor|number)=(\d+)$/;
}

print "$ver{major}.$ver{minor}.$ver{number}\n";

数据集上的输出:

1.0.0
于 2012-12-07T07:17:24.003 回答