函数/脚本,对于格式(字母和数字)的参数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