每個開發人員都應該知道的8個常用Linux命令

來源:互聯網
上載者:User

  每個開發人員到了他們職業人生的某個階段的時候,將會發現自己要尋找有關Linux的資訊。我並不是這方面的專家。但是掌握了以下8個命令,我幾乎可以得到我任何需要的東西。

  注意:以下的命令都有很多擴充的文檔,部落格裡提出的知識我最常用的命令,用法。如果你不瞭解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.99

  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

  –追加檔案並在標準輸出上列印

  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.99

  正如他的名字所說的,你可以串聯多個檔案

  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 ":dd 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'

  這是一個Regex,但沒什麼複雜的。做以下事情

  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者都可以使用Regex。

  jfields$ cat order* | sort | less

  你可以試試/113.*,這將反白訂單113。你可以使用?.*112,也將反白訂單112,你可以用’q‘退出。

  Linux命令很豐富,有些人很頭疼。這幾個命令應該能幫你完成大部分的文本工作,不用交到你的指令碼語言手裡。

  原文:jayfields 編譯:大愛資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.