I have a text file, the following shows a sample of file contents:

1234 A novel homeodomain-encoding gene is associated with a large CpG island interrupted by the <category="Modifier">myotonic dystrophy</category> unstable (CTG)n repeat. <category="SpecificDisease">Myotonic dystrophy</category> ( <category="SpecificDisease">DM</category> ) is associated with a ( CTG ) n trinucleotide repeat expansion in the 3-untranslated region of a protein kinase-encoding gene , DMPK , which maps to chromosome 19q13 . 3 . Characterisation of the expression of this gene in patient tissues has thus far generated conflicting data on alterations in the steady state levels of DMPK mRNA , and on the final DMPK protein levels in the presence of the expansion . The <category="Modifier">DM</category> region of chromosome 19 is gene rich , and it is possible that the repeat expansion may lead to dysfunction of a number of transcription units in the vicinity , perhaps as a consequence of chromatin disruption . We have searched for genes associated with a CpG island at the 3 end of DMPK . Sequencing of this region shows that the island extends over 3 . 5 kb and is interrupted by the ( CTG ) n repeat . Comparison of genomic sequences downstream ( centromeric ) of the repeat in human and mouse identified regions of significant homology . These correspond to exons of a gene predicted to encode a homeodomain protein . RT-PCR analysis shows that this gene , which we have called <category="Modifier">DM</category> locus-associated homeodomain protein ( DMAHP ) , is expressed in a number of human tissues , including skeletal muscle , heart and brain .

I need to extract what in between tags: for example,

<category="SpecificDisease">Myotonic dystrophy</category>

I need to extract "Myotonic dystrophy" and write to a new text file.

  • What do you mean by "tags"? – David Kennedy Nov 7 '13 at 5:09
  • @nlp if it is XML file then we can parse using API in languages like Python,Perl etc... so could you please add more details in your question.. if this data publicly available then just include the link.. – Rahul Patil Nov 7 '13 at 8:04
  • have you looked at ncbi.nlm.nih.gov/pubmed/8595416?report=abstract&format=text and other formats available there? – user15760 Nov 7 '13 at 12:31

You can do this using grep to find the text between tags and then sed to remove the tags themselves:

$ grep -oP '<category.+?>.*?</category>' file.txt | sed 's/<.*>\(.*\)<.*>/\1/'
myotonic dystrophy
Myotonic dystrophy


  • grep -oP : -P enables PCRE for grep and -o makes it print only the matched string.
  • '<category.+?>.*?</category>' : this tells grep to search for everything between opening and closing category tags.
  • sed 's/<.*>\(.*\)<.*>/\1/' : The output of the above grep is piped to sed which simply deletes the tags by replacing them with their contents (here \1 because parentheses were used to capture them).

This can be done through PCRE, I have tried so far.. but not understand yet fully..

here is example what I have tried and working :

grep -oP '(?:<category=[A-Za-z\"\s]*>)[A-Za-z\s]+(?:<\/category>)' input|\
awk -F">" '{split($2,a,"<"); print a[1]}'

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