1

I have a text file that looks like this:

HETATM11584
HETATM 2514
HETATM22222
HETATM 8585
HETATM 1213
HETATM62626
HETATM 6754

There are white spaces separating HETATM from a string of numbers. I want to delete theses white spaces and output a new file that looks like this:

HETATM11584
HETATM2514
HETATM22222
HETATM8585
HETATM1213
HETATM62626
HETATM6754

I tried to use sed

sed 's/.*HETATM [1-9]*.*/.*HETATM[1-9]*.*/' HETATM.txt > teste.txt

But the output looks like this:

HETATM11584
.*HETATM[1-9]*.*
HETATM22222
.*HETATM[1-9]*.*
.*HETATM[1-9]*.*
HETATM62626
.*HETATM[1-9]*.*

What is the proper sed synthax for this operation?

3 Answers 3

2

If the whole file is like your example, the easiest way would be removing all the spaces:

$ sed -r 's/ //g' file.txt
HETATM11584
HETATM2514
HETATM22222
HETATM8585
HETATM1213
HETATM62626
HETATM6754

If the file is composed by whatever followed by space(s) and a ending with a number, then this will do it:

$ sed -r 's/ +([0-9]+)$/\1/g' file.txt
HETATM11584
HETATM2514
HETATM22222
HETATM8585
HETATM1213
HETATM62626
HETATM6754
Sign up to request clarification or add additional context in comments.

Comments

1

You should not use a pattern as the substitution string. You can remove the space from your string using backreference:

sed 's/\(HETATM\) \([0-9]\)/\1\2/'  HETATM.txt > teste.txt 

HETATM and digits are captured with parenthesis. Captured strings are output(without space) with \1 and \2.

Comments

0

Do you have to use sed?

$ tr -d ' ' < HETATM.txt > teste.txt

might serve your purpose as well.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.