在数字列表中添加前导空格?
Add leading spaces to a list of numbers?
我有一个数字列表,我希望以一种排列方式显示这些数字,例如
xxxxxx 1 of 100
xxxxxx 2 of 100
...
xxxxxx 10 of 100
...
xxxxxx 99 of 100
xxxxxx 100 of 100
如示例代码所示---即我说的是 while
循环内的部分——而不是为每种可能的情况编写 if
条件,是否有更好的解决方案来添加必要的前导 space $current
变量,因此 echo
行的输出总是至少有两个 space 间隙?
到目前为止,这是我的代码:
#!/bin/bash
counter=1
totallines=
example="xxxxxx"
while [[ $counter -le $totallines ]]; do
# set up pretty formatting (leading spaces)
if [ "$counter" -le 9 ]; then
current=" $counter"
# two digit formatting
elif [ "$counter" -ge 10 ] && [ "$counter" -le 99 ]; then
current=" $counter"
# three digit formatting
elif [ "$counter" -ge 99 ] && [ "$counter" -le 999 ]; then
current=" $counter"
# beyond four digit formatting
else
current="$counter"
fi
echo "${example} $current of $totallines"
counter=$(($counter +1))
done
说明不同长度列表的一些示例:
期望的输出
xxxxxx 1 of 10
...
xxxxxx 10 of 10
xxxxxx 1 of 1000
...
xxxxxx 1000 of 1000
xxxxxx 1 of 10000
...
xxxxxx 10000 of 10000
xxxxxx 1 of 100000
...
xxxxxx 100000 of 100000
实际产量
xxxxxx 1 of 10
...
xxxxxx 10 of 10
xxxxxx 1 of 1000
...
xxxxxx 1000 of 1000
xxxxxx 1 of 10000
...
xxxxxx 9999 of 10000
xxxxxx 10000 of 10000
xxxxxx 1 of 100000
...
xxxxxx 99999 of 100000
xxxxxx 100000 of 100000
有什么想法吗?
先决条件是它适合上面已经存在的循环(这是我尝试将此解决方案应用到的循环的简化)。
谢谢!
您可以为此使用 printf
:
totallines=${1?needs an argument}
something="texthere"
for ((counter=1; counter <= totallines; counter++)); do
printf "%s %*d of %d\n" \
"$something" $((${#totallines}+1)) $counter $totallines
done
和运行它是:
bash script.sh 10
xxxxxx 1 of 10
xxxxxx 2 of 10
xxxxxx 3 of 10
xxxxxx 4 of 10
xxxxxx 5 of 10
xxxxxx 6 of 10
xxxxxx 7 of 10
xxxxxx 8 of 10
xxxxxx 9 of 10
xxxxxx 10 of 10
详情:
for
从 1 到给定 totallines
循环 运行s(作为参数传递)
printf
打印格式化输出。
%*d
使用给定参数的宽度打印 counter
以获得正确对齐。给定的参数是 $((${#totallines}+1))
这是 $totallines + 1
的长度
我有一个数字列表,我希望以一种排列方式显示这些数字,例如
xxxxxx 1 of 100
xxxxxx 2 of 100
...
xxxxxx 10 of 100
...
xxxxxx 99 of 100
xxxxxx 100 of 100
如示例代码所示---即我说的是 while
循环内的部分——而不是为每种可能的情况编写 if
条件,是否有更好的解决方案来添加必要的前导 space $current
变量,因此 echo
行的输出总是至少有两个 space 间隙?
到目前为止,这是我的代码:
#!/bin/bash
counter=1
totallines=
example="xxxxxx"
while [[ $counter -le $totallines ]]; do
# set up pretty formatting (leading spaces)
if [ "$counter" -le 9 ]; then
current=" $counter"
# two digit formatting
elif [ "$counter" -ge 10 ] && [ "$counter" -le 99 ]; then
current=" $counter"
# three digit formatting
elif [ "$counter" -ge 99 ] && [ "$counter" -le 999 ]; then
current=" $counter"
# beyond four digit formatting
else
current="$counter"
fi
echo "${example} $current of $totallines"
counter=$(($counter +1))
done
说明不同长度列表的一些示例:
期望的输出
xxxxxx 1 of 10
...
xxxxxx 10 of 10
xxxxxx 1 of 1000
...
xxxxxx 1000 of 1000
xxxxxx 1 of 10000
...
xxxxxx 10000 of 10000
xxxxxx 1 of 100000
...
xxxxxx 100000 of 100000
实际产量
xxxxxx 1 of 10
...
xxxxxx 10 of 10
xxxxxx 1 of 1000
...
xxxxxx 1000 of 1000
xxxxxx 1 of 10000
...
xxxxxx 9999 of 10000
xxxxxx 10000 of 10000
xxxxxx 1 of 100000
...
xxxxxx 99999 of 100000
xxxxxx 100000 of 100000
有什么想法吗?
先决条件是它适合上面已经存在的循环(这是我尝试将此解决方案应用到的循环的简化)。
谢谢!
您可以为此使用 printf
:
totallines=${1?needs an argument}
something="texthere"
for ((counter=1; counter <= totallines; counter++)); do
printf "%s %*d of %d\n" \
"$something" $((${#totallines}+1)) $counter $totallines
done
和运行它是:
bash script.sh 10
xxxxxx 1 of 10
xxxxxx 2 of 10
xxxxxx 3 of 10
xxxxxx 4 of 10
xxxxxx 5 of 10
xxxxxx 6 of 10
xxxxxx 7 of 10
xxxxxx 8 of 10
xxxxxx 9 of 10
xxxxxx 10 of 10
详情:
for
从 1 到给定totallines
循环 运行s(作为参数传递)printf
打印格式化输出。%*d
使用给定参数的宽度打印counter
以获得正确对齐。给定的参数是$((${#totallines}+1))
这是$totallines + 1
的长度