Chinaunix

标题: 每两个字符之间插入一个空字符怎么写? [打印本页]

作者: reer    时间: 2009-08-31 10:30
标题: 每两个字符之间插入一个空字符怎么写?
每两个字符之间插入一个空字符,这样的脚本怎么写?
作者: MYSQLER    时间: 2009-08-31 10:34
这样?

echo ab|awk -F '' '{print $1 " " $2}'
a b
作者: ly5066113    时间: 2009-08-31 10:34
awk -F '' '$1=$1'
作者: blackold    时间: 2009-08-31 10:48
LZ是这个意思吧?
  1. sed 's/.\{2\}/& /g' urfile
复制代码


作者: gnubuntu    时间: 2009-08-31 11:31
提示: 作者被禁止或删除 内容自动屏蔽
作者: reer    时间: 2009-08-31 13:58
是你这个意思

都是大牛啊,你的脚本我要看过文档后才明白意思

谢谢啊!


[quote]原帖由 [i]blackold[/i] 于 2009-8-31 10:48 发表 [url=http://bbs2.chinaunix.net/redirect.php?goto=findpost&pid=11131126&ptid=1560068][img]http://bbs2.chinaunix.net/images/common/back.gif[/img][/url]
LZ是这个意思吧?sed 's/.\{2\}/& /g' urfile
http://bbs2.chinaunix.net/images/smilies/icon_mrgreen.gif [/quote]
作者: reer    时间: 2009-08-31 13:59
是abcde变成ab cd e



原帖由 gnubuntu 于 2009-8-31 11:31 发表
怎么个意思?

abcde

变成a b c d e还是ab cd e啊。。。

作者: none311    时间: 2009-08-31 14:56
原帖由 blackold 于 2009-8-31 10:48 发表
LZ是这个意思吧?sed 's/.\{2\}/& /g' urfile


我想问下 其中那个 & 是什么意思
作者: blackold    时间: 2009-08-31 14:58
标题: 回复 #8 none311 的帖子
正则基础: 就是匹配pattern的字符串。
作者: gnubuntu    时间: 2009-08-31 15:56
提示: 作者被禁止或删除 内容自动屏蔽
作者: lucash    时间: 2009-08-31 16:41
sed 's/../& /g'
作者: liuzhy_cu    时间: 2019-06-27 16:58
重新翻起10年前的老帖子。。。

有一串数字, 需要按  千位 分开, 从右边数起,每隔3个字符 插一个 逗号 ,
如: 5166128986117 , 需要得到 5,166,128,986,117

但这样运行,得到从左边数起,每隔3个字符 插一个 逗号 , 不是希望得到的, 怎么优化?
# echo "5166128986117" |sed 's/.\{3\}/&,/g'
516,612,898,611,7


作者: jzsjm1002    时间: 2019-06-27 17:42
回复 12# liuzhy_cu

echo "5166128986117" | sed -r ':a;s/^([0-9]+)([0-9]{3})/\1,\2/;ta'
作者: wh7211    时间: 2019-06-27 17:55
回复 12# liuzhy_cu


  1. cat 1
  2. 1
  3. 12
  4. 123
  5. 1234
  6. 12345
  7. 123456
  8. 1234567
  9. 12345678
  10. 123456789
  11. 1234567890

  12. awk 'BEGIN{FS=OFS=""}{a=length($0)%3;a=a==0?3:a;for(i=a;i<NF;i+=3){$i=$i","}}1' 1
  13. 1
  14. 12
  15. 123
  16. 1,234
  17. 12,345
  18. 123,456
  19. 1,234,567
  20. 12,345,678
  21. 123,456,789
  22. 1,234,567,890
复制代码

作者: csccyab    时间: 2019-06-29 15:15
BASH 版本

cat 1.sh

#!/bin/bash

head=$0
while [ ${#head} -gt 3 ]; do
   tail=${head: -3}
   head=${head:0: -3}
   if [ -z ${result} ]; then
      result=${tail}
   else
      result="${tail},${result}"
   fi
   if [ ${#head} -le 3 ]; then
      result="${head},${result}"
   fi
done
echo ${result}

./1.sh 123456789
123,456,789

./1.sh 12345678
12,345,678

./1.sh 1234567
1,234,567

./1.sh 123456
123,456




作者: csccyab    时间: 2019-06-29 19:46
Python 一行版本

echo 1234567890 | python3.6 -c 'import sys; f = lambda x: x if len(x) <=3 else f(x[:-3]) + "," + x[-3:]; print(f(sys.stdin.read()[:-1]))'
1,234,567,890


作者: cfwyy    时间: 2019-07-02 09:01
回复 12# liuzhy_cu

python内置的format函数
  1. x=5166128986117
  2. format(x,',')

  3. '5,166,128,986,117'
复制代码







欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2