现在我们有一个文件,想要获取所有用户的工资列:
[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
原文:http://www.cnblogs.com/kellis/p/4875903.html