简体   繁体   English

Mac 地址的 Grep 正则表达式

[英]Grep Regex for Mac Address

I need help with a regex to extract mac addresses from a large file.我需要使用正则表达式从大文件中提取 mac 地址的帮助。

Here is the format of the file:这是文件的格式:

Wed Apr 25 10:15:32 EDT 2012 Client: 00aa11bb22cc mac
Wed Apr 25 10:15:34 EDT 2012 Client: aa11bb22cc33 pc

Here is what I am currently trying with no luck:这是我目前正在尝试但没有运气的内容:

grep -io '[0-9a-f]{12}' file.txt > macs.txt

Any ideas?有任何想法吗? I just want to extract only the mac address part into the file called macs.txt .我只想将 mac 地址部分提取到名为macs.txt的文件中。

尝试:

grep -io '[0-9a-f]\{12\}' file.txt > macs.txt
((([0-9a-fA-F]{2})[ :-]){5}[0-9a-fA-F]{2})|(([0-9a-fA-F]){6}[
:-]([0-9a-fA-F]){6})|([0-9a-fA-F]{12})

I tested it against following expression that I know for MAC-Adresses.我根据我知道的 MAC 地址的以下表达式对其进行了测试。 Depending on the syntax of systems, firmwares and applikations.取决于系统、固件和应用程序的语法。

F3 D3 A4 C1 99 E2
F3:D3:A4:C1:99:E2
F3-D3-A4-C1-99-E2
f3 d3 a4 c1 99 e2
f3:d3:a4:c1:99:e2
f3-d3-a4-c1-99-e2
f3d3a4 c199e2
f3d3a4:c199e2
f3d3a4-c199e2
f3d3a4c199e2
F3D3A4C199E2

One way using sed :使用sed一种方法:

sed -e 's/^.*\([a-fA-F0-9]\{12\}\).*$/\1/' file.txt > macs.txt

Result:结果:

00aa11bb22cc
aa11bb22cc33

egrep would work: egrep 可以工作:

egrep -io '[0-9a-f]{12}' file.txt > macs.txt

There are 2 options for grep to handle regex, grep 有 2 个选项来处理正则表达式,

grep -e 

and

grep -E

One of them is equal to egrep and I guess now you know which.其中之一等于 egrep,我想现在您知道是哪个了。 :) :)

This command line will extract all the MAC addresses from a regular file.此命令行将从常规文件中提取所有 MAC 地址。 It works on macOS.它适用于 macOS。

It extracts from a regular file any MAC address on it.它从常规文件中提取任何 MAC 地址。 In the example, the regular file file.txt has mixed information including MAC addresses.在示例中,常规文件file.txt具有混合信息,包括 MAC 地址。 The following command will extract them and save them on the macs.txt file.以下命令将提取它们并将它们保存在macs.txt文件中。

grep -o -E '([[:xdigit:]]{1,2}:){5}[[:xdigit:]]{1,2}' file.txt > macs.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM