다음 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
이 결과는 프로그램에 하나만 삭제하도록 명시적으로 지시했기 때문에 나타나는 결과입니다 #
.
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