help-gawk
[Top][All Lists]
Advanced

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

Capturing comment character from matching pattern


From: fatiparty
Subject: Capturing comment character from matching pattern
Date: Wed, 3 Nov 2021 17:12:48 +0100 (CET)

I have the following bash function that captures lines between the two patterns

^[[:space:]]*(#|;|!)+[[:space:]]*Mode: org$

and

^[[:space:]]*(#|;|!)+[[:space:]]*End of org$

Would like to capture the comment characterfrom matching the beginning pattern 
^[[:space:]]*(#|;|!)+[[:space:]]*Mode: org$ whether #, ;, or !.  But also for 
// and @c, so that I can remove it fromthe sections selected.

capture ()
{
local efile="$1"

local begrec endrec charcl

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

begrec="${charcl}"'Mode: org$'
endrec="${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"
}

Here is what a file could look like

 ## 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
And this is what I would like to end up with, the comment lines removed

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



reply via email to

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