python regex help

This is the place for queries that don't fit in any of the other categories.

python regex help

Postby jdinze » Thu Apr 10, 2014 1:49 pm

Code: Select all
anis_chars = re.compile('\\x1b\[7mlines [0-9]*-[0-9]* \\x1b\[27m\\x1b\[K\\r\\x1b\[K')
def strip_ansi(with_ansi):
   return ansi_chars.sub('', with_ansi)
strip_ansi (ssh.before)

ssh.before is some output from a pxssh obj.
the output returns some garbage ansi characters that i need to remove in order to proper parse the output.

sample output
Code: Select all
parts = \x1b[7mlines 24-46 \x1b[27m\x1b[K\r\x1b[KCPU 9
parts = \x1b[7mlines 25-47 \x1b[27m\x1b[K\r\x1b[K Utilization: 57%
parts = Peak Utilization Last Hour: 100% at 2014/04/09 07:07:12
parts = Avg. Utilization Last Hour: 67%
parts =
parts = \x1b[7mlines 48-70 \x1b[27m\x1b[K\r\x1b[K
parts = \x1b[7mlines 49-71 \x1b[27m\x1b[K\r\x1b[KCPU 14
parts = Utilization: 46%

Im using RHEL 5.8 and Python 2.7. the script works just fine when i run it as myself or even after using sudo bash. when i run it as a cron job, the regex doesnt match and i get output as if i didnt use the strip_ansi() function.

Ive done some research and learned that cron uses a different local, which plays a part in the way it handles some characters. so i tried running the cron job like this:
Code: Select all
*/1 **** export LANG=en_US.UTF-8; /pathtomyscript/
but no luck. i did confirm that
Code: Select all
 "export LANG=en_US.UTF-8"
actually sets the LANG variable for cron. i also checked for output errors and this script has none.

Is there a different way i can write the regex to consider the fact that cron uses a different locale? I'm also open up to other ideas, as this has been plaguing me for three days.

Last edited by Mekire on Thu Apr 10, 2014 2:14 pm, edited 1 time in total.
Reason: First post lock. Code tags added.
Posts: 1
Joined: Thu Apr 10, 2014 1:40 pm

Re: python regex help

Postby micseydel » Mon Apr 21, 2014 11:25 pm

Could you provide a very simple example of the strings that occur with cron, and what you want out of them? I think the reason you haven't gotten an answer to this is that there is a lot of extraneous information. If you reduce it to a simple regex question, you'll probably get an answer rather quickly.
Due to the reasons discussed here we will be moving to on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: Yahoo [Bot] and 12 guests