[mdlug] Another sed/regex question

Jeff Hanson jhansonxi at gmail.com
Fri Sep 26 21:32:27 EDT 2014


The program is WeiDu, a mod installer for Infinity engine games (Baldur's
Gate, etc.) I'm generating a highlighted list based on component IDs that
will be installed.

weidu output (long lines will wrap in email):
[./chitin.key] 357 BIFFs, 54184 resources
[./dialog.tlk] 94183 string entries
[widescreen/tra/english/setup.tra] has 31 translation strings
~WIDESCREEN/WIDESCREEN.TP2~ #0 #0 // Widescreen Mod -> for the original
Infinity Engine (CHOOSE THIS!): Widescreen Mod v3.02
~WIDESCREEN/WIDESCREEN.TP2~ #0 #1 // Widescreen Mod -> for GemRB:
Widescreen Mod v3.02

First #0 is language ID and doesn't change.  The second #digit is the
component ID, followed by two undesirable forward slashes and the component
ID description.  Given an installed component ID of 0, output from current
sed mess is as follows (\x1b is sed replacement hex for ESC):

\x1b[1;32m0 Widescreen Mod -> for the original Infinity Engine (CHOOSE
THIS!): Widescreen Mod v3.02\x1b[0m
\x1b[0;31m1 Widescreen Mod -> for GemRB: Widescreen Mod v3.02\x1b[0m

The first matches and is green, the second doesn't and is red.  What I use
now works but is really ugly.  I'm just looking for a way to choose between
two different replacements based on a successful match.  The search pattern
uses grouping and the replacements use backreferences to select the ID and
description from the input.

On Fri, Sep 26, 2014 at 8:01 PM, Carl T. Miller <carl at carltm.com> wrote:

> Jeff Hanson wrote:
> > Have a input with mixed lines of data, some desirable, some not.  Valid
> > lines have a particular text and digit pattern which needs to be
> extracted
> > (very predictable).  If a line contains a particular number then it is
> > output encapsulated by one set of ASCII terminal escape codes for setting
> > text color.  If it has a non-matching number then it is output with a
> > different set of codes.  I need something like:
> >
> > program_output | sed
> > s/match_pattern/MATCHCODE_cleaned-up_output_RESETCODE
> > !NOMATCHCODE_cleaned-up_output_RESETCODE
> >
> > I'm not sure how to combine the match/no-match replacements in one
> > operation.  Currently I'm using:
> >
> > program_output | sed
> > -ne s/match_pattern/MATCHCODE_cleaned-up_output_RESETCODE/p
> > -ne
> >
> /match_pattern/!{s/valid_data_pattern/NOMATCHCODE_cleaned-up_output_RESETCODE/p}
>
> Jeff, I'm having trouble visualizing what you're trying to do.
> Can you send some sample bogus data before and after running sed?
> Maybe something like this?
>
> Code1 good data
> Code2 bad data
> Code1 more good data
>
> Esc[0;32;mgood dataEsc[0;30;m
> Esc[0;31;mbad dataEsc[0;30;m
> Esc[0;32;mmore good dataEsc[0;30;m
>
> > I know this can be done in one line of awk/perl/whatever but I'm trying
> to
> > avoid additional dependencies.
>
> Hmm.  You are doing this in a single instance of sed, so I
> would call what you have a one-liner.  If you're going for
> speed, what you have will probably run quicker than a one-liner
> in awk or perl.
>
> c
>
>
> _______________________________________________
> mdlug mailing list
> mdlug at mdlug.org
> http://mdlug.org/mailman/listinfo/mdlug
>


More information about the mdlug mailing list