Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract string within double quote from the line in a file

Tags:

perl

I have the file with content as :

Component (0463) "commonfiles"
Component (0464) "demo_new_comp"
Component (0467) "test_comp" (removed)
Component (0469) "test_comp3" (removed)
Component (0465) "textfiles1

Need to extract string within double quotes from each line having (removed) and place in a array. My code is:

my $fh = new IO::File;
$fh->open("<comp.log") or die "Cannot open comp.log";
my @comp_items;
while (<$fh>) {
    if ( $_ =~ /removed/ ) {;
        my $compName = $_ = ~ m/"(.*?)"/;
        print " Componnet Name : \"$compName\"\n";
    }
}

I am not getting the correct output giving some numbers as:

"18446744073709551614"
"18446744073709551614"

Output should be :

test_comp
test_comp3
like image 626
user3616128 Avatar asked Mar 18 '23 10:03

user3616128


1 Answers

my $compName = $_ = ~ m/"(.*?)"/;

= ~ is not same as =~ but assignment and bitwise negation

and what you want is,

my ($compName) = $_ =~ m/"(.*?)"/;

or just,

my ($compName) = /"(.*?)"/;
like image 177
mpapec Avatar answered May 04 '23 00:05

mpapec