Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > system calls (mv + grep) within Perl script

Reply
Thread Tools

system calls (mv + grep) within Perl script

 
 
William
Guest
Posts: n/a
 
      01-06-2006
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.4 2
grep: can't open SUNW
/mkapp/webapps/mxrt-cgi/upload_vol/uploaded_files/trs_vol.txt.49.22.224.132.LEUNGW5.20060106.13.11.4 2

contents of dummylist.txt:
|USD AA|1|0|
|USD SUNW|0|1|

contents of trs_vol.txt.49.22.224.132.LEUNGW5.20060106.13.11.4 2:
|USD AA|USD
NYSE|Securities|1D|0|ATM|18.222100000000001|18.222 100000000001|^M
|USD AA|USD
NYSE|Securities|3D|0|ATM|19.222100000000001|19.222 100000000001|^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.994 400000000002|^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.313 000000000002|^M
|USD AA|USD
NYSE|Securities|6M|0|ATM|25.668999999999997|25.668 999999999997|^M
|USD AA|USD
NYSE|Securities|9M|0|ATM|26.104699999999998|26.104 699999999998|^M
|USD AA|USD
NYSE|Securities|1Y|0|ATM|26.466299999999997|26.466 299999999997|^M
|USD AA|USD
NYSE|Securities|1D|0|ATM|18.222100000000001|18.222 100000000001|^M
|USD AA|USD
NYSE|Securities|3D|0|ATM|19.222100000000001|19.222 100000000001|^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.994 400000000002|^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.313 000000000002|^M
|USD AA|USD
NYSE|Securities|6M|0|ATM|25.668999999999997|25.668 999999999997|^M
|USD AA|USD
NYSE|Securities|9M|0|ATM|26.104699999999998|26.104 699999999998|^M
|USD AA|USD
NYSE|Securities|1Y|0|ATM|26.466299999999997|26.466 299999999997|^M

Question:

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

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      01-06-2006
William <> wrote:

> 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

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
William
Guest
Posts: n/a
 
      01-06-2006
On Fri, 6 Jan 2006, wrote:

> William <> wrote:
>
>> 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.


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


 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-06-2006

William wrote:
> On Fri, 6 Jan 2006, wrote:
>
> > William <> wrote:
> >
> >> 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.

>
> 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;

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-06-2006

William wrote:
> On Fri, 6 Jan 2006, wrote:
>
> > William <> wrote:
> >
> >> 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.

>
> 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;

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-06-2006

Jim Gibson wrote:
> In article
> < s.uwaterloo.ca>,
> William <> wrote:
>
> > On Fri, 6 Jan 2006, wrote:
> >
> > > William <> wrote:
> > >
> > >> system "grep ''$ticker' $originalList' >> $requiredList";
> > > ...

>
> > >>
> > >> 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.

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

>
> Use regular expressions and the print command:
>
> #!/usr/local/bin/perl
> use strict;
> use warnings;
> my @search = (
> '|USD AA|1|0|',
> '|USD SUNW|0|1|'
> );
> 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/;
}

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-06-2006
William <> wrote:


> 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 = <DUMMY_FD> ) {

> ($start, $ticker, $bo_fo_eligible, $lfo_eligible) = ( split(/\|/,
> $currentLine) );
> system "grep ''$ticker' $originalList' >> $requiredList";

^^
^^ you have messed up the quoting


--
Tad McClellan SGML consulting
Perl programming
Fort Worth, Texas
 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-07-2006

Jim Gibson wrote:
> In article < .com>,
> it_says_BALLS_on_your forehead <> wrote:
>
> > Jim Gibson wrote:
> > > In article
> > > < s.uwaterloo.ca>,
> > > William <> wrote:
> > >
> > > > On Fri, 6 Jan 2006, wrote:
> > > >
> > > > > William <> wrote:
> > > > >
> > > > >> system "grep ''$ticker' $originalList' >> $requiredList";
> > > > > ...
> > >
> > > > >>
> > > > >> 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.
> > > >
> > > > I am relatively new to Perl.
> > > > how to "grep from within Perl" without using system?
> > >
> > > Use regular expressions and the print command:
> > >
> > > #!/usr/local/bin/perl
> > > use strict;
> > > use warnings;
> > > my @search = (
> > > '|USD AA|1|0|',
> > > '|USD SUNW|0|1|'
> > > );
> > > 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/;
> > }
> >

>
> 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;

 
Reply With Quote
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      01-07-2006

Jim Gibson wrote:
> In article < .com>,
> it_says_BALLS_on_your forehead <> wrote:
>
> > Jim Gibson wrote:
> > > In article
> > > < s.uwaterloo.ca>,
> > > William <> wrote:
> > >
> > > > On Fri, 6 Jan 2006, wrote:
> > > >
> > > > > William <> wrote:
> > > > >
> > > > >> system "grep ''$ticker' $originalList' >> $requiredList";
> > > > > ...
> > >
> > > > >>
> > > > >> 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.
> > > >
> > > > I am relatively new to Perl.
> > > > how to "grep from within Perl" without using system?
> > >
> > > Use regular expressions and the print command:
> > >
> > > #!/usr/local/bin/perl
> > > use strict;
> > > use warnings;
> > > my @search = (
> > > '|USD AA|1|0|',
> > > '|USD SUNW|0|1|'
> > > );
> > > 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/;
> > }
> >

>
> 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;

 
Reply With Quote
 
Tassilo v. Parseval
Guest
Posts: n/a
 
      01-07-2006
Also sprach Tad McClellan:

> William <> wrote:
>
>
>> 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.


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

perldoc -f rename

instead, right?

Tassilo
--
use bigint;
$n=71423350343770280161397026330337371139054411854 220053437565440;
$m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($ m+=<=200);
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble running Perl script from within a Perl script laredotornado@zipmail.com Perl Misc 4 07-29-2011 01:44 PM
Are system calls sometimes costlier than library calls? Richard Tobin C Programming 24 11-11-2007 08:52 AM
Interactive System calls within Ruby Script ZyLo Ruby 2 09-20-2007 06:23 PM
shell script with arguments within perl script jay Ruby 7 05-03-2007 08:56 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57