Using comment symbol for matching comment sections
by Faki from LinuxQuestions.org on (#5RF1E)
I have the following bash function to extract sections between `## Mode: org` and `## # End of org`, using `#` as the comment character.
I would like to enable other comment characters instead of handling only `#`. Using a Character Class `[#;c!C]` could be a good plan.
I also want to allow a user-defined literal string for the texinfo comment identifier `@c`.
Code:
capture ()
{
local efile="$1"
begorg='^[[:space:]]*## Mode: org$'
endorg='^[[:space:]]*## # End of org$'
awk -v bego="$begorg" -v endo="$endorg" \
'$0 ~ bego { found=1; next }
$0 ~ endo { found=0; }
found { sub(/^[[:space:]]*#+[[:space:]]*/,""); print }' "$efile"
}
I would like to enable other comment characters instead of handling only `#`. Using a Character Class `[#;c!C]` could be a good plan.
I also want to allow a user-defined literal string for the texinfo comment identifier `@c`.
Code:
capture ()
{
local efile="$1"
begorg='^[[:space:]]*## Mode: org$'
endorg='^[[:space:]]*## # End of org$'
awk -v bego="$begorg" -v endo="$endorg" \
'$0 ~ bego { found=1; next }
$0 ~ endo { found=0; }
found { sub(/^[[:space:]]*#+[[:space:]]*/,""); print }' "$efile"
}