Search code examples
textsedtext-processingline-numbers

How to keep certain line numbers and delete the rest


Using sed I know how to delete certain line numbers like

sed -i.bak -e '6d;8d;15d;' file

How can I achieve the reverse of it. I want to keep those lines and delete the rest. I want to do this in place, as I have huge files and I'd use this as part of a larger script. I'm still keeping a backup as I don't want to mess up my original dataset.


Solution

  • How about stopping the print for sed and mention wherever you want to print the lines then.

    sed -i.bak -n '6p;8p;15p;' Input_file
    

    From man sed:

       -n, --quiet, --silent
    
              suppress automatic printing of pattern space
    
       p      Print the current pattern space.