By Rocco


2019-04-10 14:46:43 8 Comments

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?

2 comments

@Digital Trauma 2019-04-10 20:03:46

And for completeness, you can do this with grep too:

$ grep -E '^[[:xdigit:]]{6}\b' oui.txt 
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 extended grep expression searches for exactly 6 hex digits at the beginning of each line, followed immediately by a non-whitespace-to-whitespace boundary (\b).

@Kusalananda 2019-04-10 14:55:45

$ 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

@Rocco 2019-04-10 15:07:25

Perfect, thank you very much. Exactly what I was looking for!

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Delete n lines after pattern and m lines before pattern

5 Answered Questions

[SOLVED] How to delete all lines that contain only duplicates characters?

  • 2018-09-28 05:46:53
  • Ahmed
  • 308 View
  • -4 Score
  • 5 Answer
  • Tags:   text-processing

2 Answered Questions

[SOLVED] Delete last characters before delimiter

4 Answered Questions

[SOLVED] If Extra delimiter delete last delimiter

4 Answered Questions

[SOLVED] How to print the inputted pattern which don't have matching lines?

2 Answered Questions

[SOLVED] Delete all lines of text that isn't all numbers

6 Answered Questions

[SOLVED] How can I wrap text at a certain column size?

  • 2011-11-23 05:58:07
  • cwd
  • 70814 View
  • 135 Score
  • 6 Answer
  • Tags:   text-processing

3 Answered Questions

[SOLVED] Count all lines between a delimiter

2 Answered Questions

Sponsored Content