Search code examples
bashshellterminalgrepcsh

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.


Solution

  • 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.