Description
In a datafile, if a line begins with '.subckt' and line(s) following it begins with a plus, then comment these lines out by adding an asterisk('*') to each of them.
Raw Input Desired Output
line 1
line 2
.subckt A B C D
line 3
.subckt E F
+ G H
+ I J K
line 4
.subckt L M
.subckt N O
+ P Q
line 5
.subckt R
+ S T
line 1
line 2
.subckt A B C D
line 3
*.subckt E F
*+ G H
*+ I J K
line 4
.subckt L M
*.subckt N O
*+ P Q
line 5
*.subckt R
*+ S T
Script and Comments
Script1
[ 1] /^\.subckt/!b
[ 2] :loop
[ 3] $!N
[ 4] /\n+/!{
[ 5] P
[ 6] D
[ 7] }
[ 8] s/^\.subckt/*&/
[ 9] P
[10] s/^.*\n/*/
[11] b loop
Comments
  1. Whenever a line beginning with '.subckt' is met, sed enters the loop consisting of Step [2] thru [11]:
    StepAction
    3 join the next line to the end of Pattern Space if the current line is NOT the last one.
    4-5 if the joined line does NOT begins with a '+', the sequence 'P then D' will print the first line of Pattern Space, then delete it and starts a new cycle.
    8 Add an '*' to the head of a line if it begins with '.subckt'.
    9 Print the first line of Pattern Space.
    10 Make the second line of Pattern Space be the first one and prepend an '*' to it.
    10 Branch to Step [3].
More...
We can make the script neat if in the datafile:
  • No lines begin with '.' except those ones begin with '.subckt'.
  • No lines begin with '*'.
Script and Comments
Script1
[ 1] /^[.*]/!b
[ 2] $!N
[ 3] s/^[^*].*\n+/*&/
[ 4] s/\n+/\n*+/
[ 5] P
[ 6] D