Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.04; 'python.': 0.05; 'lines.': 0.05; 'python': 0.07; 'bash': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'wrote:': 0.14; 'awk': 0.16; 'grep': 0.16; 'outputs': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'subject:Convert': 0.16; 'subject:regex': 0.16; 'english.': 0.19; 'command': 0.19; 'wondering': 0.19; 'mine': 0.25; 'possible,': 0.25; 'script': 0.26; 'skip:[ 10': 0.26; 'id:': 0.27; 'tried': 0.27; 'problem': 0.29; 'sort': 0.30; 'from:addr:web.de': 0.31; 'all,': 0.31; 'to:addr:python-list': 0.32; 'another': 0.32; 'done': 0.32; 'lines': 0.34; 'using': 0.34; 'header:X-Complaints- To:1': 0.34; 'certain': 0.34; 'file.': 0.34; 'clearly': 0.34; 'file': 0.35; 'describe': 0.35; 'print': 0.35; 'stuck': 0.35; 'plain': 0.36; 'getting': 0.36; 'should': 0.37; 'execute': 0.38; 'but': 0.38; 'received:org': 0.38; 'log': 0.38; 'help': 0.39; 'cut': 0.39; 'to:addr:python.org': 0.39; 'could': 0.39; 'header :Mime-Version:1': 0.39; 'how': 0.39; 'takes': 0.40; 'would': 0.40; 'header:Received:5': 0.40; 'taking': 0.61; 'below': 0.63; 'reads': 0.65; 'moment': 0.67; 'have.': 0.73; 'skip:v 50': 0.91; '100,000': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Convert AWK regex to Python Date: Mon, 16 May 2011 12:07:27 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084908d.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1305540395 news.xs4all.nl 81482 [::ffff:82.94.164.166]:59450 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:5507 J wrote: > Good morning all, > Wondering if you could please help me with the following query:- > I have just started learning Python last weekend after a colleague of mine > showed me how to dramatically cut the time a Bash script takes to execute > by re-writing it in Python. I was amazed at how fast it ran. I would now > like to do the same thing with another script I have. > > This other script reads a log file and using AWK it filters certain fields > from the log and writes them to a new file. See below the regex the > script is executing. I would like to re-write this regex in Python as my > script is currently taking about 1 hour to execute on a log file with > about 100,000 lines. I would like to cut this time down as much as > possible. > > cat logs/pdu_log_fe.log | awk -F\- '{print $1,$NF}' | awk -F\. '{print > $1,$NF}' | awk '{print $1,$4,$5}' | sort | uniq | while read service > command status; do echo "Service: $service, Command: $command, Status: > $status, Occurrences: `grep $service logs/pdu_log_fe.log | grep $command | > grep $status | wc -l | awk '{ print $1 }'`" >> logs/pdu_log_fe_clean.log; > done > > This AWK command gets lines which look like this:- > > 2011-05-16 09:46:22,361 [Thread-4847133] PDU D > CC_SMS_SERVICE_51408_656-ServerThread- VASPSessionThread-7ee35fb0-7e87-11e0-a2da-00238bce423b-TRX > - 2011-05-16 09:46:22 - OUT - (submit_resp: (pdu: L: 53 ID: 80000004 > Status: 0 SN: 25866) 98053090-7f90-11e0-a2da-00238bce423b (opt: ) ) > > > And outputs lines like this:- > > CC_SMS_SERVICE_51408 submit_resp: 0 > > I have tried writing the Python script myself but I am getting stuck > writing the regex. So far I have the following:- For the moment forget about the implementation. The first thing you should do is to describe the problem as clearly as possible, in plain English.