![打印数字后面的字符](https://linux22.com/image/982044/%E6%89%93%E5%8D%B0%E6%95%B0%E5%AD%97%E5%90%8E%E9%9D%A2%E7%9A%84%E5%AD%97%E7%AC%A6.png)
函数/脚本,对于格式(字母和数字)的参数afa5hgt3tt
,errerew9ax66w
将数字后面的字符多次打印为数字的值,例如
hgthgthgthgthgttttttt
axaxaxaxaxaxaxaxaxaxaxwwwwwww...
我的脚本:
#!/bin/bash
func () {
repeats=$(echo $1 | sed -e 's/\([A-Za-z]*\)\([0-9]*\)\([A-Za-z]*\)\([0-9]*\)\([A-Za-z]*\)/\2/g')
for (( i = 0; i < $repeats; i++ )); do
echo $1 | sed -e 's/\([A-Za-z]*\)\([0-9]*\)\([A-Za-z]*\)\([0-9]*\)\([A-Za-z]*\)/\3/g' | tr -d "\n"
done
echo -e "\n"
}
func afa5hgt3tt
func errerew9ax66w
我的函数无法打印数字后面的最后一个字符。即 ttttt 和 wwww。
答案1
使用perl
(Thx@terdon)
perl -pe 's/(.)(\d+)+/$1 x $2/eg'
或者作为函数
func () {
perl -pe 's/(.)(\d+)+/$1 x $2/eg' <<< "$1"
}
例子
% perl -pe 's/(.)(\d+)+/$1 x $2/eg' <<< "afa5hgt66tt"
afaaaaahgtttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt
% perl -pe 's/(.)(\d+)+/$1 x $2/eg' <<< "errerew9ax66w"
errerewwwwwwwwwaxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxw
% perl -pe 's/(.)(\d+)+/$1 x $2/eg' <<< "afa5hgt3tt"
afaaaaahgttttt