shell字符串处理
A=“fe:48:00:00:03:00:00:02:00:10:00:09:c9:03:b2:58"
转换规则:
从字符串首开始,每5个字符为一组,组之间“:”保留,如fe:48为一组,
第二组为00:00....最后一组为b2:58。每组中冒号去掉,从左到右后连续的0去掉,如全部为0,保留一个0,如:
fe:48->fe48
00:00->0
03:00->300
0002->2
00:10->10
00:09->9
c9:03->c903
b2:58->b258
A="fe:48:00:00:03:00:00:02:00:10:00:09:c9:03:b2:58"
B=`echo $A
[解决办法]
sed 's/\(..\):\(..\)/\1\2/g;s/\<0*\([0-9]\)/\1/g'`
echo $B
[nicenight@CSDN ~]$ B=$(echo -n $A
[解决办法]
awk -v RS=":" -v ORS=":" '{getline nextline; $0=$0""nextline; sub(/^0*/, "", $0); $0=$0==""?0:$0; result=result=="" ? $0 : result ORS $0} END{printf result}')
[nicenight@CSDN ~]$ echo $B
fe48:0:300:2:10:9:c903:b258