LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-08-2013, 09:13 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
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

Last edited by casperdaghost; 11-08-2013 at 09:16 PM.
 
Old 11-09-2013, 12:25 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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
 
Old 11-12-2013, 10:03 AM   #3
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Original Poster
Rep: Reputation: 16
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.

Last edited by casperdaghost; 11-12-2013 at 10:17 AM.
 
Old 11-12-2013, 10:33 AM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,117

Rep: Reputation: 330Reputation: 330Reputation: 330Reputation: 330
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
#-----------------------------------------------------------------------#
 
Old 11-13-2013, 03:45 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,254

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
How about:
Code:
crontab -l | perl -nle 'print $1 if /\/(\w+\.(scr|cmd))/'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Confusing issue with Perl regEx - Regex check seems to require variable being set EnderX Programming 1 09-07-2013 05:36 AM
Perl One Liner : Capturing more than one matched regex per line and printing uncle-c Programming 6 01-22-2012 05:16 AM
[SOLVED] Perl one liner - problem with printing captured regex on new line uncle-c Programming 2 01-18-2012 06:15 AM
[SOLVED] differences between shell regex and php regex and perl regex and javascript and mysql golden_boy615 Linux - General 2 04-19-2011 02:10 AM
Perl to find regex and print following 5 lines after regex casperdaghost Linux - Newbie 3 08-29-2010 09:08 PM


All times are GMT -5. The time now is 02:28 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration