help-bash
[Top][All Lists]
Advanced

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

Extracting sections from code files


From: fatiparty
Subject: Extracting sections from code files
Date: Tue, 2 Nov 2021 10:24:33 +0100 (CET)

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



reply via email to

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