Search code examples
linuxparsingawkgrepfilefilter

How to get lines that don't contain certain patterns


I have a file that contain many lines :

ABRD0455252003666
JLKS8568875002886
KLJD2557852003625
.
.
.
AION9656532007525
BJRE8242248007866

I want to extract the lines that start with (ABRD or AION) and in column 12 to 14 the numbers (003 or 007).

The output should be

KLJD2557852003625
BJRE8242248007866

I have tried this and it works but it s too long command and I want to optimise it for performance concerns:

 egrep  -a --text '^.{12}(?:003|007)' file.txt > result.txt |touch results.txt && chmod 777 results.txt |egrep -v -a --text  "ABRD|AION" result.txt > result2.text

Solution

  • The -a option is a non-standard extension for dealing with binary files, it is not needed for text files.

     grep -E '^.{11}(003|007)' file.txt | grep -Ev '^(ABRD|AION)'
    

    The first stage matches any line with either 003 or 007 in the twelfth through fourteenth column.

    The second stage filters out removes any line starting with either ABRD or AION.