This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Extract and ADD text information

I have a text file with multiple segments like that:

       Sites with alignment gaps or missing data: 0
       Invariable (monomorphic) sites: 604
       Variable (polymorphic) sites: 17   (Total number of mutations: 0)
          Singleton variable sites: 0
          Parsimony informative sites: 0

          Singleton variable sites (two variants): 0
          Parsimony informative sites (two variants): 0
          Variable sites (three variants): 0
          Variable sites (four variants): 0


Fu and Li's D* test statistic: 0,00000
     Statistical significance: Not significant, P > 0.10
 Fu and Li's F* test statistic: 0,00000
     Statistical significance: Not significant, P > 0.10

And I want to extract 3 lines "Variable (polymorphic) sites\|Fu and Li's F* test statistic\|Statistical significance",But, I also want to add an information like DNA POLYMORPHISM AND FU AND LI'S TEST before the results of each analysis, generating a output like that:

DNA POLYMORPHISM
     Variable (polymorphic) sites: 17

FU AND LI'S TEST
    Fu and Li's F* test statistic: 0,00000
             Statistical significance: Not significant, P > 0.10

There is any way to do like it with grep?

grep text

There is any way to do like it with grep?

yes, show us what you have tried, and then we will help you

Oh sorry, I tried that:

$grep "Input Data File\|print "DNA POLYMORPHSM"\|Variable (polymorphic) sites\|print "FU AND LI'S TEST"\|Fu and Li's F test statistic\|Statistical significance" Arch.txt

I have no idea if a print function can be used inside a list of grep patterns, the help of grep doesn't show anything about text processing during the pattern extraction.

Thanks for your attention.

If you know some basic programming you can use python to read a file in and append every new line to a list.

you can then save only the elements that you want and using string slicing to further carve what you what out of each element. Python is really REALLY good with text stuff and this project would be a good learning experience and is probably not a heavy lift if you already know some bash scripting.

0 answers

No answers yet.

Log in to answer this question.