Some regular-expressions-foo

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

traviscj
Posts: 22
Joined: Wed Mar 07, 2007 5:11 am UTC

Some regular-expressions-foo

Postby traviscj » Sat Mar 01, 2008 7:54 am UTC

Hi guys,

So, I hope this is an appropriate forum for this. Here goes:

I have this program that prints out a status bar to (presumably) a terminal. Thus, it prints the following:

Code: Select all

[.................] 0%\r[#####.......] 50%\r[#########] 100%\n


It looks pretty decent on a terminal, but the problem is this is part of a nightly cron-job which gets emailed to me and a few other admins. And it actually prints out basically each percentage. So what should be 12 line email(at tops) is like a couple hundred lines and incredibly obnoxious. Honestly I'd rather write a Python parser for the real xml logfile the program outputs... but I need to get some stuff actually done, and I want it to stop annoying me in the mean time. Can anyone spot a regular expression or have already written a program to reduce it down to the final status, what it'd look like on the terminal?

Thanks!

Travis

Karrion
Posts: 92
Joined: Fri Jun 22, 2007 12:14 am UTC
Location: Melbourne, AU

Re: Some regular-expressions-foo

Postby Karrion » Sat Mar 01, 2008 9:19 am UTC

If you control the source of the program generating the output, you could add a command line option to suppress the output, or detect whether the output is a terminal with isatty() (on a posix system).

Otherwise you could try some combination of the head and tail commands to give you the lines you need...

btilly
Posts: 1877
Joined: Tue Nov 06, 2007 7:08 pm UTC

Re: Some regular-expressions-foo

Postby btilly » Sat Mar 01, 2008 9:22 am UTC

Code: Select all

s/.*\r//g
Some of us exist to find out what can and can't be done.

Others exist to hold the beer.

traviscj
Posts: 22
Joined: Wed Mar 07, 2007 5:11 am UTC

Re: Some regular-expressions-foo

Postby traviscj » Sun Mar 02, 2008 8:14 am UTC

btilly wrote:

Code: Select all

s/.*\r//g


I *knew* it was gonna be something simple like that. Thanks! That works perfectly.

I think I was trying to make it too complicated. Ahh well. Lesson learned.

And thanks, Karrion, now I know how to not write broken code in my own software, someday. Most of the reason I wanted to avoid the head/tail method was.... they aren't on different lines! But about as important was that the program needed to find unexpected output just as easily. With the sed solution, as long as we don't have:

Code: Select all

somethingimportant\r something else important
, we don't lose anything important.

Travis


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests