- 论坛徽章:
- 0
|
原帖由 woodie 于 2006-10-12 08:50 发表
兄弟这样也行?应该是从个位开始向高位方向,每三位加逗号。可以改改再用。^_^
另外$CO上应该没有bash。
如果有perl的话,可以搜索一下我以前的写的一行perl,关键字就用“牛与非牛吧”。^_^
BASH 可以了,不用 perl 吧
#! /bin/bash
# add_comma
for input
do
length=${#input}
[ -n "$input" ] || exit 2
case "$length" in
1|2|3) pat="$input" ;;
4) pat="${input:0:1},${input:1}" ;;
5) pat="${input:0:2},${input:2}" ;;
6) pat="${input:0:3},${input:3}" ;;
7|8|9) pat="${input:0:3},${input:3:3},${input:6}" ;;
10|11|12) pat="${input:0:3},${input:3:3},${input:6:3},${input:9}" ;;
esac
echo $pat
done
[victor@localhost ~]$ ./addcomma.sh 123 45678 9876543
123
45,678
987,654,3
[victor@localhost ~]$ ./addcomma.sh 123 45678 9876543 1111111
123
45,678
987,654,3
111,111,1 |
|