Score:-2

Introducing blank lines between matched sections

jp flag

I have the following bash function that uses sed to extract sections occurring between ## Mode: org and ## # End of org, where # is the comment character. Finally I remove the comment character and any spaces.

This is my input

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

Here is the bash function with implementation in 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"
}

Originally, I had the two commands as

begorg='${charcl_bre}Mode: org$'
endorg='${charcl_bre}# End of org$'

which were not expanding the variable charcl_bre.

The output is

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 `-'.

What I would like to do is have a blank line between sections.

Artur Meinild avatar
vn flag
What's the question again? Please make sure your question is clear and definite.
pLumo avatar
in flag
Variables won't expand in single quotes.
pLumo avatar
in flag
Does this answer your question? [How do I use variables in a sed command?](https://askubuntu.com/questions/76808/how-do-i-use-variables-in-a-sed-command)
Score:0
ca flag

According to your latest edit, you just need to have a blank line between the different comment sections. You can do that by changing the last sed command from:

sed "/$mdr\|$edr/d"

to:

sed "/$mdr/d; s/$edr//"

This changes the sed command from deleting both $mdr and $edr lines to only deleting the $mdr line and replacing the $edr line with an empty string, effectively keeping the blank line you want.

The output is:

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 `-'.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.