如何grep字符串并将其分配给Perl中的变量

时间:2018-02-14 10:38:00

标签: perl

我有一个包含以下详细信息的文件;

文件名:allappsclus

const { Tray, nativeImage } = require('electron');

const iconPath = path.join(__dirname, 'build/icon-st.png');
mainWindow.tray = new Tray(nativeImage.createFromPath(iconPath));

我想在关联的cont:i-02dd208bf1d81c254 rs:i-0098ad0b59b7fe7cf 名称中使用i-XXX"=的值,并将其分配给另一个变量。

如果运行我的代码并获得输出,那么

cont
test-1.1.0.0
1insideif-CNS

现在我想在输出中打印值use strict; use warnings; use Data::Dumper; my $jupyter = 0; my $controller = 0; my $rstudio = 0; my $zeppelin = 0; my $fh= '/tmp/allappsclus'; open my $fh2, '<', $fh or die "Cannot open file: $!\n"; while ( <$fh2> ) { if ( $_ =~ /jup/ ) { $jupyter = 1; } elsif ( $_ =~ /con/ ) { $controller = 1; } elsif ( $_ =~ /rs/ ) { $rstudio = 1; } elsif ( $_ =~ /zep/ ) { $zeppelin = 1; } } print "test-$rs.$con.$jup.$zep\n"; if ( $zepeq '0' && $jup eq '0' && $con eq '1' && $rs eq '1' ) { print "insideif-CNS"; } else { print "do nothing"; } close $fh; close $fh2; 而不是i-02dd208bf1d81c254

1 个答案:

答案 0 :(得分:0)

$myStr = "cont:i-02dd208bf1d81c254 rs:i-0098ad0b59b7fe7cf";

if ($myStr =~ /cont:([^ ]+)/) # match the full pattern with the value you required
{
   $controller=1;
   $cont = $1;    # assign required value to a variable
}
print $cont;      # print the variable

这可能适合你。