我正在使用以下 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"
}
这是输入
文件:测试.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.
答案1
您得到这个结果是因为您明确指示您的程序仅删除一个#
,所以这就是它正在做的事情:
sub(/^[[:space:]]*#[[:space:]]*/,"")
如果您将其更改为用于#+
删除一个或多个#
,它将按预期工作。接下来,您需要在各部分之间有一个空行,因此print ""
当您找到末尾时,您需要一个额外的空行。将您的功能更改为:
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; print "" }
flag { sub(/^[[:space:]]*#+[[:space:]]*/,""); print }' "$efile"
}
上面的结果是:
$ capture file
* 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}.
请注意,这不会像示例输出中那样保留此行的注释,但我假设这只是一个错误,因为该行似乎没有任何特别之处:
# Rmk: Using -- as value to an option
答案2
你可以尝试使用这个命令
awk '{sub(/^#*/,"",$0);print }' filename