system calls (mv + grep) within Perl script

Discussion in 'Perl Misc' started by William, Jan 6, 2006.

  1. William

    William Guest

    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|
    |USD SUNW|0|1|

    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"?
     
    William, Jan 6, 2006
    #1
    1. Advertising

  2. William

    Guest

    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
     
    , Jan 6, 2006
    #2
    1. Advertising

  3. William

    William Guest

    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?
     
    William, Jan 6, 2006
    #3
  4. 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;
     
    it_says_BALLS_on_your forehead, Jan 6, 2006
    #4
  5. 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;
     
    it_says_BALLS_on_your forehead, Jan 6, 2006
    #5
  6. Jim Gibson wrote:
    > In article
    > <>,
    > 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/;
    }
     
    it_says_BALLS_on_your forehead, Jan 6, 2006
    #6
  7. 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
     
    Tad McClellan, Jan 6, 2006
    #7
  8. Jim Gibson wrote:
    > In article <>,
    > it_says_BALLS_on_your forehead <> wrote:
    >
    > > Jim Gibson wrote:
    > > > In article
    > > > <>,
    > > > 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;
     
    it_says_BALLS_on_your forehead, Jan 7, 2006
    #8
  9. Jim Gibson wrote:
    > In article <>,
    > it_says_BALLS_on_your forehead <> wrote:
    >
    > > Jim Gibson wrote:
    > > > In article
    > > > <>,
    > > > 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;
     
    it_says_BALLS_on_your forehead, Jan 7, 2006
    #9
  10. 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=71423350343770280161397026330337371139054411854220053437565440;
    $m=-8,;;$_=$n&(0xff)<<$m,,$_>>=$m,,print+chr,,while(($m+=8)<=200);
     
    Tassilo v. Parseval, Jan 7, 2006
    #10
  11. William

    Paul Lalli Guest

    it_says_BALLS_on_your forehead wrote:
    > 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
     
    Paul Lalli, Jan 7, 2006
    #11
  12. it_says_BALLS_on_your forehead <> wrote:


    > my @new = grep /keepme/ @old;



    That is a syntax error.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 7, 2006
    #12
  13. Tad McClellan wrote:
    > it_says_BALLS_on_your forehead <> wrote:
    >
    >
    > > my @new = grep /keepme/ @old;

    >
    >
    > That is a syntax error.


    oops.

    my @new = grep /keepme/, @old;

    thanks for the correction, Tad.
     
    it_says_BALLS_on_your_forehead, Jan 7, 2006
    #13
  14. it_says_BALLS_on_your_forehead wrote:
    > Tad McClellan wrote:
    > > it_says_BALLS_on_your forehead <> wrote:
    > >
    > >
    > > > my @new = grep /keepme/ @old;

    > >
    > >
    > > That is a syntax error.

    >
    > oops.
    >
    > my @new = grep /keepme/, @old;


    or alternatively:

    my @new = grep {/keepme/} @old;
     
    it_says_BALLS_on_your_forehead, Jan 7, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,824
  2. Richard Tobin
    Replies:
    24
    Views:
    800
  3. ZyLo
    Replies:
    2
    Views:
    290
    Marcin Raczkowski
    Sep 20, 2007
  4. jeniffer
    Replies:
    4
    Views:
    296
    John W. Krahn
    Mar 20, 2006
  5. Replies:
    4
    Views:
    298
    ccc31807
    Jul 29, 2011
Loading...

Share This Page