Text processing tools and/or languages

Chris Green cl at isbd.net
Fri Jan 21 09:40:00 UTC 2022


On Thu, Jan 20, 2022 at 11:26:38PM -0500, Little Girl wrote:
> Hey there,
> 
> Chris Green wrote:
> 
> >Yes, but it's *not* how sed and awk work. :-)
> >
> >Both sed and awk feed the whole of the text past each line of the
> >script in turn, they don't process the text file line by line.  You
> >don't have a concept of "where you are in the text" as you progress
> >down your program.
> 
> How about a Bash one-liner using sed with a pipe?
> 
> sed -n '/Category/,$p' yourfile.txt | sed '0,/^$/{//d}'
> 
> The first command takes your file as input and prints nothing while
> looking for the specified pattern (Category), then prints from the
> first line matching the pattern to the end of the input. Then the
> output of that command is piped to another sed command that removes
> only the first blank line from the input and prints the output to the
> terminal window.
> 
My first attempt at doing this used a pipeline of sed commands and was
nearly there but not quite.  I think I could have made it work but
writing complex code using sed often/usually ends up with very messy
and difficult to maintain code.


> I'm not well-versed in sed, but just playing around because I love
> text manipulation. Someone more experienced could probably do all that
> with one sed command and no pipe.
> 
I've just about got to where I want using awk, it's not the ideal tool
but it's usually a bit easier to understand/maintain than sed scripts.

If it got much more complex I think I'd move to Python but that does
add quite a bit of coding overhead because it simply isn't so handy
for pattern matching and such.

-- 
Chris Green




More information about the ubuntu-users mailing list