Delete all lines which don’t have n characters before delimiter

grepsedtext formattingtext processing

I have a very long text file (from here) which should contain 6 hexadecimal characters then a 'break' (which appears as one character and doesn't seem to show up properly in the code markdown below) followed by a few words:

00107B  Cisco Systems, Inc
00906D  Cisco Systems, Inc
0090BF  Cisco Systems, Inc
5080    Cisco Systems, Inc
0E+00   ASUSTek COMPUTER INC.
000C6E  ASUSTek COMPUTER INC.
001BFC  ASUSTek COMPUTER INC.
001E8C  ASUSTek COMPUTER INC.
0015F2  ASUSTek COMPUTER INC.
2354    ASUSTek COMPUTER INC.
001FC6  ASUSTek COMPUTER INC.
60182E  ShenZhen Protruly Electronic Ltd co.
F4CFE2  Cisco Systems, Inc
501CBF  Cisco Systems, Inc

I've done some looking around and can't see something which would work in this situation. My question is, how can I use grep/sed/awk/perl to delete all lines of this text file which do not start with exactly 6 hexadecimal characters and then a 'break'?

P.S. For bonus points, what's the best way of sorting the file alphabetically and numerically according to the hex characters (i.e. 000000 -> FFFFFF)? Should I just use sort?

Best Answer

$ awk '$1 ~ /^[[:xdigit:]]{6}$/' file
00107B  Cisco Systems, Inc
00906D  Cisco Systems, Inc
0090BF  Cisco Systems, Inc
000C6E  ASUSTek COMPUTER INC.
001BFC  ASUSTek COMPUTER INC.
001E8C  ASUSTek COMPUTER INC.
0015F2  ASUSTek COMPUTER INC.
001FC6  ASUSTek COMPUTER INC.
60182E  ShenZhen Protruly Electronic Ltd co.
F4CFE2  Cisco Systems, Inc
501CBF  Cisco Systems, Inc

This uses awk to extract the lines that contains exactly six hexadecimal digits in the first field. The [[:xdigit:]] pattern matches a hexadecimal digit, and {6} requires six of them. Together with the anchoring to the start and end of the field with ^ and $ respectively, this will only match on the wanted lines.

Redirect to some file to save it under a new name.

Note that this seems to work with GNU awk (commonly found on Linux), but not with awk on e.g. OpenBSD, or mawk.


A similar approach with sed:

$ sed -n '/^[[:xdigit:]]\{6\}\>/p' file
00107B  Cisco Systems, Inc
00906D  Cisco Systems, Inc
0090BF  Cisco Systems, Inc
000C6E  ASUSTek COMPUTER INC.
001BFC  ASUSTek COMPUTER INC.
001E8C  ASUSTek COMPUTER INC.
0015F2  ASUSTek COMPUTER INC.
001FC6  ASUSTek COMPUTER INC.
60182E  ShenZhen Protruly Electronic Ltd co.
F4CFE2  Cisco Systems, Inc
501CBF  Cisco Systems, Inc

In this expression, \> is used to match the end of the hexadecimal number. This ensures that longer numbers are not matched. The \> pattern matches a word boundary, i.e. the zero-width space between a word character and a non-word character.


For sorting the resulting data, just pipe the result trough sort, or sort -f if your hexadecimal numbers uses both upper and lower case letters

Related Question