以空格作为分隔符时 cut和awk处理的差异


http://blog.163.com/hot4321@126/blog/static/16538535820105613258809/

[rocrocket@rocrocket programming]$ cat tab_space.txt |cut -d ' ' -f 1
this
this注意,两个单引号之间可确实要有一个空格哦,不能偷懒。
而且,你只能在-d后面设置一个空格,可不许设置多个空格,因为cut只允许间隔符是一个字符。

ls -lc | cut -d ' '   -s -f 8-

10 cut有哪些缺陷和不足?

猜出来了吧?对,就是在处理多空格时。
如果文件里面的某些域是由若干个空格来间隔的,那么用cut就有点麻烦了,因为cut只擅长处理“以一个字符间隔”的文本内容

/* 参考:

http://linuxop.blog.51cto.com/13377/249094
结论:
awk 以空格为分割域时,是以单个或多个连续的空格为分隔符的;
cut则是以单个空格作为分隔符。

*/


例子:
[wyh@ qt-everywhere-opensource-src-4.7.4 12:22:25]$ls -lc | awk '{print $6,$7,$8,$9}' | sort > /tmp/ls_lc_sorted.txt
[wyh@ qt-everywhere-opensource-src-4.7.4 12:22:46]$ls --time=c -lrth | awk '{print $6,$7,$8,$9}' > /tmp/ls_time-c_lrth.txt


GOOGLE:  linux awk printf field till line end  $NF;

awk中只能使用for循环!:

You could use a for-loop to loop through printing fields $2 through $NF (built-in variable that represents the number of fields on the line).

Edit: Since "print" appends a newline, you'll want to buffer the results: awk '{out=""; for(i=2;i<=NF;i++){out=$out" "$i}; print $out}'

 Alternatively, use printf:   awk 'for(i=2;i<=NF;i++){printf "%s ", $i}; printf "\n"}'

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值