Re: Delete only the nth block of text, multi-line

"[email protected] [sed-users]" <[email protected]>
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
That's very clever, the way you "increment the block counter" by adding dots to the hold pattern. Below is a variation on your solution, storing a number with expr, instead of a using series of dots. It's no better, just a little different, and helps me understand your solution.
 

 Daniel
 

 --------------------------------------------
 

 $ cat in
foo
[
  aaa 1
  bbb 1
  ccc 1
]
[
  ccc 2
  bbb 2
  aaa 2
]
[
  bbb 3
  ccc 3
  aaa 3
]
[
  bbb 4
  aaa 4
  ccc 4
]
bar
 

 ---------------------------------------
 

 $ cat sed3.txt
## Objective: To delete the 3rd-block from input.
## Where a block begins with [ on a line by itself
## and the block ends with a ] on a separate line.
 

 # skip the non-block lines right away.
/^\[$/,/^\]$/!b end
 

 # increment the block counter
x; s/.*/expr & + 1/e; x
 

 # accumulate the block
:loop $b end; N; /\n\]$/ ! b loop
 

 # we now have the whole block in the pattern space.
# check whether this is the 3rd block?
x
# yes: print remainder after the block
/^3$/ { :remaining n; p; b remaining }
# no: just display block as is
x
 

 :end p

 -----------------------------------------------------
 

 $ sed -n -f sed3.txt in
foo
[
  aaa 1
  bbb 1
  ccc 1
]
[
  ccc 2
  bbb 2
  aaa 2
]
[
  bbb 4
  aaa 4
  ccc 4
]
bar

 



[Non-text portions of this message have been removed]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.