awk で先頭の `# ` を削除する

awk で先頭の `# ` を削除する

## mode: recとの間の行をキャプチャするために次の bash 関数を使用していますが、結果の## # 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

答え2

このコマンドを試すことができます

awk '{sub(/^#*/,"",$0);print }' filename

関連情報