I have this file:
sometext1{
string1
}
sometext2{
string2
string3
}
sometext3{
string4
string5
string6
}
I want to search this file for a specific string and print everything before this string up to the opening {
and everything after this string up to the closing }
. I tried to achieve this with sed but if I try to print everything in the range /{/,/string2/
for example sed prints this:
sometext1{
string1
}
sometext2{
string2
sometext3{
string4
string5
string6
}
If I search for the string "string2" I need the output to be:
sometext2{
string2
string3
}
Thanks.
Best Answer
Here are two commands. If you want a command that trims up to the last
.*{$
line in a sequence (as @don_crissti does withed
) you can do:...which works by appending every line to
H
old space following a\n
ewline character, overwritingh
old space for every line that matches{$
, and swapping ingh
old and pattern spaces for every line that matches^}
- and thereby flushing its buffer.It only prints lines which match a
{
then a\n
ewline and thenPATTERN
at some point - and that only ever happens immediately following a buffer swap.It elides any lines in a series of
{$
matches to the last in the sequence, but you can get all of those inclusive like:What it does is swap pattern and
h
old spaces for every...{$.*^}.*
sequence, appends all lines within the sequence toH
old space following a\n
ewline character, andD
eletes up to the first occurring\n
ewline character in pattern space for every line cycle before starting again with what remains.Of course, the only time it ever gets
\n
ewline in pattern space is when an input line matches^}
- the end of your range - and so when it reruns the script on any other occasion it just pulls in the next input line per usual.When
PATTERN
is found in the same pattern space as a\n
ewline, though, it prints the lot before overwriting it with^}
again (so it can end the range and flush the buffer).Given this input file (thanks don):
The first prints:
...and the second...