How to do pattern matching for multiple files in Perl

How to do pattern matching for multiple files in Perl

am 28.08.2007 15:23:36 von Kimi

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

Re: How to do pattern matching for multiple files in Perl

am 28.08.2007 16:55:11 von Ben Morrow

Quoth Kimi :
> Hi,
>
> I am new to Perl and know ksh better. He is what I am doing with 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.

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