1

I have file with lines that start with two possible keywords, ATOM and HETATM. In some of those lines, there is a second keyword HOH, appearing after some other characters. In other lines, there are other 3-character keywords XXX like SOL instead of HOH:

ATOM  1231  O  XXX ...
HETATM1232  O  SOL ...
HETATM1233  O  HOH ...

What I want to do is to change HETATMs to ATOM only if HOH also appears on the line.

I tried the following:

sed -E '/\(HETATM\)\(.*HOH\)/s//\1ATOM/' file

which gives the illegal byte sequence error. What am I doing wrong?

sodiumnitrate
  • 2,899
  • 6
  • 30
  • 49

1 Answers1

2

Try this:

sed '/HOH/s/HETATM/ATOM/' file

Output:

ATOM  1231  O  XXX ...
HETATM1232  O  SOL ...
ATOM1233  O  HOH ...

Only in lines which contain HOH search HETATM and replace by ATOM.

Cyrus
  • 84,225
  • 14
  • 89
  • 153