Vahid Mirjalili
Vahid Mirjalili

Reputation: 6501

Delete lines matchin a string and followed by a line with another string

So, I have 30000 files that contains an some extra lines that should be deleted.

ATOM   2208  O   LYS   168      12.304 -23.216   0.596  1.00  0.00
ATOM   2209  OXT LYS   168      13.852 -21.707   0.692  1.00  5.19
TER
ATOM   2210  N   ASP   178       2.448 -25.410  -0.746  1.00  5.40
ATOM   2211  HT1 ASP   178       2.706 -25.926   0.119  1.00  0.00
..
TER
END

So, I want to remove only the line that has TER but followed by a new line starts with ATOM. Therefore I don't want to remove if TER is followed by anything else.

Is there any way to do this with sed:

I can use this command but it will delete all lines with TER:

sed -i '/^TER/d' myfile

Upvotes: 0

Views: 153

Answers (2)

potong
potong

Reputation: 58371

This might work for you (GNU sed):

sed '$!N;/^TER.*\nATOM/!P;D' file

Only print lines that don't start with TER followed by ATOM.

Upvotes: 1

coPro
coPro

Reputation: 126

I learned a whole lot about sed while trying to figure out this answer so that was cool.

sed -i '/^TER$/{N; s/^TER\nATOM/ATOM/}' test.txt

Basically it finds a line containing only "TER". It then appends the next line onto this line. If this new string starts with the "TER" followed by a new line and "ATOM" it replaces this with just "ATOM".

I would suggest not doing an inline edit unless you have already made a backup just in case.

Hope this works for you.

Upvotes: 3

Related Questions