我正在使用以下 bash 函数来捕获 和 之间的行## mode: rec
,## # End of rec
但无法删除#
结果的前导。
capture ()
{
local efile="$1"
local begorg endorg
begorg='^[[:space:]]*## mode: org$'
endorg='^[[:space:]]*## # End of org$'
awk -v bego="$begorg" -v endo="$endorg" \
'$0 ~ bego { flag=1; next }
$0 ~ endo { flag=0; }
flag { sub(/^[[:space:]]*#[[:space:]]*/,""); print }' "$efile"
}
这是输入
文件: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
## mode: org
## HDG: Handling function argument parsing with getopts
## Rmk: No call to {shift} is required with getopts
## Rmk: No hyphen {-} required when searching option names
## + Case patterns do not start with option hyphen {-} because
## + getopts strips off the hyphen and makes the value of {arg}
## + to be just the option letter.
## Rmk: Separating options from non-options with --
## + {getopts} stops processing options when the argument is not
## + defined as an option in {shortopts}; or if the argument is
## + "--", which explicitly terminates the list of options.
## Rmk: Using -- as value to an option
## + An option value can be -- without it being considered a
## + separator between options and non-options.
## + Example { edvart-getopts -g "--" }.
## Rmk: Explicitly testing for {--}
## + There is no need to test for {--} when using {getopts}.
## # End of org
但我得到了这个结果
# * 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.
# HDG: Handling function argument parsing with getopts
# Rmk: No call to {shift} is required with getopts
# Rmk: No hyphen {-} required when searching option names
# + Case patterns do not start with option hyphen {-} because
# + getopts strips off the hyphen and makes the value of {arg}
# + to be just the option letter.
# Rmk: Separating options from non-options with --
# + {getopts} stops processing options when the argument is not
# + defined as an option in {shortopts}; or if the argument is
# + "--", which explicitly terminates the list of options.
# Rmk: Using -- as value to an option
# + An option value can be -- without it being considered a
# + separator between options and non-options.
# + Example { edvart-getopts -g "--" }.
# Rmk: Explicitly testing for {--}
# + There is no need to test for {--} when using {getopts}.
# HDG: Silent Error Reporting Mode (SERM) in getopts
# Rmk: Detects warnings without printing built-in messages.
# Rmk: Enabled by colon {:} as first character in shortopts.
预期的输出是
* 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.
HDG: Handling function argument parsing with getopts
Rmk: No call to {shift} is required with getopts
Rmk: No hyphen {-} required when searching option names
+ Case patterns do not start with option hyphen {-} because
+ getopts strips off the hyphen and makes the value of {arg}
+ to be just the option letter.
Rmk: Separating options from non-options with --
+ {getopts} stops processing options when the argument is not
+ defined as an option in {shortopts}; or if the argument is
+ "--", which explicitly terminates the list of options.
# Rmk: Using -- as value to an option
+ An option value can be -- without it being considered a
+ separator between options and non-options.
+ Example { edvart-getopts -g "--" }.
Rmk: Explicitly testing for {--}
+ There is no need to test for {--} when using {getopts}.
HDG: Silent Error Reporting Mode (SERM) in getopts
Rmk: Detects warnings without printing built-in messages.
Rmk: Enabled by colon {:} as first character in shortopts.
你得到这个结果是因为你明确指示你的程序只删除一个
#
,所以这就是它正在做的事情:如果您将其更改为用于
#+
删除一个或多个#
,它将按预期工作。接下来,您希望各部分之间有一个空行,因此print ""
当您找到结尾时需要一个额外的行。将您的功能更改为:以上产生:
请注意,这不会像您在示例输出中那样保留此行的注释,但我假设这只是一个错误,因为该行似乎没有什么特别之处:
你可以试试这个命令