每一個開發人員到了他們職業人生的某個階段的時候,將會發現本身要尋找有關Linux的信息。我並非這方面的專家。可是掌握瞭如下8個Linux命令,我幾乎能夠獲得我任何須要的東西。html
注意:如下的命令都有不少擴展的文檔,博客裏提出的知識我最經常使用的命令,用法。若是你不瞭解Linux命令,這個帖子會給你一點指導。linux
咱們以一些文本舉例。假設咱們有2個文件,裏面有訂單關於第三方的放置地點和發送迴應。正則表達式
order.out.log
8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99
8:23:45 112, 1, Joy of Clojure, Hardcover, 29.99
8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99less
order.in.log
8:22:20 111, Order Complete
8:23:50 112, Order sent to fulfillment
8:24:20 113, Refund sent to processing編輯器
cat視頻
–追加文件並在標準輸出上打印htm
jfields$ cat order.out.log
8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99
8:23:45 112, 1, Joy of Clojure, Hardcover, 29.99
8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99blog
正如他的名字所說的,你能夠串聯多個文件排序
jfields$ cat order.*
8:22:20 111, Order Complete
8:23:50 112, Order sent to fulfillment
8:24:20 113, Refund sent to processing
8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99
8:23:45 112, 1, Joy of Clojure, Hardcover, 29.99
8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99教程
看到效果了,但咱們能夠提升其可讀性。
sort
–對文本文件進行行排序,這裏使用排序是不錯的選擇
jfields$ cat order.* | sort
8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99
8:22:20 111, Order Complete
8:23:45 112, 1, Joy of Clojure, Hardcover, 29.99
8:23:50 112, Order sent to fulfillment
8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99
8:24:20 113, Refund sent to processing
上面顯示了咱們想要看到的效果,可是這只是小文件。而真實的數據是很大的,有些是你不想要的數據怎麼辦?
grep
grep, egrep, fgrep–進行匹配輸出
假設我只關心給PofEAA的訂單,使用grep就能夠作到。
jfields$ cat order.* | sort | grep Patterns
8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99
8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99
假設訂單113裏面發生了一些問題,你想看到關於113的全部訂單信息。沒錯,grep能幫你。
jfields$ cat order.* | sort | grep 「:\d\d 113, 」
8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99
8:24:20 113, Refund sent to processing
你會發如今表達式裏面不止有113,這是由於113也可能出如今價格裏面,或者產品裏面,這樣作是嚴格限制其查找結果。
如今咱們已經發出退貨訂單的信息,咱們每日也要給會計發送銷售統計。他們要求每一個PofEAA的項目,但他們只關心數量和價格,咱們要把不須要的部分刪減掉。
cut
–從文件的每一行刪除一部分
仍是要先使用grep。
jfields$ cat order.* | sort | grep Patterns
8:22:19 111, 1, Patterns of Enterprise Architecture, Kindle edition, 39.99
8:24:19 113, -1, Patterns of Enterprise Architecture, Kindle edition, 39.99
jfields$ cat order.* | sort | grep Patterns | cut -d」,」 -f2,5
1, 39.99
-1, 39.99
咱們已經減小了數據,讓會計一目瞭然。
假設會計想要把訂單ID作爲參考,把它放在每一行的最後,並用單引號。
sed
–流編輯器。用來處理文本轉換。
下面的示例演示怎樣使用它來作到咱們想要的數據。
jfields$ cat order.* | sort | grep Patterns \
>| sed s/」[0-9\:]* \([0-9]*\)\, \(.*\)」/」\2, ‘\1′」/
1, Patterns of Enterprise Architecture, Kindle edition, 39.99, ’111′
-1, Patterns of Enterprise Architecture, Kindle edition, 39.99, ’113′
lmp-jfields01:~ jfields$ cat order.* | sort | grep Patterns \
>| sed s/」[0-9\:]* \([0-9]*\)\, \(.*\)」/」\2, ‘\1′」/ | cut -d」,」 -f1,4,5
1, 39.99, ’111′
-1, 39.99, ’113′
這是一個正則表達式,但沒什麼複雜的。
作如下事情
1.刪除時間
2.捕獲訂單號
3.刪除逗號和訂單號後面的空格
4.捕獲此行的其他部分
一旦咱們看到了咱們須要的數據,可使用\1&\2讓輸出數據符合咱們的格式要求。
uniq
–去除重複行
下面的示例演示如何grep的惟一相關的交易,削減沒必要要的信息,並得到計數。
jfields$ cat order.out.log | grep 「\(Kindle\|Hardcover\)」 | cut -d」,」 -f3 | sort | uniq -c
1 Joy of Clojure
2 Patterns of Enterprise Architecture
jfields$ cat order.out.log | grep 「\(Kindle\|Hardcover\)」 | cut -d」,」 -f3 | sort | uniq
Joy of Clojure
Patterns of Enterprise Architecture
find
–在目錄裏找文件
假設這2個文本文件存在於咱們的主目錄,咱們沒必要知道他們的全名。
jfields$ find /Users -name 「order*」
Users/jfields/order.in.log
Users/jfields/order.out.log
固然還有不少選項,但99%的狀況下我這麼作。
less
–在一個文件裏面向前向後移動
讓咱們回到最簡單的cat|sort的例子。你能夠向前搜索使用」/」,向後使用」?」,2者均可以使用正則表達式。
jfields$ cat order* | sort | less
你能夠試試/113.*,這將突出顯示訂單113。你可使用?.*112,也將突出顯示訂單112,你能夠用’q'退出。
Linux命令很豐富,有些人很頭疼。這幾個命令應該能幫你完成大部分的文本工做,不用交到你的腳本語言手裏。