shell command inside perl script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    shell command inside perl script

    I am trying to use a shell grep command inside a perl script using system command as below.

    Code:
     system "cat $source_path/s_${i}_${j}_0*testing.txt | grep '     $1'  | head > $path/s_${i}_${j}.txt";
    I am trying to grep for lines that ends with 1 inside the system command. system command concatenates everything and writing into output file and grep ' $1' does not work. I tried with double quotes too but throwing error. Anything wrong with the syntax?
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Lilly,

    Please forgive me, as some of my information may be out of date here, but...

    First off, the default grep program searches for simple strings - I think you want to use egrep, or one of the grep switches, which enables regular expressions.

    Second, your regular expression is probably wrong. You say you want a "1" at the end of a line, which would be the regex '1$'; not the expression ' $1', which probably searches for the six character string consisting of four spaces, a dollar sign, and the numeral one.

    Try:
    Code:
    system "cat $source_path/s_${i}_${j}_0*testing.txt | egrep '1$'  | head > $path/s_${i}_${j}.txt";
    as a first cut. If the system complains about lack of egrep, then you'll have to find the correct grep command line switch(es).

    Also, please try prototyping your system command from the command prompt - it'll give you a lot better feedback as to why things are (or are not) working. Once your prototype works as desired, then you can hoist it into your Perl code.

    Good Luck!

    Comment

    • lilly07
      New Member
      • Jul 2008
      • 89

      #3
      Hi,
      Thx for your response. My data is tab delimited columns. Hence I wanted to grep lines with last column as 1. So I used (control+v then tab key to get
      ' $1' the last column which ends with 1. I will try with egrep. When I just use a she ll script with above description, it works and hence I constructed as system command inside my perl script. But it doesn't work as my syntax is wrong. Thanks again.

      Comment

      • Oralloy
        Recognized Expert Contributor
        • Jun 2010
        • 988

        #4
        lilly,

        Do yourself a favor and try printing out the command that is being constructed, just to be certain that it is what you want:

        Code:
        $cmd = "cat $source_path/s_${i}_${j}_0*testing.txt | egrep '1$'  | head > $path/s_${i}_${j}.txt"; 
        print $cmd;
        I wasn't really not sure why you used $1 as your search pattern. If it's working, then we have to modify your script just a little bit to do the correct insert, otherwise Perl will try to substitute the first pattern match result from the last match (unless that's what you want).

        Perhaps try:
        Code:
        my $pat = chr(9) . '$1';
        my $cmd = "cat ${source_path}/s_${i}_${j}_0*testing.txt | grep '     ${pat}'  | head > ${path}/s_${i}_${j}.txt";
        
        system $cmd;
        Good Luck!!
        Oralloy

        Comment

        • lilly07
          New Member
          • Jul 2008
          • 89

          #5
          Thx Orally for you help. Finally
          Code:
          egrep '    1\$\'
          worked.

          Comment

          Working...