[Cialug] Regex Question

Tim Wilson tim_linux at wilson-home.com
Sat Oct 13 15:46:35 UTC 2018


Here's the whole script, including the calculation the max length of the
first word on the arrow lines:

#!/bin/bash
#let len=`grep '^ .*' <foo.txt | awk '{print $1}' | xargs -I% sh -c 'echo %
| wc -c' | sort -k1n | tail -1`-1
let len=`awk 'BEGIN {max=0} { if(NF > 1 && length($1) > max) max =
length($1) } END { print max }' foo.txt`
awk "{if(NF > 1) {printf \"    %-*s %s %s\n\", $len, \$1, \$2, \$3} else
{print \$1}}" foo.txt

The commented "let" line was my first attempt to get the max length of the
first word.  However, it was time consuming, so I opted to find another
way.  I use awk to get the max length and store it as a shell variable.
Then the second awk uses it for the length specifier.


On Fri, Oct 12, 2018 at 2:39 PM Tim Wilson <tim_linux at wilson-home.com>
wrote:

> I forgot the 4 leading spaces in front of the 1st %. That’s what I get for
> doing it on one computer and typing it into an email on another.
>
> I have another line that will figure out the length of the first indented
> word. It’s rather long and is composed of several commands joined by pipes.
> I’ll send it later.
>
> On Fri, Oct 12, 2018 at 1:30 PM Todd Walton <tdwalton at gmail.com> wrote:
>
>> On Fri, Oct 12, 2018 at 1:43 PM Tim Wilson <tim_linux at wilson-home.com>
>> wrote:
>>
>> > 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
>> >
>>
>> Nice.
>>
>> Also, jim, I don't use Perl or JavaScript. I know, I know, heresy. Turn in
>> my sysadmin card. Etc.
>>
>> --
>> Todd
>> _______________________________________________
>> Cialug mailing list
>> Cialug at cialug.org
>> http://cialug.org/mailman/listinfo/cialug
>>
> --
> Tim
> Required reading: http://bccplease.com/
>


-- 
Tim
Required reading: http://bccplease.com/


More information about the Cialug mailing list