使用sed从输出中删除不需要的字符 [英] Using sed to remove unwanted characters from output
问题描述
我正在bash shell中运行以下命令,以尝试仅返回 []
中的十六进制字符和紧随其后的整数值.第一次sed调用将替换所有内容,包括 [
在内的所有内容.第二个替换:
和]
.该命令会产生所需的结果,但是我想在一个sed语句中完成此操作,即无需再次通过sed进行遍历.我已经尝试了这两个regex表达式的很多组合,但是找不到能够产生所需结果的组合.
I am running the following command in a bash shell in an attempt to return only the hexadecimal characters inside the []
and the integer value immediately following. The first sed call replaces everything up to and including the [
. The second replaces :
and ]
. The command produces the desired result however I'd like to accomplish this in a single sed statement, i.e. without having to pipe through sed a second time. I've attempted a good many combinations of these two regex expressions but have not been able to find a combination that produces the desired result.
echo 'dot1dTpFdbPort[00:02:6f:d9:16:ca] 12' | sed 's/^.*\[//g' | sed 's/[][\:]//g'
00026fd916ca 12
推荐答案
也许您可以尝试通过一个唯一的sed命令使用两个正则表达式:
maybe you can try using the two regex with one unique sed command:
echo 'dot1dTpFdbPort[00:02:6f:d9:16:ca] 12' | sed -e 's/^.*\[//g' -e 's/[][\:]//g'
该命令可以完全按照您的要求产生所需的结果:仅在一次sed指令中即可.
That command produces your desired result exactly what you want: in only one sed stetment.
礼炮.
这篇关于使用sed从输出中删除不需要的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!