problem running grep within system command in perl

J

jeniffer

system("nm -r *.o|grep -v '.o' |grep "T\|t\|U\|u" |cut -c 12-|sort|
uniq"); the problem is that if we match 1 or more strings in grep the
argument must be in quotes i.e T\|t\|U\|u must be withing quotes ...how
do i escape them so that the above works fine?
 
J

John Bokma

jeniffer said:
system("nm -r *.o|grep -v '.o' |grep "T\|t\|U\|u" |cut -c 12-|sort|
uniq"); the problem is that if we match 1 or more strings in grep the
argument must be in quotes i.e T\|t\|U\|u must be withing quotes ...how
do i escape them so that the above works fine?

system('nm -r *.o|grep -v '.o' |grep "T\|t\|U\|u" |cut -c 12-|sort|

system("nm -r *.o|grep -v '.o' |grep \"T\|t\|U\|u\" |cut -c 12-|sort|

system( qq(nm -r *.o|grep -v '.o' |grep "T\|t\|U\|u" |cut -c 12-|sort|

I would use the fist one.
 
A

A. Sinan Unur

Is that an inflated ego or what?

system("nm -r *.o|grep -v '.o' |grep "T\|t\|U\|u" |cut -c 12-|sort|
uniq"); the problem is that if we match 1 or more strings in grep the
argument must be in quotes i.e T\|t\|U\|u must be withing quotes ...how
do i escape them so that the above works fine?

perldoc perlop

Look for "Quote and Quote-like Operators"

Sinan
 
J

John W. Krahn

jeniffer said:
system("nm -r *.o|grep -v '.o' |grep "T\|t\|U\|u" |cut -c 12-|sort|
uniq"); the problem is that if we match 1 or more strings in grep the
argument must be in quotes i.e T\|t\|U\|u must be withing quotes ...how
do i escape them so that the above works fine?

open PIPE, 'nm -r *.o |' or die "Cannot open pipe from nm $!";

my %data;
while ( <PIPE> ) {
next if /\.o/;
next unless /[tu]/i;
$data{ substr $_, 12 } = ();
}

close PIPE or warn $! ? "Error closing nm pipe: $!"
: "Exit status $? from nm";

print sort keys %data;


John
 
J

John W. Krahn

John said:
jeniffer said:
system("nm -r *.o|grep -v '.o' |grep "T\|t\|U\|u" |cut -c 12-|sort|
uniq"); the problem is that if we match 1 or more strings in grep the
argument must be in quotes i.e T\|t\|U\|u must be withing quotes ...how
do i escape them so that the above works fine?

open PIPE, 'nm -r *.o |' or die "Cannot open pipe from nm $!";

my %data;
while ( <PIPE> ) {
next if /\.o/;
next unless /[tu]/i;
$data{ substr $_, 12 } = ();
}

close PIPE or warn $! ? "Error closing nm pipe: $!"
: "Exit status $? from nm";

print sort keys %data;

And to avoid any shell interaction:

my $prog = 'nm';
my @switches = qw[ -r ];
my @files = glob '*.o';

open PIPE, '-|', $prog, @switches, @files
or die "Cannot open pipe from $prog $!";

my %data;
while ( <PIPE> ) {
next if /\.o/;
next unless /[tu]/i;
$data{ substr $_, 12 } = ();
}

close PIPE or warn $! ? "Error closing $prog pipe: $!"
: "Exit status $? from $prog";

print sort keys %data;



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,115
Latest member
JoshuaMoul
Top