Josh
Josh

Reputation: 21

Remove Lines in Multiple Text Files that Begin with a Certain Word

I have hundreds of text files in one directory. For all files, I want to delete all the lines that begin with HETATM. I would need a csh or bash code.

I would think you would use grep, but I'm not sure.

Upvotes: 0

Views: 191

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207738

Use sed like this:

sed -i -e '/^HETATM/d' *.txt

to process all files in place.

-i means "in place".

-e means to execute the command that follows.

/^HETATM/ means "find lines starting with HETATM", and the following d means "delete".

Make a backup first!

If you really want to do it with grep, you could do this:

#!/bin/bash

for f in *.txt
do
   grep -v "^HETATM" "%f" > $$.tmp && mv $$.tmp "$f"
done

It makes a temporary file of the output from grep (in file $$.tmp) and only overwrites your original file if the command executes successfully.

Upvotes: 2

julienc
julienc

Reputation: 20345

Using the -v option of grep to get all the lines that do not match:

grep -v '^HETATM' input.txt > output.txt

Upvotes: 0

Related Questions