2010年10月4日 星期一

[ Linux 小技巧 ] cut 常見使用範例


Source (VBird)
底下我們會對常用的 cut 使用方法做介紹, 而 cut 語法可以參考 這裡.
範例一 : 將 PATH 變數取出, 並找出第五個路徑.
linux-tl0r:~ # echo $JPATH
/home/john/tools/jdk1.6.0_16/bin/:/usr/local/scala/bin/:/usr/lib/mpi/gcc/openmpi/bin:/sbin:/usr/sbin:/usr/local/sbin <第五欄為 /usr/sbin>
linux-tl0r:~ # echo $JPATH | cut -d ':' -f5
/usr/sbin
linux-tl0r:~ # echo $JPATH | cut -d ':' -f 3,5 <顯示第3與第五欄>
/usr/lib/mpi/gcc/openmpi/bin:/usr/sbin

範例二 : 將 export 輸出的訊息, 取得第12字元以後的所有字串.
linux-tl0r:~ # export
declare -x COLORTERM="1"
declare -x CPU="i686"
declare -x CSHEDIT="emacs"
...(以下省略)...
linux-tl0r:~ # export | cut -c 12- <去除 'declare -x'>
COLORTERM="1"
CPU="i686"
CSHEDIT="emacs"
...(以下省略)...
# 我們還可以指定某個範圍的值, 例如第 12-20 的字元, 就是 cut -c 12-20

範例三 : 用 last 將顯示的登入者資訊中, 僅留下登入者大名.
linux-tl0r:~ # last
root pts/1 192.168.0.154 Tue Oct 5 10:19 still logged in
root pts/1 192.168.0.154 Mon Oct 4 18:08 - 18:09 (00:01)
root pts/1 192.168.0.154 Mon Oct 4 16:41 - 17:48 (01:07)
...(以下省略)...
linux-tl0r:~ # last | cut -d ' ' -f 1
root
root
root
...(以下省略)...
# 因為 'root' 與 'pts/1' 之間空格有好幾個, 並非僅有一個, 所以如果我們要找出 'pts/1'
# 其實不能以 cut -d ' ' -f 1,2 喔! 輸出結果將不是我們想要的.
This message was edited 5 times. Last update was at 05/10/2010 10:38:16

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...