Search code examples
bashpattern-matchingcut

Delete lines where 3rd character equals a number


I have a consistent file with numbers like

0123456
0234566
.
.
.
etc

With bash tools, command line preferable, how can I remove each line if the third digit equals 2 .

eg, with cut -c3 I can get the correct digit but I cannot combine it effectively with sed or something similar. I am not looking for a pattern, only the 3rd digit.

(I have done it in a script in python but I was wondering how its done through a one-line bash command). Thank you!

EDIT: Additionally, if I want to delete the lines where the third digit NOT equals to 2 (opposite question)


Solution

  • You can just do this with sed

    sed -i '/^..2/d' file
    

    If you want to do the opposite you can do:

    sed -i '/^..[^2]/d' file
    

    since you are dealing with a specific character.