Get the n-th range by pattern
My input is like this:
start
content A
end
garbage
start
content B
end
I want to extract the second (or first, or third ...) start .. end
block. With
sed -ne '/start/,/end开发者_运维问答/p'
I can filter out the garbage, but how do I get just "start content B end"?
But anyway, if you want sed - you get sed:)
/^start$/{
x
s/^/a/
/^aaa$/{
x
:loop
p
/^end$/q
n
bloop
}
x
}
The number of a's in the middle match equals to which segment you want to get. You could also have it in regexp repetion like Dennis noted. That approach allows for specifying direct number to the script.
Note: the script should be run with -n
sed
option.
Get all range
$ awk 'BEGIN{RS="end";FS="start"}{ print $NF}' file
content A
content B
Get 2nd range
$ awk 'BEGIN{RS="end";FS="start"}{c++; if (c==2) print $NF}' file
content B
Ruby(1.9+), get first range
$ ruby -0777 -ne 'puts $_.scan(/start(.*?)end/m)[0]' file
content A
精彩评论