[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Extracting sections from code files
From: |
Daniel Mills |
Subject: |
Re: Extracting sections from code files |
Date: |
Tue, 2 Nov 2021 10:33:24 -0400 |
On Tue, Nov 2, 2021 at 9:38 AM fatiparty--- via <help-bash@gnu.org> wrote:
> Have written a bash function to print sections of text enclosed between
> lines matching `## mode: org` and `## # End of org` in a bash file, with an
> empty line between sections. Before the ##, there can be any number of
> spaces.
>
>
> capt ()
> {
> local begsec='## mode: org'
> local endsec='## # End of org'
>
> awk -v start="$begsec" -v end="$endsec" \
> '{
> if($0~start){want=1; next}
> if($0~end){want=0; print ""; next}
> gsub(/\s*#+\s*/,"");
> } want' "$1"
> }
>
> But it is important to require that $begsec and $endsec be
> the only things on the line (other than leading whitespace).
> Otherwise, the function will pick up
>
> `local begsec='## mode: org'`
>
> present in the file test.sh.
>
> file: test.sh
> capt ()
> {
> local begsec='## mode: org'
> local endsec='## # End of org'
>
> awk -v start="$begsec" -v end="$endsec" \
> '{
> if($0~start){want=1; next}
> if($0~end){want=0; print ""; next}
> gsub(/\s*#+\s*/,"");
> } want' "$1"
> }
>
> ## mode: org## * Using case statement## # End of orgcase $arg in ("V")
> echo "Author" ;; (*) ## mode: org ## ** Silent Error Reporting Mode
> (SERM) in getopts ## *** Detects warnings without printing built-in
> messages. ## *** Enabled by colon {:} as first character in shortopts.
> ## # End of org break ;;esac
>
>
Not really a bash question, more an awk question, by why not use == instead
of ~ if you want an exact match?