[mdlug] Another sed/regex question

Aaron Kulkis akulkis00 at gmail.com
Mon Sep 29 01:29:13 EDT 2014


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}
>
> I know this can be done in one line of awk/perl/whatever but I'm trying to
> avoid additional dependencies.

awk is a given.  It's been a standard part of Unix since the mid 1970's.
It's a "dependency" ins the same way that libc is a dependency.. yes, it
IS a dependency...but you're not going to find a system out there that
fails to meet the dependency and still functions.



More information about the mdlug mailing list