我有以下 bash 函数,用于提取在和sed
之间出现的部分,其中是注释字符。最后,我删除了注释字符和所有空格。## Mode: org
## # End of org
#
这是我的意见
cat /home/flora/docs/recnotes.txt
## Mode: org
# Assigns shell positional parameters or changes the values of shell
# options. The -- option assigns the positional parameters to the
# arguments of {set}, even when some of them start with an option
# prefix `-'.
## # End of org
;; Mode: org
; Assigns shell positional parameters or changes the values of shell
; options. The -- option assigns the positional parameters to the
; arguments of {set}, even when some of them start with an option
; prefix `-'.
;; # End of org
@c Mode: org
@c Assigns shell positional parameters or changes the values of shell
@c options. The -- option assigns the positional parameters to the
@c arguments of {set}, even when some of them start with an option
@c prefix `-'.
@c # End of org
这是 中的实现的 bash 函数sed
。
capture ()
{
local efile="$1"
local charcl begorg endorg
charcl_ere='^[[:space:]]*([#;!]+|@c|\/\/)[[:space:]]*'
charcl_bre='^[[:space:]]*\([#;!]\+\|@c\|\/\/\)[[:space:]]*'
begorg="${charcl_bre}"'Mode: org$'
endorg="${charcl_bre}"'# End of org$'
mdr='^Mode: org$' ; edr='^# End of org$'
sed -n "/$begorg/,/$endorg/ s/$charcl_bre//p" "$efile" |
sed "/$mdr\|$edr/d"
}
最初,我有两个命令
begorg='${charcl_bre}Mode: org$'
endorg='${charcl_bre}# End of org$'
没有扩大变量charcl_bre
。
输出为
Assigns shell positional parameters or changes the values of shell
options. The -- option assigns the positional parameters to the
arguments of {set}, even when some of them start with an option
prefix `-'.
Assigns shell positional parameters or changes the values of shell
options. The -- option assigns the positional parameters to the
arguments of {set}, even when some of them start with an option
prefix `-'.
Assigns shell positional parameters or changes the values of shell
options. The -- option assigns the positional parameters to the
arguments of {set}, even when some of them start with an option
prefix `-'.
我想要做的是在各部分之间留一行空白。
答案1
根据您的最新编辑,您只需要在不同的注释部分之间留一个空行即可。您可以通过将最后一个sed
命令从以下内容更改为以下内容:
sed "/$mdr\|$edr/d"
到:
sed "/$mdr/d; s/$edr//"
这会将sed
命令从删除$mdr
和$edr
行更改为仅删除$mdr
行并$edr
用空字符串替换该行,从而有效地保留所需的空白行。
输出为:
Assigns shell positional parameters or changes the values of shell
options. The -- option assigns the positional parameters to the
arguments of {set}, even when some of them start with an option
prefix `-'.
Assigns shell positional parameters or changes the values of shell
options. The -- option assigns the positional parameters to the
arguments of {set}, even when some of them start with an option
prefix `-'.
Assigns shell positional parameters or changes the values of shell
options. The -- option assigns the positional parameters to the
arguments of {set}, even when some of them start with an option
prefix `-'.