I have written a bash function to print sections of text enclosed between lines matching ## mode: org
and ## # End of org
in a file, with an empty line between sections. Before the ##
, there can be any number of spaces.
Here is an example of a file to extract information from.
file: test.sh
## mode: org
## * Using case statement
## # End of org
case $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
The desired output would be
Code:
* Using case statement
** Silent Error Reporting Mode (SERM) in getopts
*** Detects warnings without printing built-in messages.
*** Enabled by colon {:} as first character in shortopts.
Here is the function I am using
capture-org ()
{
local efile="$1"
local begsec="## mode: org"
local endsec="## # End of org"
sed -n "/^[[:space:]]*${begsec}$/,/^[[:space:]]*${endsec}$/s/ *//p'" "$efile" |
sed 's/^'"${begsec}"'$/\n'"${begsec}"'/' |
sed '/^'"${begsec}"'$/d' | sed '/^'"${endsec}"'$/d' | cut -c 3-
}
I would like to simplify the function, using variables to construct patterns. But need some assistance to compile commands together such that I do not have to call sed
so many times.
Perhaps using awk
would be a better strategy.
capture-org ()
{
local efile="$1"
local begsec='^[[:space:]]*## mode: org$'
local endsec='^[[:space:]]*## # End of org$'
sed -n "/${begsec}/,/${endsec}/s/ *//p" "$efile" |
sed 's/^## # End of org$/## # End of org\n/' |
sed '/^## mode: org$/d' | sed '/^## # End of org$/d' | cut -c 3-
}