Delete lines between lines in a file

I have files that look like this:

ATOM 2517 O   VAL 160 8.337  12.679  -2.487
ATOM 2518 OXT VAL 160 7.646  12.461  -0.386
TER 
ATOM 2519 N   VAL 161 -14.431  5.789 -25.371
ATOM 2520 H1  VAL 161 -15.336  5.698 -25.811
ATOM 2521 H2  VAL 161 -13.416 10.529  17.708
ATOM 2522 H3  VAL 161 -14.363  9.436  18.498
ATOM 2523 CA  VAL 161   4.400  9.233  16.454
ATOM 2524 HA  VAL 161   3.390  9.170  16.047

I need to delete "TER", the line before "TER" and 3 lines after the line immediately after TER and make the file the same:

ATOM 2517 O   VAL 160   8.337 12.679  -2.487
ATOM 2519 N   VAL 161 -14.431  5.789 -25.371
ATOM 2523 CA  VAL 161   4.400  9.233  16.454
ATOM 2524 HA  VAL 161   3.390  9.170  16.047
+2
source share
4 answers

Just delete the line starting with TER.

   sed -i.bak '/^\s*TER\s*$/d' transrotate/myfiles

If you just want to delete an empty line, try the following:

sed -i.bak '/^\s*$/d' transrotate/myfiles
+2
source
sed '/^TER/d' yourFile  

will complete the task

kent$  echo "ATOM 2517 O   VAL 160 8.337 12.679 -2.487
dquote> ATOM 2518 OXT VAL 160 7.646 12.461 -0.386
dquote> TER 
dquote> ATOM 2519 N  VAL 161 -14.431  5.789 -25.371
dquote> ATOM 2520 H1 VAL 161 -15.336  5.698 -25.811
dquote> ATOM 2521 H2 VAL 161 -13.416 10.529  17.708
dquote> ATOM 2522 H3 VAL 161 -14.363  9.436  18.498" |sed '/^TER/d'

ATOM 2517 O   VAL 160 8.337 12.679 -2.487
ATOM 2518 OXT VAL 160 7.646 12.461 -0.386
ATOM 2519 N  VAL 161 -14.431  5.789 -25.371
ATOM 2520 H1 VAL 161 -15.336  5.698 -25.811
ATOM 2521 H2 VAL 161 -13.416 10.529  17.708
ATOM 2522 H3 VAL 161 -14.363  9.436  18.498

updated based on new requirement

see awk line below:

kent$  cat t.txt
ATOM 2517 O   VAL 160 8.337 12.679 -2.487
ATOM 2518 OXT VAL 160 7.646 12.461 -0.386
TER 
ATOM 2519 N  VAL 161 -14.431  5.789 -25.371
ATOM 2520 H1 VAL 161 -15.336  5.698 -25.811
ATOM 2521 H2 VAL 161 -13.416 10.529  17.708
ATOM 2522 H3 VAL 161 -14.363  9.436  18.498

kent$  awk 'NR==FNR{if ($0~/^TER/)a[NR]=1;}NR>FNR{f=0;for(x in a){if(FNR>=x-1 && FNR<=x+3){f=1;break;}}if(!f){print $0;}f=0}' t.txt t.txt
ATOM 2517 O   VAL 160 8.337 12.679 -2.487
ATOM 2522 H3 VAL 161 -14.363  9.436  18.498

updated again

Hope this is the latest update:

awk line:

awk 'NR==FNR{if ($0~/^TER/)a[NR]=1;}NR>FNR{f=0;for(x in a){if(FNR==x-1 || FNR==x || (FNR>x+1 && FNR<=x+4)){f=1;break;}}if(!f){print $0;}f=0}' yourFile yourFile

Test:

kent$  cat t.txt
ATOM 2517 O   VAL 160 8.337  12.679  -2.487
ATOM 2518 OXT VAL 160 7.646  12.461  -0.386
TER 
ATOM 2519 N   VAL 161 -14.431  5.789 -25.371
ATOM 2520 H1  VAL 161 -15.336  5.698 -25.811
ATOM 2521 H2  VAL 161 -13.416 10.529  17.708
ATOM 2522 H3  VAL 161 -14.363  9.436  18.498
ATOM 2523 CA  VAL 161   4.400  9.233  16.454
ATOM 2524 HA  VAL 161   3.390  9.170  16.047

kent$  awk 'NR==FNR{if ($0~/^TER/)a[NR]=1;}NR>FNR{f=0;for(x in a){if(FNR==x-1 || FNR==x || (FNR>x+1 && FNR<=x+4)){f=1;break;}}if(!f){print $0;}f=0}' t.txt t.txt
ATOM 2517 O   VAL 160 8.337  12.679  -2.487
ATOM 2519 N   VAL 161 -14.431  5.789 -25.371
ATOM 2523 CA  VAL 161   4.400  9.233  16.454
ATOM 2524 HA  VAL 161   3.390  9.170  16.047
+1
source

, :

perl -e 'undef $/; ($a=<>)=~s!(.*\n){1}TER\n(.*\n)(.*\n){3}!$2!; print $a;' INFILE > OUTFILE

, , , .


PS If the input is more than RAM, then you need a simple state machine. Read the file in a line in a loop. pushto the intermediate buffer. If there are more than 4 lines in the buffer, shiftprint the head. If the second line is in the buffer TERand the buffer contains 4 lines, clear the buffer. Repeat until there are lines.

0
source

ed great for this kind of thing:

$ ed -s file.txt <<EOF
> /^TER/d
> -1d
> +1d
> d
> d
> ,p
> EOF

pin the next line displays the result; change it to wfor in-place editing and save it in the same file.

0
source

All Articles