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"}'