How to do pattern matching for multiple files in Perl

Discussion in 'Perl Misc' started by Kimi, Aug 28, 2007.

  1. Kimi

    Kimi Guest

    Hi,

    I am new to Perl and know ksh better. He is what I am doing with ksh

    "$ cat file_list
    file1.log
    file2.log

    $cat file_list | xargs -L 500 egrep -ci "pattern_to_search" /dev/null
    | grep -v "0$" | sort -b -t ":" -k4,4 -T "/tmp" > "summary_file"

    $ cat summary_file
    file1.log:2
    file2.log:2

    $cat file_list | xargs -L 500 egrep -A3 -i "pattern_to_search" | cut -
    c1-1024 > "detail_file"

    $cat detail_file
    file1.log: pattern_to_search 1
    file1.log: pattern_to_search 2
    file2.log: pattern_to_search 6
    file2.log: pattern_to_search 7"

    inside the ksh script, pattern.ksh
    ------------------------------------------------
    cat file_list | xargs -L 500 egrep -ci "pattern_to_search" /dev/null |
    grep -v "0$" | sort -b -t ":" -k4,4 -T "/tmp" > "summary_file"
    cat file_list | xargs -L 500 egrep -A3 -i "pattern_to_search" | cut -
    c1-1024 > "detail_file"


    And I was told that perl is faster than ksh. If so, how can we do the
    above operation using perl. I dont think using the above statement
    directly inside perl script as

    `cat file_list | xargs -L 500 egrep -ci "pattern_to_search" /dev/null
    | grep -v "0$" | sort -b -t ":" -k4,4 -T "/tmp" > "summary_file"`
    `cat file_list | xargs -L 500 egrep -A3 -i "pattern_to_search" | cut -
    c1-1024 > "detail_file"`

    is the right way. Any suggestions and pointers is appreciable.

    Thanks in Advance,
    Kimi
     
    Kimi, Aug 28, 2007
    #1
    1. Advertising

  2. Kimi

    Ben Morrow Guest

    Quoth Kimi <>:
    > Hi,
    >
    > I am new to Perl and know ksh better. He is what I am doing with ksh
    >

    <snip>
    > cat file_list | xargs -L 500 egrep -ci "pattern_to_search" /dev/null |
    > grep -v "0$" | sort -b -t ":" -k4,4 -T "/tmp" > "summary_file"
    > cat file_list | xargs -L 500 egrep -A3 -i "pattern_to_search" | cut -
    > c1-1024 > "detail_file"
    >
    > And I was told that perl is faster than ksh. If so, how can we do the
    > above operation using perl. I dont think using the above statement
    > directly inside perl script as
    >
    > `cat file_list | xargs -L 500 egrep -ci "pattern_to_search" /dev/null
    > | grep -v "0$" | sort -b -t ":" -k4,4 -T "/tmp" > "summary_file"`
    > `cat file_list | xargs -L 500 egrep -A3 -i "pattern_to_search" | cut -
    > c1-1024 > "detail_file"`
    >
    > is the right way. Any suggestions and pointers is appreciable.


    You can open a file with open, see perldoc -f open.

    You can read its contents with the <> operator, see perldoc perlop.

    You can filter a list with the grep function, see perldoc -f grep, and
    perldoc perlretut.

    You can sort a list with the sort function, see perldoc -f sort.

    You can extract a piece of a string with the substr function, see
    perldoc -f substr.

    If you have tried to put these pieces together and can't make it work,
    post what you have and we'll help you fix it.

    Ben

    --
    Razors pain you / Rivers are damp
    Acids stain you / And drugs cause cramp. [Dorothy Parker]
    Guns aren't lawful / Nooses give
    Gas smells awful / You might as well live.
     
    Ben Morrow, Aug 28, 2007
    #2
    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. Replies:
    4
    Views:
    976
    M.E.Farmer
    Feb 13, 2005
  2. Chad Kellerman

    pattern matching with multiple lists

    Chad Kellerman, Jul 16, 2010, in forum: Python
    Replies:
    0
    Views:
    203
    Chad Kellerman
    Jul 16, 2010
  3. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    246
    Marc Bissonnette
    Jan 13, 2004
  4. Darius
    Replies:
    4
    Views:
    100
    Anno Siegel
    Sep 5, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    242
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page