每個(gè)開(kāi)發(fā)人員都要知道的8個(gè)Linux命令

2024-02-11    分類: 網(wǎng)站建設(shè)

每個(gè)開(kāi)發(fā)人員到了他們職業(yè)人生的某個(gè)階段的時(shí)候,將會(huì)發(fā)現(xiàn)自己要尋找有關(guān)Linux的信息。我并不是這方面的專家。但是掌握了以下8個(gè)命令,我?guī)缀蹩梢缘玫轿胰魏涡枰臇|西。

我們以一些文本舉例。假設(shè)我們有2個(gè)文件,里面有訂單關(guān)于第三方的放置地點(diǎn)和發(fā)送回應(yīng)。

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

–追加文件并在標(biāo)準(zhǔn)輸出上打印

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

正如他的名字所說(shuō)的,你可以串聯(lián)多個(gè)文件

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

–對(duì)文本文件進(jìn)行行排序,這里使用排序是不錯(cuò)的選擇

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

上面顯示了我們想要看到的效果,但是這只是小文件。而真實(shí)的數(shù)據(jù)是很大的,有些是你不想要的數(shù)據(jù)怎么辦?

grep

grep, egrep, fgrep–進(jìn)行匹配輸出

假設(shè)我只關(guān)心給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

假設(shè)訂單113里面發(fā)生了一些問(wèn)題,你想看到關(guān)于113的所有訂單信息。沒(méi)錯(cuò),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

你會(huì)發(fā)現(xiàn)在表達(dá)式里面不止有113,這是因?yàn)?13也可能出現(xiàn)在價(jià)格里面,或者產(chǎn)品里面,這樣做是嚴(yán)格限制其查找結(jié)果。

現(xiàn)在我們已經(jīng)發(fā)出退貨訂單的信息,我們每日也要給會(huì)計(jì)發(fā)送銷售統(tǒng)計(jì)。他們要求每個(gè)PofEAA的項(xiàng)目,但他們只關(guān)心數(shù)量和價(jià)格,我們要把

不需要的部分刪減掉。

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

我們已經(jīng)減少了數(shù)據(jù),讓會(huì)計(jì)一目了然。

假設(shè)會(huì)計(jì)想要把訂單ID做為參考,把它放在每一行的最后,并用單引號(hào)。

sed

–流編輯器。用來(lái)處理文本轉(zhuǎn)換。

下面的示例演示怎樣使用它來(lái)做到我們想要的數(shù)據(jù)。

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′

這是一個(gè)正則表達(dá)式,但沒(méi)什么復(fù)雜的。做以下事情

1.刪除時(shí)間

2.捕獲訂單號(hào)

3.刪除逗號(hào)和訂單號(hào)后面的空格

4.捕獲此行的其余部分

一旦我們看到了我們需要的數(shù)據(jù),可以使用\1&\2讓輸出數(shù)據(jù)符合我們的格式要求。

uniq

–去除重復(fù)行

下面的示例演示如何grep的唯一相關(guān)的交易,削減不必要的信息,并獲得計(jì)數(shù)。

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

–在目錄里找文件

假設(shè)這2個(gè)文本文件存在于我們的主目錄,我們不必知道他們的全名。

jfields$ find /Users -name “order*”

Users/jfields/order.in.log

Users/jfields/order.out.log

當(dāng)然還有很多選項(xiàng),但99%的情況下我這么做。

less

–在一個(gè)文件里面向前向后移動(dòng)

讓我們回到最簡(jiǎn)單的cat|sort的例子。你可以向前搜索使用”/”,向后使用”?”,2者都可以使用正則表達(dá)式。

jfields$ cat order* | sort | less

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

Linux命令很豐富,有些人很頭疼。這幾個(gè)命令應(yīng)該能幫你完成大部分的文本工作,不用交到你的腳本語(yǔ)言手里。

分享標(biāo)題:每個(gè)開(kāi)發(fā)人員都要知道的8個(gè)Linux命令
網(wǎng)站URL:http://www.bm7419.com/news12/317162.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站設(shè)計(jì)、云服務(wù)器、外貿(mào)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化