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: Wed, 3 Nov 2021 16:51:54 +0100 (CET)

Nov 2, 2021, 14:33 by danielmills1@gmail.com:

> 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.
>>
>> Not really a bash question, more an awk question, by why not use == instead 
>> of ~ if you want an exact match?
>>
Have updated the bash function to the following, but want to detect either the 
single character comment ([#;!C]) or the double character comment ([//|@c])
so I can remove the starting comments from the sections automatically

capture ()
{
local efile="$1"

local begrec endrec charcl

impl="2"
if [ "$impl" = "1" ]; then
   charcl='^[[:space:]]*(#|;|!)+[[:space:]]*'
elif [ "$impl" = "2" ]; then
   charcl='^[[:space:]]*(//|@c)[[:space:]]*'
fi

bego="${charcl}"'Mode: org$'
endo="${charcl}"'# End of org$'

awk -v ccls="$charcl" -v bego="$begorg" -v endo="$endorg" \
   '$0 ~ bego { insc=1; next }
    $0 ~ endo { insc=0; }
    insc { sub(/ccls/,""); print }' "$efile"
}

With the following file

## Mode: org
#  Assigns shell positional parameters or changes the values of shell
#  options.  The -- option assigns the positional parameters to the
#  arguments of {set}, even when some of them start with an option
#  prefix `-'.
## # End of org

;; Mode: org
;  Assigns shell positional parameters or changes the values of shell
;  options.  The -- option assigns the positional parameters to the
;  arguments of {set}, even when some of them start with an option
;  prefix `-'.
;; # End of org

@c Mode: org
@c Assigns shell positional parameters or changes the values of shell
@c options.  The -- option assigns the positional parameters to the
@c arguments of {set}, even when some of them start with an option
@c prefix `-'.
@c # End of org

I want the output to be

Assigns shell positional parameters or changes the values of shell
options.  The -- option assigns the positional parameters to the
arguments of {set}, even when some of them start with an option
prefix `-'.

Assigns shell positional parameters or changes the values of shell
options.  The -- option assigns the positional parameters to the
arguments of {set}, even when some of them start with an option
prefix `-'.

Assigns shell positional parameters or changes the values of shell
options.  The -- option assigns the positional parameters to the
arguments of {set}, even when some of them start with an option
prefix `-'.









reply via email to

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