我正在处理一个处理OUI mac地址数据库(http://standards.ieee.org/regauth/oui/oui.txt)的脚本,示例输出如下所示:
40-25-C2 (hex) Intel Corporate
4025C2 (base 16) Intel Corporate
Lot 8, Jalan Hi-Tech 2/3
Kulim Hi-Tech Park
Kulim Kedah 09000
MALAYSIA
40-27-0B (hex) Mobileeco Co., Ltd
40270B (base 16) Mobileeco Co., Ltd
#2126, IT Tower B, Keumkang Penterium Bldg, 810
Kwanyang-Dong, Dongan-Ku
Anyang City Kyunggi-Do 431810
KOREA, REPUBLIC OF
最后,我希望每一行看起来像这样:
40:25:C2 Intel Corporate
40:27:0B Mobileeco Co., Ltd
我不知道最好的方法是什么,到目前为止我一直采取一步这是我迄今为止所做的一次
sed '/base 16/!d' test.txt > test1.txt # delete all extra lines
sed 's/^...//' test1.txt > test2.txt # delete 3 spaces at the beginning of each line
下一步是删除空格和(基数16),我似乎无法让它工作......或者我将如何添加:'s
除非有更好的方法来做到这一点。
我还需要在osx和ubuntu
中运行它提前感谢!
答案 0 :(得分:2)
一种方法是说:
sed -r -n '/base 16/{s/\s+(..)(..)(..)\s+\([^)]*\)\s+/\1:\2:\3 /p}' test.txt
为了您的输入,它会产生:
40:25:C2 Intel Corporate
40:27:0B Mobileeco Co., Ltd
或者,您可以说:
sed -n '/base 16/{s/\s*\(..\)\(..\)\(..\)\s*([^)]*)\s*/\1:\2:\3 /p}' test.txt
这应该适用于Ubuntu和OSX。
答案 1 :(得分:1)
@devnull的建议在POSIX sed中重写:
sed -n '/base 16/{s/[[:blank:]]*\(..\)\(..\)\(..\)[[:blank:]]*([^)]*)[[:blank:]]*/\1:\2:\3 /p;}' file
最后的右大括号需要以分号为前缀。
答案 2 :(得分:0)
这是使用awk
的一种方式:
awk '{ sub("\n.*",""); gsub("-",":",$1); $2="" }1' RS= file
结果:
40:25:C2 Intel Corporate
40:27:0B Mobileeco Co., Ltd
与上面的解决方案一样好,输出中还有一个额外的空间,它只是通过查看每条记录的第一行来作弊。在阅读上面的代码之后,看起来您只对每条记录的第二行感兴趣,即包含“base 16”的行。这是使用awk
处理这些问题的另一种解决方案。为了便于阅读,我将其拆分为多行:
awk '{
n = split($2, a, OFS);
gsub(/..\B/,"&:",a[1]);
for (i=4;i<=n;i++) {
r = (r ? r OFS : "") a[i];
}
print a[1], r
}' FS="\n" RS= file
结果:
40:25:C2 Intel Corporate
40:27:0B Intel Corporate Mobileeco Co., Ltd
答案 3 :(得分:0)
这是另一个awk
awk -F") +" '/hex/ {split($1,a," ");gsub(/-/,":",a[1]);print a[1],$2}' file
40:25:C2 Intel Corporate
40:27:0B Mobileeco Co., Ltd