我有這個 bash 函式,可以在Mode: org和之間列印行# End of org。我希望匹配的部分用空行分隔。
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"
}
這是輸入
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
uj5u.com熱心網友回復:
我假設在這之間的行中,您并不是要包括帶有Mode: org和的行# End of org。你沒有具體說明。嘗試:
perl -nlE'$p=/Mode: org/?1:/# End of org/?0*say$":$p&&$p say' recnotes.txt
如果避免最后出現空分隔線很重要,請嘗試:
perl -nlE'$p=/Mode: org/?1:/# End of org/?0*do{eof||say("")}:$p&&$p say' r.txt
uj5u.com熱心網友回復:
我想出了一個簡單的方法使用
sed -n "/$begorg/,/$endorg/ s/$charcl_bre//p" "$efile" |
sed "/$mdr/d" | sed "s/$edr//"
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350260.html
