system calls (mv + grep) within Perl script

W

William

code in question:
#!/usr/bin/perl -w

use strict;

my $InputFileDir = "/mkapp/webapps/mxrt-cgi/upload_vol/uploaded_files/";
my $savedfilename = "trs_vol.txt.49.22.224.132.LEUNGW5.20060106.13.11.42";
my $requiredList = "trs_vol.txt";
my $dummy_list = "dummylist.txt";

my $originalList = $InputFileDir . $savedfilename;
my $originalListBack = $InputFileDir . $requiredList;

open ( DUMMY_FD, $dummy_list ) || die "Cannot open file $dummy_list\n";
my @lines = <DUMMY_FD>;
close ( DUMMY_FD );

my $start;
my $ticker;
my $bo_fo_eligible;
my $lfo_eligible;

foreach my $currentLine ( @lines ) {
($start, $ticker, $bo_fo_eligible, $lfo_eligible) = ( split(/\|/,
$currentLine) );
system "grep ''$ticker' $originalList' >> $requiredList";
}


Error message:
grep: can't open AA
/mkapp/webapps/mxrt-cgi/upload_vol/uploaded_files/trs_vol.txt.49.22.224.132.LEUNGW5.20060106.13.11.42
grep: can't open SUNW
/mkapp/webapps/mxrt-cgi/upload_vol/uploaded_files/trs_vol.txt.49.22.224.132.LEUNGW5.20060106.13.11.42

contents of dummylist.txt:
|USD AA|1|0|[email protected]
|USD SUNW|0|1|[email protected]

contents of trs_vol.txt.49.22.224.132.LEUNGW5.20060106.13.11.42:
|USD AA|USD
NYSE|Securities|1D|0|ATM|18.222100000000001|18.222100000000001|^M
|USD AA|USD
NYSE|Securities|3D|0|ATM|19.222100000000001|19.222100000000001|^M
|USD AA|USD NYSE|Securities|1W|0|ATM|20.8015|20.8015|^M
|USD AA|USD NYSE|Securities|2W|0|ATM|21.4873|21.4873|^M
|USD AA|USD NYSE|Securities|3W|0|ATM|22.2561|22.2561|^M
|USD AA|USD
NYSE|Securities|1M|0|ATM|25.994400000000002|25.994400000000002|^M
|USD AA|USD NYSE|Securities|3M|0|ATM|25.1098|25.1098|^M
|USD AA|USD
NYSE|Securities|4M|0|ATM|25.313000000000002|25.313000000000002|^M
|USD AA|USD
NYSE|Securities|6M|0|ATM|25.668999999999997|25.668999999999997|^M
|USD AA|USD
NYSE|Securities|9M|0|ATM|26.104699999999998|26.104699999999998|^M
|USD AA|USD
NYSE|Securities|1Y|0|ATM|26.466299999999997|26.466299999999997|^M
|USD AA|USD
NYSE|Securities|1D|0|ATM|18.222100000000001|18.222100000000001|^M
|USD AA|USD
NYSE|Securities|3D|0|ATM|19.222100000000001|19.222100000000001|^M
|USD AA|USD NYSE|Securities|1W|0|ATM|20.8015|20.8015|^M
|USD AA|USD NYSE|Securities|2W|0|ATM|21.4873|21.4873|^M
|USD AA|USD NYSE|Securities|3W|0|ATM|22.2561|22.2561|^M
|USD AA|USD
NYSE|Securities|1M|0|ATM|25.994400000000002|25.994400000000002|^M
|USD AA|USD NYSE|Securities|3M|0|ATM|25.1098|25.1098|^M
|USD AA|USD
NYSE|Securities|4M|0|ATM|25.313000000000002|25.313000000000002|^M
|USD AA|USD
NYSE|Securities|6M|0|ATM|25.668999999999997|25.668999999999997|^M
|USD AA|USD
NYSE|Securities|9M|0|ATM|26.104699999999998|26.104699999999998|^M
|USD AA|USD
NYSE|Securities|1Y|0|ATM|26.466299999999997|26.466299999999997|^M

Question:

I am trying to grep for "USD AA" and "USD SUNW". What is the proper way
to "grep"?
 
X

xhoster

William said:
system "grep ''$ticker' $originalList' >> $requiredList"; ....

Error message:
grep: can't open AA
/mkapp/webapps/mxrt-cgi/upload_vol/uploaded_files/trs_vol.txt.49.22.224.1
32.LEUNGW5.20060106.13.11.42 grep: can't open SUNW
/mkapp/webapps/mxrt-cgi/upload_vol/uploaded_files/trs_vol.txt.49.22.224.1
32.LEUNGW5.20060106.13.11.42
...

I am trying to grep for "USD AA" and "USD SUNW". What is the proper way
to "grep"?

You could do the grep from within Perl. Then at least you would have a
real perl question, rather than a shell and/or gnu utility question.

Anyway, I'd think not sprinkling single quotes around at random would
help. Maybe this is what you meant?:

system "grep '$ticker' '$originalList' >> $requiredList";

You alse might need to chomp some stuff, or strip \r characters.

Xho
 
W

William

You could do the grep from within Perl. Then at least you would have a
real perl question, rather than a shell and/or gnu utility question.

I am relatively new to Perl.
how to "grep from within Perl" without using system?
 
I

it_says_BALLS_on_your forehead

William said:
I am relatively new to Perl.
how to "grep from within Perl" without using system?

#!/usr/bin/perl

use strict; use warnings;

my @old = qw( hello there keepme );
my @new = grep /keepme/ @old;

print "$_\n" for @new;
 
I

it_says_BALLS_on_your forehead

William said:
I am relatively new to Perl.
how to "grep from within Perl" without using system?

#!/usr/bin/perl

use strict; use warnings;

my @old = qw( hello there keepme );
my @new = grep /keepme/ @old;

print "$_\n" for @new;
 
I

it_says_BALLS_on_your forehead

Jim said:
Use regular expressions and the print command:

#!/usr/local/bin/perl
use strict;
use warnings;
my @search = (
'|USD AA|1|0|[email protected]',
'|USD SUNW|0|1|[email protected]'
);
my @data_lines = <DATA>;
foreach my $pattern ( @search ) {
my (undef, $ticker) = ( split(/\|/, $pattern), 2 );
print "\nLooking for $ticker:\n";
foreach ( @data_lines ) {
print if /\Q$ticker\E/;
}
}
__DATA__
|USD AA|USD NYSE|Securities|1D|0|ATM|18.222100000000001|...
|USD AA|USD NYSE|Securities|3D|0|ATM|19.222100000000001|...
|USD AA|USD NYSE|Securities|1W|0|ATM|20.8015|20.8015|
|USD AA|USD NYSE|Securities|2W|0|ATM|21.4873|21.4873|
|USD AA|USD NYSE|Securities|3W|0|ATM|22.2561|22.2561|
etc

if you have the array, using grep is cleaner. since it appears that you
don't need the array, you should just do a regex match in a while loop:

my $pattern = 'wanted';

while ( <DATA> ) {
# print or push to array if /$pattern/;
}
 
T

Tad McClellan

Subject: system calls (mv + grep) within Perl script


There is no "mv" anywhere in your article.

It there had been, then I would have pointed you to:

perldoc rename

but you didn't, so I won't.

#!/usr/bin/perl -w


Lexical warnings are much better than global warnings, so you should
enable warnings this way instead:

use warnings;

open ( DUMMY_FD, $dummy_list ) || die "Cannot open file $dummy_list\n";


You should include the _reason_ for the failure in your message.

It is also a good idea to put delimiters around the failed filename:

open ( DUMMY_FD, $dummy_list ) || die "Cannot open file '$dummy_list' $!";

my @lines = <DUMMY_FD>;
close ( DUMMY_FD );

foreach my $currentLine ( @lines ) {


Don't read the entire file into an array if all you plan to do is
process it line-by-line anyway.

Simply read it line-by-line in the first place:

while ( my $currentLine = said:
($start, $ticker, $bo_fo_eligible, $lfo_eligible) = ( split(/\|/,
$currentLine) );
system "grep ''$ticker' $originalList' >> $requiredList";
^^
^^ you have messed up the quoting
 
I

it_says_BALLS_on_your forehead

Jim said:
The OP's program was using grep to test a number of patterns against a
single file. The patterns were extracted from the lines in a second
file. Each pattern (e.g. 'USD AA') was used in a system call to the
grep utility.

The above program does the same thing, except it assigns the pattern
lines to the @search array rather than reading them from a file. This
was done for simplicity. The file containing the data lines is read
from the <DATA> special file handle, again for simplicity.

Your suggested modification will only test the file against one
pattern. Nor does it use grep, neither the built-in Perl grep nor the
external grep utility.

So, I don't exactly see what you are suggesting.

But, thanks for suggesting it anyway! :)

yeah, it was strange, i posted a response *twice* with a simple program
showing the OP how to use grep, but it never showed up.

anyway, my suggestion wasn't intended to be directly applicable to the
problem at hand, but more of a generic guide which i thought would be
more helpful to more people. and the post that i made (the one that
didn't show up) answered the OP's question. i'll rewrite it here: the
question was:

how to "grep from within Perl" without using system?

and my code was:

#!/usr/bin/perl

use strict; use warnings;

my @old = qw( hello there keepme );
my @new = grep /keepme/ @old;

print "$_\n" for @new;
 
I

it_says_BALLS_on_your forehead

Jim said:
The OP's program was using grep to test a number of patterns against a
single file. The patterns were extracted from the lines in a second
file. Each pattern (e.g. 'USD AA') was used in a system call to the
grep utility.

The above program does the same thing, except it assigns the pattern
lines to the @search array rather than reading them from a file. This
was done for simplicity. The file containing the data lines is read
from the <DATA> special file handle, again for simplicity.

Your suggested modification will only test the file against one
pattern. Nor does it use grep, neither the built-in Perl grep nor the
external grep utility.

So, I don't exactly see what you are suggesting.

But, thanks for suggesting it anyway! :)

yeah, it was strange, i posted a response *twice* with a simple program
showing the OP how to use grep, but it never showed up.

anyway, my suggestion wasn't intended to be directly applicable to the
problem at hand, but more of a generic guide which i thought would be
more helpful to more people. and the post that i made (the one that
didn't show up) answered the OP's question. i'll rewrite it here: the
question was:

how to "grep from within Perl" without using system?

and my code was:

#!/usr/bin/perl

use strict; use warnings;

my @old = qw( hello there keepme );
my @new = grep /keepme/ @old;

print "$_\n" for @new;
 
T

Tassilo v. Parseval

Also sprach Tad McClellan:
There is no "mv" anywhere in your article.

It there had been, then I would have pointed you to:

perldoc rename

but you didn't, so I won't.

And if you had had the intention to point him to rename(), you would
have written

perldoc -f rename

instead, right? ;-)

Tassilo
 
P

Paul Lalli

it_says_BALLS_on_your forehead said:
yeah, it was strange, i posted a response *twice* with a simple program
showing the OP how to use grep, but it never showed up.

yes, it did. Twice.

Welcome to Usenet. Articles are not and never have been guaranteed to
"show up" to any particular news server immediately, in any particular
order, or even at all.

Paul Lalli
 

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

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top