[Cialug] Regex Question

Tim Wilson tim_linux at wilson-home.com
Fri Oct 12 17:42:04 UTC 2018


Does it have to be regex? I have a short awk script that should work:

awk ‘{if(NF > 1) {printf “%-9s %s %s\n”, $1, $2, $3} else {print $1}}’
<foo.txt

This worked on your example. If the data is more complex, then the script
would need to be modified.

If it has to be regex, I’d recommend playing around with the patterns on
regextester.com. I’ve used it before. It gives some examples and will
highlight the match.

On Fri, Oct 12, 2018 at 9:28 AM jim kraai <jimgkraai at gmail.com> wrote:

> 1. recent versions of perl, in the last ten years or so, can do this
> directly with its more advanced regex
> 2. if you're working in JavaScript, I think you want XRegExp
> 3. manually in two steps using an intermediate marker
> 4. you'll need to use a procedural language to deal with length
>
>
> On Fri, Oct 12, 2018, 08:31 Todd Walton <tdwalton at gmail.com> wrote:
>
> > I somewhat frequently run into this specific type of problem... Let's
> say I
> > have this text:
> >
> > group
> >     something  -->  blah
> >   another   -->           yes
> >
> > I want to line these up all pretty like. I want there to be, say, four
> > spaces at the start of the something and the another lines, then the
> word,
> > then a number of spaces that varies depending on the length of the first
> > word, and then -->, a space, and the final word. Like this:
> >
> > group
> >     something --> blah
> >     another   --> yes
> >
> > If your email reader can handle it, I typed those in in fixed width font,
> > for visual clarity. The problem is that no regex I've ever seen has any
> > facility for length. I could determine that there should be 14 total
> > characters before the -->, and then do this:
> >
> > search: ^\s+([a-z]{9})\s+-->
> > replace: \s\s\s\s\1 -->
> >
> > That would replace the something line. And then I would reduce the digit
> by
> > 1 and add 1 space before the -->. Then repeat. Then repeat. That's what I
> > usually do. But I wish I could somehow say "four spaces, then the
> captured
> > backreference, then a number of spaces equal to 10 minus the captured
> > backreference's length". I know that's a mental mouthful, but hopefully
> you
> > can see what I'm getting at. One would think that horizontal tabs were
> > invented for this very thing, but they don't always line up right,
> > especially if the variability in word length is high, and also I don't
> want
> > tab characters in the result.
> >
> > Y'all ever run into this kind of problem?
> >
> > --
> > Todd
> > _______________________________________________
> > Cialug mailing list
> > Cialug at cialug.org
> > http://cialug.org/mailman/listinfo/cialug
> >
> _______________________________________________
> Cialug mailing list
> Cialug at cialug.org
> http://cialug.org/mailman/listinfo/cialug
>
-- 
Tim
Required reading: http://bccplease.com/


More information about the Cialug mailing list