awk用法:取列表最后一列
很多時候我們有需要取出文本中的最后一列,或者比如 ls -l 最后一列為文件名,這里可以使用多種方法取出最后一列;
舉例說明:
[root@testserver tmp]# ls -l
總用量 1000
-rw-r--r-- 1 root ?root ?925223 6月 ?15 18:06 0615.txt
-rw-r--r-- 1 root ?root ? ? ?84 6月 ?15 15:07 dir.txt
-rw-r--r-- 1 root ?root ? ? ?86 6月 ?14 12:00 for.sh
-rw-r--r-- 1 root ?root ? ? 715 6月 ?14 12:09 G.txt
-rw-r--r-- 1 root ?root ? ?1614 6月 ?14 12:07 M.txt
-rw-r--r-- 1 root ?root ? 37763 6月 ?14 12:00 size.txt
-rw-r--r-- 1 root ?root ? 35579 6月 ?14 11:51 test.txt
方法1:
直接使用awk $NF取最后一列,問題來了,注意取出來的第一行,包括ls -l 顯示的第一行總用量 1000這個數(shù)值;
[root@testserver?tmp]#?ls?-l|awk?'{print?$NF}' 1000 0615.txt dir.txt for.sh G.txt M.txt size.txt test.txt方法2:
同樣awk $NF,加一個條件NR>1,跳過第一行
[root@testserver?tmp]#?ls?-l|awk?'NR>1?{print?$NF}' 0615.txt dir.txt for.sh G.txt M.txt size.txt test.txt方法3:
使用awk next函數(shù),NR==1 跳過第一行
[root@testserver?tmp]#?ls?-l|awk?'NR==1?{next}?{print?$NF}' 0615.txt dir.txt for.sh G.txt M.txt size.txt test.txt方法4:
可以寫一個for循環(huán),并定向到一個文件中
可以使用for循環(huán)取文件列表??for?i?in?`ls`;do?echo?$i>>dir.txt;done;
[root@testserver?tmp]#?cat?dir.txt? 0615.txt dir.txt for.sh G.txt M.txt size.txt test.txt轉(zhuǎn)載于:https://blog.51cto.com/mofansheng/1789910
總結(jié)
以上是生活随笔為你收集整理的awk用法:取列表最后一列的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 解决jquery的scrollTop()
- 下一篇: 语句
