1

I have a file with the following structure:

[Term]
id: GO:0000001
name: mitochondrion inheritance
namespace: biological_process
def: "The distribution of mitochondria, including the mitochondrial genome, into daughter cells after mitosis or meiosis, mediated by interactions between mitochondria and the cytoskeleton." [GOC:mcc, PMID:10873824, PMID:11389764]
synonym: "mitochondrial inheritance" EXACT []
is_a: GO:0048308 ! organelle inheritance
is_a: GO:0048311 ! mitochondrion distribution

This structure is repeated several times, like a dictionary. Each definition starts with [Term] and it's separated from the following by an empty newline; not every definition ends with a line starting with is_a. I want to grep some terms and retrieve the whole definition, so from [Term] to the empty newline; i.e., the structure example shown before could be the result of grep -i "mitochondria" myfile. How do I do it? The number of lines of each definition is not fixed, and the match could be at any point of the definition.

I'm not sure that grep is the right tool to do it; the thing is that I want to match several words at once, so I started using grep -i -e "match" -e "someothermatch". Is there some regex that does the trick?

I work with Cygwin in a Windows environment, and apparently it supports PCRE.

  • can you provide more number of structure and the expected output – Kamaraj Oct 12 '16 at 14:28
  • As per your title, i understood that, you need something like this... awk '/start_pattern/,/end_pattern/' filename – Kamaraj Oct 12 '16 at 14:29
1

From your description (emphasis mine):

Each definition starts with [*Term*] and
it's separated from the following by an empty newline

Awk, when called with a null RS (RS='') splits a file on empty lines.
It is also capable of selecting based on strings:

$ var="someothermatch"
$ awk -v RS='' -v var="$var" '$0~var{print}' infile

Should work to get the whole paragraph that match.

From awk manual:

If RS is set to the null string, then records are separated by blank lines. When RS is set to the null string, the newline character always acts as a field separator, in addition to whatever value FS may have.

  • Thank you, RS is what I was looking for! Actually I wanted to find more th than one match, but following unix.stackexchange.com/questions/92815/… I modified your command in this way: awk -v RS='' '/match|anothermatch|thirdmatch/ {print}' with no actual need of a variable. – LinuxBlanket Oct 13 '16 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.