使用 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"
}

這是輸入

檔案:測試.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

相關內容