qx and awk for array

R

Rocky Allen

Hi Y'all,

Having trouble with this command when placed in a perl script:

cat /tmp/filesontape | awk '{print $9}'

on the command line output is as follows:
/etc/LGTOuscsi/sjirdtag
/etc/LGTOuscsi/sjirelem
/etc/LGTOuscsi/sjirjc

output from script is as follows:
-rwxr-xr-x 1 root root 550592 Feb 12 2002 /etc/LGTOuscsi/sjirdtag
-rwxr-xr-x 1 root root 550816 Feb 12 2002 /etc/LGTOuscsi/sjirelem
-rwxr-xr-x 1 root root 550544 Feb 12 2002 /etc/LGTOuscsi/sjirjc

I read perldoc perlop for qx and it claims that it honors pipes, and
wildcards etc. any thoughts?

here is the script:

#!/usr/bin/perl

my $cpioin = '/tmp/cpioin';
my $putontape = '/tmp/filesontape';

open(FH3, "$cpioin") or die "cant get FH3 $cpioin:$!";

my @pretape = <FH3>;
close FH3;

my @posttape = qx/cat $putontape | awk '{print $9}'/;

print "@posttape";

Thanks in advance,
Rocky
 
P

Paul Lalli

Rocky said:
Having trouble with this command when placed in a perl script:

cat /tmp/filesontape | awk '{print $9}'

I read perldoc perlop for qx and it claims that it honors pipes, and
wildcards etc. any thoughts?

here is the script:

#!/usr/bin/perl

It is massively rude to ask hundreds of people to do the work of a
computer for you. If you had enabled warnings, like the posting
guidelines tell you to, it would have told you what the problem was.
my $cpioin = '/tmp/cpioin';
my $putontape = '/tmp/filesontape';

open(FH3, "$cpioin") or die "cant get FH3 $cpioin:$!";

useless use of double quotes.
perldoc -q quoting
my @pretape = <FH3>;
close FH3;

my @posttape = qx/cat $putontape | awk '{print $9}'/;

qx// interpolates variables, just like qq// (ie, double-quoted
strings). You'r e using the perl variable $9 in that example, which
(since you haven't previously used a pattern match with 9 capturing
parentheses) is undefined.

Backslash the dollar sign.
print "@posttape";

(not only useless, but destructive, use of double quotes)

Didn't you wonder why you're getting an extra space on every line but
the first?

perldoc -q spaces

Paul Lalli
 
R

Rocky Allen

It is massively rude to ask hundreds of people to do the work of a
computer for you. If you had enabled warnings, like the posting
guidelines tell you to, it would have told you what the problem was.

I enabled warnings(use warnings;) on the script and got no warning
useless use of double quotes.
perldoc -q quoting
got it. Thank you

qx// interpolates variables, just like qq// (ie, double-quoted strings).
You'r e using the perl variable $9 in that example, which (since you
haven't previously used a pattern match with 9 capturing parentheses) is
undefined.

Backslash the dollar sign. Thank you very much.


(not only useless, but destructive, use of double quotes)

Didn't you wonder why you're getting an extra space on every line but
the first?
it was only to view the array contents, but I will remember that.
perldoc -q spaces


Anyway, thanks for your help.
 
P

Paul Lalli

Rocky said:
I enabled warnings(use warnings;) on the script and got no warning

Sorry, but I completely fail to believe you...

$ cat clpm.pl
#!/usr/bin/perl
use strict;
use warnings;

my $putontape = '/tmp/lallip_tmp';

my @posttape = qx/cat $putontape | awk '{print $9}'/;

print @posttape;

$ cat /tmp/lallip_tmp
-rwxr-xr-x 1 root root 550592 Feb 12 2002
/etc/LGTOuscsi/sjirdtag
-rwxr-xr-x 1 root root 550816 Feb 12 2002
/etc/LGTOuscsi/sjirelem
-rwxr-xr-x 1 root root 550544 Feb 12 2002
/etc/LGTOuscsi/sjirjc

$ perl clpm.pl
Use of uninitialized value in concatenation (.) or string at clpm.pl
line 7.
-rwxr-xr-x 1 root root 550592 Feb 12 2002
/etc/LGTOuscsi/sjirdtag
-rwxr-xr-x 1 root root 550816 Feb 12 2002
/etc/LGTOuscsi/sjirelem
-rwxr-xr-x 1 root root 550544 Feb 12 2002
/etc/LGTOuscsi/sjirjc
Thank you very much.

You are quite welcome

Paul Lalli
 
J

John W. Krahn

Rocky said:
Having trouble with this command when placed in a perl script:

cat /tmp/filesontape | awk '{print $9}'

The way to do that in perl is:


open IN, '<', '/tmp/filesontape' or die "Cannot open /tmp/filesontape: $!";
while ( <IN> ) {
my @fields = split;
print "$fields[8]\n";
}
close IN;



John
 
W

William James

John said:
Rocky said:
Having trouble with this command when placed in a perl script:

cat /tmp/filesontape | awk '{print $9}'

The way to do that in perl is:


open IN, '<', '/tmp/filesontape' or die "Cannot open /tmp/filesontape: $!";
while ( <IN> ) {
my @fields = split;
print "$fields[8]\n";
}
close IN;

I think you would find it more pleasant in Ruby:

IO.foreach("junk"){ |line|
puts line.split[8]
}


Or if the field is the last:

IO.foreach("junk"){ |line|
puts line.split.last
}
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top