How to do pattern matching for multiple files in Perl

K

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
 
B

Ben Morrow

Quoth Kimi said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top