help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Stop printing when non-comment lines are reached


From: Hans Lonsdale
Subject: Re: Stop printing when non-comment lines are reached
Date: Thu, 2 Feb 2023 04:20:42 +0100 (CET)


> ----------------------------------------
> From: Greg Wooledge <greg@wooledge.org>
> Date: Feb 2, 2023, 7:31:59 AM
> To: <help-bash@gnu.org>
> Subject: Re: Stop printing when non-comment lines are reached
> 
> 
> On Wed, Feb 01, 2023 at 03:54:51PM +0100, Hans Lonsdale wrote:
> > I am printing between sections of the following form
> > 
> > ---------
> > ## FAML [ASMB] keyword,keyword  
> > 
> > ## Some text
> > 
> > ## END OF FAML [ASMB]
> > ---------
> > 
> > I have encountered a problem that occurs when "## END OF FAML [ASMB]" is 
> > not reached. 
> > I want to stop as soon as lines not starting with comment characters "##" 
> > are encountered.
> 
> Oh, now there's additional text on the starting line?  And those ##
> characters are actually part of the file?  Hrmph.
> 
> I should know from experience that the problem is just going to keep
> getting harder and harder and harder each time I propose an answer,
> and then the OP says "No, that's not right because..." but it's still
> frustrating.
> 
> The goal here is to AVOID doing stupid regular expression tricks, because
> the strings you're proposing as section headers are just that -- strings.
> They're not regexes.  So, we don't want to do regex matching on them.  If
> we did, then we would have to convert the strings to regexes first
> (inserting a boatload of backslashes in front of any characters on a
> certian list), and that's just painful.

You are quite right.  One ends up with painful boatload of backslashes.
 
> Anyway, we haven't reached that tipping point yet.  It can still be
> done using string operations on strings.  Here's the next iteration.
> I'm sure you're going to retort with yet another reason why this won't
> work (maybe the number of # characters is meant to be variable or
> something)....
> 
> unicorn:~$ cat foo
> #!/bin/sh
> faml="FM"
> asmb="TOBIN"
> exec awk -v marker="$faml [$asmb]" '
>   BEGIN                         {mark=0}
>   ! /^##/                       {exit}
>   index($0, "## " marker) == 1  {mark=1}
>   mark                          {print}
>   $0 == "## END OF " marker     {exit}
> ' "$@"
> 
> unicorn:~$ cat bar
> ## leading junk
> ## FM [TOBIN] keyword,keyword
> ## Some text
> ## Like this?
> ## NOT THE END OF FM [TOBIN]
> ## Still going!
> don't print this
> 
> unicorn:~$ ./foo bar
> ## FM [TOBIN] keyword,keyword
> ## Some text
> ## Like this?
> ## NOT THE END OF FM [TOBIN]
> ## Still going!
> 
> unicorn:~$ cat baz
> ## leading junk
> ## FM [TOBIN] keyword,keyword
> ## Some text
> ## Like this?
> ## END OF FM [TOBIN]
> ## Still going!
> don't print this
> 
> unicorn:~$ ./foo baz
> ## FM [TOBIN] keyword,keyword
> ## Some text
> ## Like this?
> ## END OF FM [TOBIN]
> 
> 


-- 
Sent with https://mailfence.com  
Secure and private email



reply via email to

[Prev in Thread] Current Thread [Next in Thread]