Reputation: 1
I have a large file stored in a bash variable as a string, and i need to extract all the lines between two same words. But I also need to check if between these lines, I have at least one occurence of magicstring
. Here's an example :
abc
xxxxxxxx
yyyyyyyy
abc
magicstring
kkkkkkkk
magicstring
abc
mmmmmmmm
nnnnnnnn
What I am looking for is the following :
magicstring
kkkkkkkk
magicstring
The two same words are always abc
, but using sed, awk or even parameter expansion, how could I precise that I need the word magicstring
contained at least once in those lines ?
I am assuming that I don't know the section where magicstring
is contained, since it's a large file.
EDIT : Also, magicstring
appears only once or multiple times in only one section.
Using sed for example, I would get all the lines like this : sed '/abc/,/abc/{//!d}' <<<($myFileInVar)
but I don't know to precise the need of magicstring
.
Upvotes: 0
Views: 65
Reputation: 203532
Using any awk in any shell on every UNIX box:
$ awk -v RS= -v ORS='\n\n' '/magicstring/' file
abc
magicstring
kkkkkkkk
magicstring
$ awk -v RS= -v ORS='\n\n' '/magicstring/{sub(/^[^\n]+\n/,""); print}' file
magicstring
kkkkkkkk
magicstring
Upvotes: 1
Reputation: 67507
gawk
to the rescue!
$ awk -v RS='(^|\n)abc\n' '/magicstring/' file
magicstring
kkkkkkkk
magicstring
define the delimiter word as the record separator, print the record if it matches the magic string.
Upvotes: 1