1754368 Members
4413 Online
108813 Solutions
New Discussion юеВ

Re: Perl scriptin help

 
Rajesh SB
Esteemed Contributor

Perl scriptin help

Hi Scritping Gurus,

I am beginner in Perl Scripting. Looking for help.

I want search for the ERROR message in a log file based on Keyword and detected line message text needs to be truncated to 90 characters.

Thanks in advance.

Regards,
Rajesh
10 REPLIES 10
RAC_1
Honored Contributor

Re: Perl scriptin help

My perl skills are same as yours. Just plain grep and cut here.

grep -i "ERROR" your_file | cut -c1-90
There is no substitute to HARDWORK
Ralph Grothe
Honored Contributor

Re: Perl scriptin help

TIMTOWTDI,

e.g. one possible way


perl -ne 'printf"%s\n", substr($_,0,30) if /error/i' /var/adm/syslog/syslog.log
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: Perl scriptin help

Oops, forgot it was the leading 90 chars you are after.
So substitute 30 by 90 in the substr() call.

Also have a look at

perldoc -f substr
perldoc perlre
perldoc perlop
Madness, thy name is system administration
Muthukumar_5
Honored Contributor

Re: Perl scriptin help

You can use awk / cut / perl on this simple requirement.

awk '// { substr($0,1,90); print; }'

-Muthu
Easy to suggest when don't know about the problem!
Rajesh SB
Esteemed Contributor

Re: Perl scriptin help

Thanks for your prompt replies.
I need help on another scenario like.

Looking for shell command to trim the message text to length to n characters.

Each message text strings needs to be trimed to n no. of characters.

Sure you guys have some good ideas.

Thanks & Regards,
Rajesh
RAC_1
Honored Contributor

Re: Perl scriptin help

Again cut is the command that you need.

echo "your_string"|cut -c1-5
Will cut first 5 chars.
There is no substitute to HARDWORK
Muthukumar_5
Honored Contributor

Re: Perl scriptin help

Use script as,

#!/bin/ksh
# Input
pattern=""
count=""
file=""

grep "${pattern}" ${file} | cut -c1-${count}

# end
exit 0

-Muthu
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: Perl scriptin help

with perl:

perl -e '$count=5;$pattern="no";while(<>){ printf "%s\n", substr($_,1,$count) if /$pattern/; }' ..

Change $count value from 5 to your need. And $pattern from no to someother string in "".

-Muthu
Easy to suggest when don't know about the problem!
Rajesh SB
Esteemed Contributor

Re: Perl scriptin help

Thanks for quick response. I got it.

Cheers,
Rajesh