首页 > 其他 > 详细

Bash 替换连续多个空格为一个

时间:2015-10-13 22:51:09      阅读:489      评论:0      收藏:0      [点我收藏+]

现在我们有一个文件,想要获取所有用户的工资列:

[dell@localhost res]$ cat table
100  Thomas  Manager    Sales       $5,000  
200  Jason   Developer  Technology  $5,500  
300  Sanjay  Sysadmin   Technology  $7,000  
400  Nisha   Manager    Marketing   $9,500  
500  Randy   DBA        Technology  $6,000

也许你会想到cut命令,然后呢?cut -d ‘ ‘ -f 5 ,是否缺了点什么,对了,怎么把连续多个空格转化为一个呢?

哎,对了。还记不记得有个现成的命令tr,让我们看一看吧

  -c, -C, --complement    use the complement of SET1
  -d, --delete                    delete characters in SET1, do not translate
  -s, --squeeze-repeats   replace each input sequence of a repeated character
                            that is listed in SET1 with a single occurrence of that character

   看到第三个选项了没,现成的。好吧,现在试一下:

    [dell@localhost res]$ cat table | tr -s ‘ ‘ | cut -d ‘ ‘ -f 5
    $5,000
    $5,500
    $7,000
    $9,500
    $6,000

哇噻,感觉好吊的样子,有没有其它方法呢?当然还有很多,我们就简单的拿sed说说吧,关于sed命令,请君自查吧

    [dell@localhost res]$ cat table | sed ‘s/  */ /g‘ | cut -d ‘ ‘ -f 5
    $5,000
    $5,500
    $7,000
    $9,500
    $6,000

    记得上面是两个空格吆,奥,对了推荐一篇介绍神奇的正则的:http://net.pku.edu.cn/~yhf/tutorial/tao_regexps_zh.html#HardExamples

Bash 替换连续多个空格为一个

原文:http://www.cnblogs.com/kellis/p/4875903.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!