1

I want to extract all the lines in a file containting these patterns: "#1:" and "tree length for".

Input:

#1: nexus0002_Pseudomonas_10M     

 branch          t       N       S   dN/dS      dN      dS  N*dN  S*dS

   6..5      0.000   390.0   195.0  0.0668  0.0000  0.0000   0.0   0.0
   6..7      0.013   390.0   195.0  0.0668  0.0008  0.0114   0.3   2.2
   7..1      0.000   390.0   195.0  0.0668  0.0000  0.0000   0.0   0.0
   7..4      0.000   390.0   195.0  0.0668  0.0000  0.0000   0.0   0.0
   6..8      0.000   390.0   195.0  0.0668  0.0000  0.0000   0.0   0.0
   8..2      0.013   390.0   195.0  0.0668  0.0008  0.0114   0.3   2.2
   8..3      0.013   390.0   195.0  0.0668  0.0008  0.0114   0.3   2.2

tree length for dN:       0.0023
tree length for dS:       0.0341

#1: nexus0003_Pseudomonas_10M     

 branch          t       N       S   dN/dS      dN      dS  N*dN  S*dS

   6..5      0.000   390.0   195.0  0.0668  0.0000  0.0000   0.0   0.0
   6..7      0.013   390.0   195.0  0.0668  0.0008  0.0114   0.3   2.2
   7..1      0.000   390.0   195.0  0.0668  0.0000  0.0000   0.0   0.0
   7..4      0.000   390.0   195.0  0.0668  0.0000  0.0000   0.0   0.0
   6..8      0.000   390.0   195.0  0.0668  0.0000  0.0000   0.0   0.0
   8..2      0.013   390.0   195.0  0.0668  0.0008  0.0114   0.3   2.2
   8..3      0.013   390.0   195.0  0.0668  0.0008  0.0114   0.3   2.2

tree length for dN:       0.0111
tree length for dS:       0.0444

Output:

#1: nexus0002_Pseudomonas_10M     

tree length for dN:       0.0023
tree length for dS:       0.0341

#1: nexus0003_Pseudomonas_10M

tree length for dN:       0.0111
tree length for dS:       0.0444

Is there any simple sed solution?

5

Use grep

grep -E "^#1:|tree length for" infile.txt 

or sed

sed -n '/^#1:/p;/^tree length for/p' infile.txt 
  • 1
    Worked just fine. – Manuel Aug 19 '17 at 17:48
0

From the data that you supplied, it looks as if you'd like to get all lines that starts with a non-blank character:

$ grep '^[^[:blank:]]' file.in
#1: nexus0002_Pseudomonas_10M   
tree length for dN:       0.0023
tree length for dS:       0.0341
#1: nexus0003_Pseudomonas_10M   
tree length for dN:       0.0111
tree length for dS:       0.0444

With sed:

$ sed -n '/^[^[:blank:]]/p' file.in

In both grep and sed, [[:blank:]] will match a single space or tab character. [^[:blank:]] with therefore match any single character that is not a space or a tab. Putting ^ in front of that anchors the pattern to the start of the line.

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.