Si vous utilisez Linux, il a un programme printf
pour cela. D'autres variantes UNIX peuvent également l'avoir.
Rembourrage un numérique avec x
est pas vraiment sur des cas d'utilisation, mais vous pouvez obtenir le même résultat avec:
pax> printf "%7d\n" 250 | tr ' ' 'x'
xxxx250
qui délivre 250 avec un rembourrage de l'espace, utilise alors le tr
traduire utilitaire pour transformer ces espaces en x
caractères.
Si vous cherchez une bash
solution -Seulement, vous pouvez commencer par:
pax> n=250 ; echo ${n}
250
pax> n=xxxxxxx${n} ; echo ${n}
xxxxxxx250
pax> n=${n: -7} ; echo ${n}
xxxx250
Si vous voulez une solution généralisée, vous pouvez utiliser cette fonction fmt
, le code de test unitaire est inclus :
#!/bin/bash
#
# fmt <string> <direction> <fillchar> <size>
# Formats a string by padding it to a specific size.
# <string> is the string you want formatted.
# <direction> is where you want the padding (l/L is left,
# r/R and everything else is right).
# <fillchar> is the character or string to fill with.
# <size> is the desired size.
#
fmt()
{
string="$1"
direction=$2
fillchar="$3"
size=$4
if [[ "${direction}" == "l" || "${direction}" == "L" ]] ; then
while [[ ${#string} -lt ${size} ]] ; do
string="${fillchar}${string}"
done
string="${string: -${size}}"
else
while [[ ${#string} -lt ${size} ]] ; do
string="${string}${fillchar}"
done
string="${string:0:${size}}"
fi
echo "${string}"
}
# Unit test code.
echo "[$(fmt 'Hello there' r ' ' 20)]"
echo "[$(fmt 'Hello there' r ' ' 5)]"
echo "[$(fmt 'Hello there' l ' ' 20)]"
echo "[$(fmt 'Hello there' l ' ' 5)]"
echo "[$(fmt 'Hello there' r '_' 20)]"
echo "[$(fmt 'Hello there' r ' .' 20)]"
echo "[$(fmt 250 l 'x' 7)]"
Ce sorties:
[Hello there ]
[Hello]
[ Hello there]
[there]
[Hello there_________]
[Hello there . . . . ]
[xxxx250]
et vous n'êtes pas limité juste de les imprimer, vous pouvez également enregistrer les variables pour plus tard avec une ligne telle que:
formattedString="$(fmt 'Hello there' r ' ' 20)"
Cela a fonctionné parfaitement. Merci beaucoup! – raytracer