awkを使って先行 `#`を削除してください。

awkを使って先行 `#`を削除してください。

次の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.

ベストアンサー1

これは、プログラムに1つだけを削除するように明示的に指示したために表示される結果です#

sub(/^[[:space:]]*#[[:space:]]*/,"")

#+1つ以上の削除に使用するように変更すると、期待#どおりに機能します。次に、セクション間に空白行が必要なので、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

おすすめ記事