Sed删除由包含关键字的行指示的MAC地址

问题描述 投票:0回答:1

我正在对包含特定字符的行中的MAC地址进行混淆。但是,我只想在“事件”关键字指示的行中将其删除。对我而言,困难的是,“偶数”关键字和MAC地址之间的关键字(包括数字和字母)的长度将是随机的。所以我要替换:

[random length combinations of words, numbers, potentially punctuation or space] "event" [random length combinations of words, numbers, potentially punctuation or space] "xx:xx:xx:xx:xx:xx" [random length combinations of words, numbers, potentially punctuation, nothing at all or space] 

至“ x”我只知道如何混淆MAC地址:

sed -E 's/'([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}')/ x /g'

但是我不知道如何修改它以使其模糊于特定行。例如,我有一个文件input.txt:

[INFO] device 1 event - client connected with aa:bb:8a:de:8e:23
[ERROR] device 2 event - routing data to gg:7a:e3:89:2f:2f failed
[LOG] device 3 connected 
[INFO] connected to 2 devices. event: aa:bb:8a:de:8e:23 success!

输出应为:

[INFO] device 1 event - client connected with x
[ERROR] device 2 event - routing data to x failed
[LOG] device 3 connected 
[INFO] connected to 2 devices. event: x success!
sed mac-address
1个回答
0
投票

[这里是一种可能的sed解决方案-允许event出现在行中的任何位置,它必须是一个完整的单词(不是更长的单词的一部分,例如eventful),并且可能有多个MAC地址在一行上-如果单词event在同一行上,则必须全部屏蔽。我也使用xx:xx:xx:xx:xx:xx作为遮罩,但是您可以轻松地替换它。

我创建了以下输入文件进行测试:

$ cat mac.input

[INFO] device 1 event - client connected with aa:bb:8a:de:8e:23
[ERROR] device 2 event - routing data to gg:7a:e3:89:2f:2f failed
[LOG] device 3 connected 
[INFO] connected to 2 devices. event: aa:bb:8a:de:8e:23 success!
[LINE] this was an eventful evening 22:33:22:33:22:33
[TWO MAC ADDRESSES] aa:bb:aa:33:dd:1f is event 99:33:00:23:43:83 good

正如我已经提到的,第二行的“地址”将不会被屏蔽(即使存在单词event),因为该地址不是开头为有效的MAC地址-g不是十六进制数字。 [LINE]行上的地址将不会被屏蔽,因为单词event本身并不存在(即使eventful存在)。在最后一行,有两个MAC地址,而我两个都被屏蔽了-即使是出现在单词事件before的那个MAC地址。 (我在评论中要求您澄清这种情况是否可能,如果可能,必须如何处理;在这里,我只是做出了一个随机选择以表明可能的情况。)

所以,这是sed命令及其输出:

$ sed -E '/\bevent\b/
>   s/([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}/xx:xx:xx:xx:xx:xx/g' mac.input 

[INFO] device 1 event - client connected with xx:xx:xx:xx:xx:xx
[ERROR] device 2 event - routing data to gg:7a:e3:89:2f:2f failed
[LOG] device 3 connected 
[INFO] connected to 2 devices. event: xx:xx:xx:xx:xx:xx success!
[LINE] this was an eventful evening 22:33:22:33:22:33
[TWO MAC ADDRESSES] xx:xx:xx:xx:xx:xx is event xx:xx:xx:xx:xx:xx good
© www.soinside.com 2019 - 2024. All rights reserved.