Removendo `#` inicial com awk

Removendo `#` inicial com awk

Estou usando a seguinte função bash para capturar linhas entre ## mode: rece ## # End of rec, mas não consigo remover o início #do resultado.

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

Esta é a entrada

arquivo: teste.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

Mas estou obtendo esse resultado

# * 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.

A saída esperada é

* 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.

Responder1

Você está obtendo esse resultado porque está instruindo explicitamente seu programa a remover apenas one #, e é isso que ele está fazendo:

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

Se você alterar isso para #+remover um ou mais #, funcionará conforme o esperado. Em seguida, você deseja uma linha vazia entre as seções, então precisará de uma linha extra print ""quando encontrar o final. Mude sua função para esta:

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

O acima produz:

$ 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}.

Observe que isso não mantém esta linha comentada como você fez em seu exemplo de saída, mas presumo que foi apenas um erro, já que não parece haver nada de especial nessa linha:

# Rmk: Using -- as value to an option

Responder2

Você pode tentar com este comando

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

informação relacionada