题解 | #格式化输出#
格式化输出
https://www.nowcoder.com/practice/d91a06bfaff443928065e611b14a0e95
#!/bin/bash
# 定义一个函数来格式化数字串
format_number() {
local number="$1"
# 使用sed命令每3个数字插入一个逗号
echo "$number" | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta'
}
# 遍历文件中的每一行并调用format_number函数处理
while IFS= read -r line; do
formatted_line=$(format_number "$line")
echo "$formatted_line"
done < "nowcoder.txt"
