LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   regex or statement with perl one liner (https://www.linuxquestions.org/questions/linux-newbie-8/regex-or-statement-with-perl-one-liner-4175483958/)

casperdaghost 11-08-2013 08:13 PM

regex or statement with perl one liner
 
So i have this nasty. long crontab file - here is a sample
Code:

##--------------------------------------
#00 18 * * 1-5 /production/cronjobs/FbEmailF50GovtSunTrd/web_FbEmailF50GovtSunTrd.cmd 1>/tmp/web_FbEmailF50GovtSunTrd.log 2>&1
45 06 * * * /production/java/apache-tomcat-5.5.27/bin/restart.sh
#00 18 * * 1-5 /production/cronjobs/web_StockRecPosF50_rpt.cmd 1>/production/log/web_StockRecPosF50_rpt.log 2>&1
#40 7-17 * * 1-5 /production/cronjobs/rims_trade_dump.scr rims_tickets/gkrk8@voice1.shadow > /production/log/rims_trade_dump.log 2>&1
#20 18 * * 1-5 /production/cronjobs/rims_trade_dump.scr rims_tickets/gkrk8@voice1.shadow > /production/log/rims_trade_dump.log 2>&1
#TT24386## run CustCommDiff report @ 5:20pm every Thursday
#TT24386#20 17 * * 4 /production/web/cgi-bin/CustCommDiffCron.sh > /production/log/CustCommDiffCron.log 2>&1
#-----------------------------------------------------------------------#


what i want to parse them down to is the relative command names - no path, no log file path, just the command names.

the above sample, respectvely - this is what i need.

Code:

web_FbEmailF50GovtSunTrd.cmd
restart.sh
web_StockRecPosF50_rpt.cmd
rims_trade_dump.scr
rims_trade_dump.scr

CustCommDiffCron.sh

so i use this short script - which gives me what i need - kinda, but not really

Code:

cat /tmp/foo | perl -nle 'print /\/(\w+\.[scr|cmd])/'
web_FbEmailF50GovtSunTrd.c
restart.s
web_StockRecPosF50_rpt.c
rims_trade_dump.s
rims_trade_dump.s

CustCommDiffCron.s

See it only iterates between the first two letter of the or statement between scr and cmd.
I thought that brackets and a or '|' would say look for a word ending in '.cmd' or a word ending in '.scr'

I have tried :
Code:

cat /tmp/foo | perl -nle 'print /\/(\w+\.[scr-cmd])/'
or
cat /tmp/foo | perl -nle 'print /\/(\w+\.[scr,cmd])/'
or
cat /tmp/foo | perl -nle 'print /\/(\w+\.[scr cmd])/'

but they don't work. i can't use parenthesis because that would mess up the perl capture.
i want to do something like [cmd|sh|src] - right now the [cmd|src] is picking up both the sh and the src but i then i have to go through the whole crontab to see what they are - are they ending in .sh or .src. and this is a big crontab trust me. my eyes hurt just looking at it. i am going to need to differentiate between the .sh and .src and for that matter those scripts ending in .cmd

grail 11-08-2013 11:25 PM

That would be because [] is for a range of characters, hence \.[scr|cmd] says, a period followed by one of s,c,r,|,c,m,d ... which is what was returned

Try using () instead which is a grouping, so \.(scr|cmd) says, a period followed by either scr or cmd

casperdaghost 11-12-2013 09:03 AM

The () messes up the perl capture.
Code:

crontab -l |  perl -nle 'print /\/(\w+\.(scr|cmd))/'

emkt_comm_rpts.scrscr
getBtecMRO.scrscr
readMRO.scrscr
readMRO.scrscr
run_start_of_day.scrscr
run_init_day.scrscr
GovtUnRealP_GOVT.cmdcmd

Trying to get rid of the duplicate words by piping to a sed backreference.

haertig 11-12-2013 09:33 AM

The following works pretty good on your test data. Comment-out the "next if" line if you want to include commands that are commented out. The "next unless" line is really only useful when you have already commented out the "next if" line. "next unless" is doing a very basic check to see of the line "looks like" a crontab entry, so as to filter out the comment lines (that are no longer being skipped because you took out the "next if"). This "next unless" check fails and gives you one extraneous comment line in the output in the example below. You could make "next unless" a more sophisticated REGEX, or just manually edit out the occassional false hit from the results.

Code:

#!/usr/bin/perl

while (<DATA>) {
    next if /^\s*#/;
    next unless /\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+/;
    s {.*?/} {};
    s {\s.*} {};
    s {.*/} {};
    print;
}

__END__
##--------------------------------------
#00 18 * * 1-5 /production/cronjobs/FbEmailF50GovtSunTrd/web_FbEmailF50GovtSunTrd.cmd 1>/tmp/web_FbEmailF50GovtSunTrd.log 2>&1
45 06 * * * /production/java/apache-tomcat-5.5.27/bin/restart.sh
00 18 * * 1-5 /production/cronjobs/web_StockRecPosF50_rpt.cmd 1>/production/log/web_StockRecPosF50_rpt.log 2>&1
40 7-17 * * 1-5 /production/cronjobs/rims_trade_dump.scr rims_tickets/gkrk8@voice1.shadow > /production/log/rims_trade_dump.log 2>&1
20 18 * * 1-5 /production/cronjobs/rims_trade_dump.scr rims_tickets/gkrk8@voice1.shadow > /production/log/rims_trade_dump.log 2>&1
# run CustCommDiff report @ 5:20pm every Thursday
20 17 * * 4 /production/web/cgi-bin/CustCommDiffCron.sh > /production/log/CustCommDiffCron.log 2>&1
#-----------------------------------------------------------------------#


grail 11-13-2013 02:45 AM

How about:
Code:

crontab -l | perl -nle 'print $1 if /\/(\w+\.(scr|cmd))/'


All times are GMT -5. The time now is 09:50 AM.