偶遇的shell题:
把"------"短线上的数据处理成线下的数据
0,1,2,10,22,00,01,2,3,33
-----------------------------------
00,01,02,10,22,00,01,02,03,33
我承认,我落伍了,智力测验 + 能力检验么?
方法一: awk + awk 内置字符处理函数 + awk 控制语句
方法二: bash
貌似bash对文本批量处理的功能比较弱,不过分割以后,再通过bash的变量扩展(Parameter Expansion)还是很方便的。
其它语言,python,perl应该会更好处理。
方法一:
echo -en '0,1,2,10,22,00,01,2,3,33' |awk 'BEGIN{RS=",";ORS=","}{if (length($0)<2) {print "0"$0} else { print $0} }' |sed -r 's/^(.*),$/\1/g'
方法二:
#!/bin/bash str="0,1,2,10,22,00,01,2,3,33" typeset -i n typeset -a tmp for s in ${str//,/ } do [ ${#s} -lt 2 ] && tmp[n]=0$s || tmp[n]=$s ((n++)) done STR=${tmp[*]} echo ${STR// /,}
转载于:https://blog.51cto.com/wangxiaoyu/1324068