【问题标题】:Introducing blank lines separating matched sections引入分隔匹配部分的空白行
【发布时间】:2021-12-19 00:12:10
【问题描述】:

我有这个 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

【问题讨论】:

  • 请将该示例输入的所需输出(无描述、无图像、无链接)添加到您的问题(无评论)。

标签: bash sed


【解决方案1】:

我想出了一个简单的使用方法

   sed -n "/$begorg/,/$endorg/ s/$charcl_bre//p" "$efile" |
    sed "/$mdr/d" | sed "s/$edr//"

【讨论】:

    【解决方案2】:

    我假设 lines between 你不是指包含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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      • 2013-10-30
      相关资源
      最近更新 更多