2

I have a file similar to this; where I want to remove every fourth line containing POPEL term and the lines not containing POPEL term should be unchanged. Is there any shell commands which I can use to achieve the same?

ATOM  35168 C310 POPEL  21     -55.342  50.172 277.202  1.00  0.00      L29  C  
ATOM  35169 C311 POPEL  21     -54.573  48.982 277.883  1.00  0.00      L29  C  
ATOM  35170 C312 POPEL  21     -55.514  47.942 278.341  1.00  0.00      L29  C  
ATOM  35171 C313 POPEL  21     -54.925  46.686 279.024  1.00  0.00      L29  C  
ATOM  35172 C314 POPEL  21     -55.997  45.711 279.545  1.00  0.00      L29  C  
ATOM  35173 C315 POPEL  21     -55.353  44.401 280.101  1.00  0.00      L29  C  
ATOM  35174 C316 POPEL  21     -56.243  43.664 281.090  1.00  0.00      L29  C  
ATOM  53696  CA  ARG A  17     -20.523 -12.601 204.054  1.00111.27           C  
ATOM  53707  CA  LEU A  18     -19.719  -8.859 204.177  1.00108.86           C  
ATOM  53715  CA  PRO A  19     -18.861  -6.764 207.199  1.00105.57           C  
ATOM  53722  CA  HIS A  20     -21.685  -4.738 208.657  1.00 92.98           C  
ATOM  53732  CA  PRO A  21     -23.472  -1.861 207.030  1.00 79.64           C  
ATOM  53739  CA  THR A  22     -22.732   0.531 209.838  1.00 69.21           C  
ATOM  53746  CA  LEU A  23     -19.126  -0.513 209.819  1.00 82.65           C  
ATOM  53754  CA  LEU A  24     -18.800   0.542 206.174  1.00 86.67           C  
ATOM  53762  CA  PHE A  25     -19.447   4.011 207.542  1.00 87.41           C  
ATOM  53773  CA  VAL A  26     -16.695   3.967 210.128  1.00 78.42           C  
3
  • That's ambiguous. Do you mean (line number % 4 == 0 && contains POPEL) or (count(lines with POPEL so far) % 4 == 0)?
    – muru
    Apr 20, 2018 at 3:14
  • (line number % 4 == 0 && contains POPEL)
    – shome
    Apr 20, 2018 at 3:15
  • (1) Unambiguous explanations are good. Please also show the output you want. (2) Please do not respond in comments; edit your question to make it clearer and more complete. Apr 21, 2018 at 4:10

2 Answers 2

2

Using GNU sed:

sed -i.bak '0~4{/POPEL/d}' infile

delete lines containing POPEL in every 4th lines only.

from man sed

first~step

Match every step'th line starting with line first. For example, ``sed -n 1~2p'' will print all the odd-numbered lines in the input stream, and the address 2~5 will match every fifth line, starting with the second. first can be zero; in this case, sed operates as if it were equal to step. (This is an extension.)

1

With awk:

awk '(NR % 4) || !/POPEL/' input-file > output-file

The default action in awk for a true condition is to print the line, so this prints the input when:

  • NR % 4 is not zero, so everything except every fourth line
  • and for every fourth line, if the line doesn't contain POPEL.
1
  • 1
    Or awk '!/POPEL/ || ++n % 4' if it's every 4th [line containing POPEL] Apr 20, 2018 at 6:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .