Shell to PERL


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell to PERL
# 1  
Old 03-09-2012
Shell to PERL

Hi,

i must admit that am naive to scripting.

I got one shell script created which worked fine on linux.
Later i was told that this script is to run on MP-RAS(aka NCR Unix), which is quite an old version of Unix i understand.Smilie

in the shell script that i got created, i have made use of the mmin option of the find command.
now the problem is - this mmin option doesnt work on MP-RAS platform.

If i share the shell script in this forum, then can you please try redesign this shell script to PERL? the script is not a long script.

If anyone can please help me, let me know, i shall then post the shell script describing the requirement as well with it.

Thanks and Regards,
Utkarsh
# 2  
Old 03-09-2012
# 3  
Old 03-09-2012
Shell to Perl

Thanks Ygor.Smilie

Just thought to share the requirement as well with you.
I have 40 files that i need to keep parsing at regular intervals for a Specific Keyword if that appears.

"within the same single directory there are 41 log files to be monitored for a specfic KEYWORD at regular intervals.
These log files have .log as their filename extension. the content of these log files is in human readable form.

Now there are chances that ALL the log files may be updated in regular intervals with their respective application writing into them or may be just a few would be getting updated. now say for example, if the log files are read for the specific KEYWORD at regular intervals of 15 minutes as per the following schedule for example:

The first, very first time i implement this solution and run the Script(to look for the Log files content) say the time is 10am.OK. at this point of time the script would run and may be Parse through the content of ALL the log files and If or Whichever LOG file(s) it find the KEYWORD it would ALERT(by writing into some other file or watever the logic you use for this capturing).

OK at this point of time , at 10am, all the log files are parsed and the keyword matched for and alerted by the monitoring application.

NOW, the second time the monitor script runs is say at 10.15am. NOW, this monitor script SHOULD be ABLE TO ONLY AND ONLY look for and parse the CONTENT of ALL such LOG FILES which have got UPDATED after 10am(or in genral terms- THE previous RUN of the monitor script) till 10.15am.

say for example if ALL or some of these 41 log files got SOMENEW content as update after 10am, then the monitor script should ONLY Parse and MONITOR for the KEYWORD in the NEWLY added/appended content of the MODIFIED log file(s).
Say for example, when we run the script FOR THE VERY FIRST TIME (whenever we implement this solution)say at 1.00pm, then it should by default parse/read the content of all the 41 log files.and in this case, in which ever log files it finds the Keyword "ERROR", it would Alert for this keyword for all such Log files.
Coming back to our scenario. Now(however we define the schedule) say we get the script to run at 1.15pm as its next scheduled Run. then the SCRIPT SHOULD ONLY PICK UP the MODIFIED files within this FIFTEEN minutes interval. WATEVER the script has found till 1.00PM should NOT be considered NOW as this would have already been alerted for such log file at the 1.00PM run of script. Hence, the application team would be alerted for such log files through a ticket which our monitoring tool would generate for them at its 1.00 PM schedule. right.

similarly, when the script again runs at say 1.30PM, then it should ONLY look for the Log files that got MODIFIED between 1.15 PM to 1.30 PM. say, out of 41 log files may be ALL these are modfied or ONLY some are modified. WHICHEVER Log File(s) are modified between the scheduled 15 minutes RUN of the Script, pick up such FILES, LOOK for the APPENDED(newly added) content in the past FIFTEEN minutes ONLY and FIND if there is any ERROR keyword in such picked up files and alert for those. so the RUN of the script would be scheduled for 15 mins or 20 mins as per our choice."

Does mtime work in PERL, if relevant to this requirement of mine.Smilie
Incase you have any doubts on the requirement part please feel free to let me know.


Regards,
Utkarsh
# 4  
Old 03-09-2012
Is this a different problem from the original problem with which the thread was started?
# 5  
Old 03-09-2012
You could use grep -c to get a count of errors for each log file, and compare that to the counts fifteen minutes earlier. Something like...
Code:
if [[ ! -f errors.txt ]]
then # 1st time
   grep -c $RANDOM~~$RANDOM *.log > errors.txt
fi
mv errors.txt errors.old
grep -c ERROR *.log > errors.txt
comm -13 errors.old errors.txt | while read ERR
do
   echo do something with $ERR
done

# 6  
Old 03-09-2012
Just for fun, I thought I'd test it out...
Code:
$ cat sh.1
if [[ ! -f errors.txt ]]
then # 1st time
   grep -c $RANDOM~~$RANDOM *.log > errors.txt
fi
mv errors.txt errors.old
grep -c ERROR *.log > errors.txt
comm -13 errors.old errors.txt | while read ERR
do
   LOG=$(echo $ERR|cut -d: -f1)
   CNT=$(echo $ERR|cut -d: -f2)
   if [[ $CNT -gt 0 ]]
   then
       echo do something with file: $LOG which has error count: $CNT
   fi
done

$ echo ERROR > A.log

$ echo okay > B.log

$ sh sh.1
do something with file: A.log which has error count: 1

$ sh sh.1

$ echo ERROR >> B.log

$ echo ERROR > C.log

$ sh sh.1
do something with file: B.log which has error count: 1
do something with file: C.log which has error count: 1

$ echo ERROR >> A.log

$ echo ERROR >> B.log

$ sh sh.1
do something with file: A.log which has error count: 2
do something with file: B.log which has error count: 2

$ echo ERROR >> B.log

$ echo ERROR >> C.log

$ sh sh.1
do something with file: B.log which has error count: 3
do something with file: C.log which has error count: 2

$ rm errors.txt #--reset

$ sh sh.1
do something with file: A.log which has error count: 2
do something with file: B.log which has error count: 3
do something with file: C.log which has error count: 2

$

# 7  
Old 03-10-2012
No matrixmadhan... this is the same problem for which the thread was started.Smilie

---------- Post updated at 10:20 PM ---------- Previous update was at 10:18 PM ----------

Thanks Ygor.
I should try the option/suggestion given by you.
really thanks for this...would let you know of the results if meeting my expectation or we require some more improvement on this... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting shell to Perl I run into shell built in function trap and need alternative in Perl

I am working on converting shell to Perl script. In shell we have built in function trap Do you know alternative in Perl or actually we don't need it? Thanks for contribution (3 Replies)
Discussion started by: digioleg54
3 Replies

2. Shell Programming and Scripting

Shell / Perl help

Hi, here is my problem. I have a command line tool to process files. Now I have few hundreds of files to process. So I have written a perl script to generate the command dynamically using variables for the arguments. when I use backtick to execute those it creates error. the shell doesn't get the... (4 Replies)
Discussion started by: jith
4 Replies

3. Shell Programming and Scripting

HELP on Perl array / sorting - trying to convert Korn Shell Script to Perl

Hi all, Not sure if this should be in the programming forum, but I believe it will get more response under the Shell Programming and Scripting FORUM. Am trying to write a customized df script in Perl and need some help with regards to using arrays and file handlers. At the moment am... (3 Replies)
Discussion started by: newbie_01
3 Replies

4. Shell Programming and Scripting

Shell or Perl?

Hello, I am looking for some basic script help, I am currently tailing a log with a simple tail –f, combined with egrep –vi to remove some messages from the tail, i do not want to see. . I wanted to create something a little more complex were if it’s possible to script this into... (4 Replies)
Discussion started by: hamon
4 Replies

5. UNIX for Dummies Questions & Answers

perl through shell

Can we call a perl script/ commands through a shell script? (1 Reply)
Discussion started by: chn10db001
1 Replies

6. Shell Programming and Scripting

Shell to perl

Hi all experts, I am new to Perl. Could you please help me to convert the following SHELL script to PERL ? I will appreciate. #!/bin/bash #set -x fileday=$1 result_file=Total`date +%Y%m%d%H%M` out_folder=/home/cmd/pm_analyze tmp_folder=/tmp/pmtmp datatype='split licenceCounter... (4 Replies)
Discussion started by: jinanchen
4 Replies

7. Shell Programming and Scripting

Perl Vs Shell Programming

Can someone please tell me what the big deal about perl is? i have been doing shell programming for quite a number of years and I have to say, there's very little if any thing that I can't do in shell programming. i just need to investigate how to do it. so, my question is, does deep... (1 Reply)
Discussion started by: SkySmart
1 Replies

8. Shell Programming and Scripting

Perl - pass shell-vars into perl for input loop

I need to process a file line-by-line using some value from a shell variable Something like:perl -p -e 's/$shell_srch/$shell_replace/g' input.txt I can't make the '-s' work in the '-p' or '-n' input loop (or couldn't find a syntaxis.) I have searched and found... (4 Replies)
Discussion started by: alex_5161
4 Replies

9. UNIX for Dummies Questions & Answers

shell to perl

hi all, Can we change a shell script into a perl script instead of writing a fresh one in perl? Is there any utility present that can be used for this purpose? please help me. Thanks in advance.. (1 Reply)
Discussion started by: arthi
1 Replies

10. Shell Programming and Scripting

How to Run a shell script from Perl script in Parent shell?

Hi Perl/UNIX experts, I have a problem in running a shell script from my perl script (auto.pl). I run the perl script using perl auto.pl from the shell prompt The shell script picks the files in "input" folder and procesess it. The shell script blue.sh has this code. export... (16 Replies)
Discussion started by: hifake
16 Replies
Login or Register to Ask a Question