欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何理解Linuxsed命令

本篇文章給大家分享的是有關(guān)如何理解Linux sed命令,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

創(chuàng)新互聯(lián)公司公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站設(shè)計、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元寶安做網(wǎng)站,已為上家服務(wù),為寶安各地企業(yè)和個人服務(wù),聯(lián)系電話:18982081108

Linux sed 命令詳解

Linux sed 命令是利用腳本處理文本文件。

sed 可按照腳本的指令來處理、編輯文本文件。

sed 主要用于自動編輯一個或多個文件、簡化對文件的反復(fù)操作、編寫轉(zhuǎn)換程序等。

語法:

sed [-hnV][-e<script>][-f<script文件>][文本文件]

參數(shù)說明:

-e <script>或--expression=<script> 以選項(xiàng)中指定的script來處理輸入的文本文件。

-f <script文件>或--file=<script文件> 以選項(xiàng)中指定的script文件來處理輸入的文本文件。

-h 或--help 顯示幫助。

-n 或--quiet或--silent 僅顯示script處理后的結(jié)果。

-V 或--version 顯示版本信息。

動作說明:

a :新增, a 的后面可以接字串,而這些字串會在新的一行出現(xiàn)(目前的下一行)~

c :取代, c 的后面可以接字串,這些字串可以取代 n1,n2 之間的行!

d :刪除,因?yàn)槭莿h除啊,所以 d 后面通常不接任何咚咚;

i :插入, i 的后面可以接字串,而這些字串會在新的一行出現(xiàn)(目前的上一行);

p :打印,亦即將某個選擇的數(shù)據(jù)印出。通常 p 會與參數(shù) sed -n 一起運(yùn)行~

s :取代,可以直接進(jìn)行取代的工作哩!通常這個 s 的動作可以搭配正規(guī)表示法!例如 1,20s/old/new/g 就是啦!

元字符集:

^ 指定行的開始

$ 指定行的結(jié)尾

. 匹配一個非換行符的字符

* 匹配零個或多個字符

[] 匹配指定字符內(nèi)的任一字符

[^] 匹配不在指定字符內(nèi)的任一字符

.. 保存已匹配的字符

&  s/super/YY&yy/   super變成YYsuperyy   & 保存搜索字符用來替換其他字符

\< 詞首定位符 /\<my/  匹配包含以my開頭的單詞的行

\> 詞尾定位符 /my\>/  匹配包含以my結(jié)尾的單詞的行

x\{m\} 連續(xù)m個x /9\{5\}/ 匹配包含連續(xù)5個9的行

x\{m,\} 至少m個x /9\{5,\}/  匹配包含至少連續(xù)5個9的行

x\{m,n\} 至少m個,但不超過n個x /9\{5,7\}/  匹配包含連續(xù)5到7個9的行

實(shí)例:

在testfile文件第四行后添加一行,并將結(jié)果輸出到標(biāo)準(zhǔn)輸出,在命令行提示符輸入如下命令:

sed -e 4a\hellowolrd testfile

查看testfile 中的內(nèi)容:

[root@127-0-0-1 yoon]# cat testfile.txt

HELLO LINUX!

Linux is a free unix-type opterating system.

This is a linux testfile!

good good study!

Linux test

使用sed命令后,輸出結(jié)果如下:

[root@127-0-0-1 yoon]# sed -e 4a\helloworld testfile.txt

HELLO LINUX!

Linux is a free unix-type opterating system.

This is a linux testfile!

good good study!

helloworld

Linux test

[root@127-0-0-1 yoon]# sed -e '4a\hello world' testfile.txt

HELLO LINUX!

Linux is a free unix-type opterating system.

This is a linux testfile!

good good study!

hello world

Linux test

以行為單位的新增和刪除

刪除

將testfile的內(nèi)容列出并且打印行號,同時刪除第4-5行刪除

[root@127-0-0-1 yoon]# nl testfile.txt

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

[root@127-0-0-1 yoon]# nl testfile.txt | sed '4,5d'

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     6Linux test

sed的動作為'4,5d',d是刪除,因?yàn)?-5行刪除了,所以顯示的數(shù)據(jù)沒有4-5行,另外,原本應(yīng)該要下達(dá) sed -e 才對,沒有 -e 也可以。同事注意,sed 后面接的動作,務(wù)必以 '' 兩個單引號擴(kuò)住。

只刪除第二行:

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2d'

     1HELLO LINUX!

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

刪除第三行到最后一行:

[root@127-0-0-1 yoon]# nl testfile.txt | sed '3,$d'    ($代表最后一行)

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

新增

在第二行后面加上drink milk 字樣(亦即是加在第三行):

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2a drink milk'

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

drink milk

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

如果加入到第二行前:

[root@127-0-0-1 yoon]# nl testfile.txt | sed  '2i drink milk'

     1HELLO LINUX!

drink milk

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

如果是增加兩行以上,在第二行后面加入兩行字,例如,drink milk,eat pig :

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2a drink milk\  (每一行之間都必須要以反斜杠『 \ 』來進(jìn)行新行的添加)

> eat pig'

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

drink milk

eat pig

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2a drink milk\neat pig'  (\n換行符)

     1HELLO LINUX.

     2This is a linux testfile.

drink milk

eat pig

     3good good study.

     4hello world.

     5Linux test.

     6#This is good !

追加一行的話,不需要換行符\n,只有追加多行的情況下才需要換行符,最后一行也無需添加換行符,添加的話會多出一個空格

在第四行添加一行:

[root@127-0-0-1 yoon]# sed -e '4a new world' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

new world

Linux test.

#This is good !

在第四行追加兩行:

[root@127-0-0-1 yoon]# sed -e '4a new world\nold world' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

new world

old world

Linux test.

#This is good !

在第四行追加三行(兩行文字和一行空格):

[root@127-0-0-1 yoon]# sed -e '4a new world\nold world\n' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

new world

old world

Linux test.

#This is good !

添加一個空行:

[root@127-0-0-1 yoon]# sed -e '4a \\' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

Linux test.

#This is good !

添加兩個空行:

[root@127-0-0-1 yoon]# sed -e '4a \\n' testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

Linux test.

#This is good !

以行為單位的替換和顯示

將第3-4行替換為 No 2-5 number

[root@127-0-0-1 yoon]# nl testfile.txt | sed '2,5c No 2-5 number' (通過這個方法可以取代整行)

     1HELLO LINUX!

No 2-5 number

     6Linux test

僅列出第2-3行:

[root@127-0-0-1 yoon]# nl testfile.txt | sed -n '2,3p'

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

 數(shù)據(jù)的搜尋并顯示

 搜索 linux 關(guān)鍵字的行:

 [root@127-0-0-1 yoon]# nl testfile.txt | sed '/linux/p'    (如果linux找到,除了輸出所有行,還會匹配輸出行)

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

使用 -n 的時候只輸出包含模板的行

[root@127-0-0-1 yoon]# nl testfile.txt | sed -n '/linux/p'

     3This is a linux testfile!

數(shù)據(jù)的搜尋并刪除

刪除testfile中包含linux的行,其他行輸出:

[root@127-0-0-1 yoon]# nl testfile.txt | sed '/linux/d'

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     4good good study!

     5hello world

     6Linux test

 數(shù)據(jù)的搜尋并執(zhí)行命令

 搜索 linux 對應(yīng)的行,執(zhí)行花括號中的命令,每個命令用分號分隔,并把 testfile 替換成 newtestfile,再輸出這行:

 [root@127-0-0-1 yoon]# nl testfile.txt | sed -n '/linux/{s/testfile/newtestfile/;p;q}'   (最后的 q 是退出)

     3This is a linux newtestfile!

數(shù)據(jù)的搜尋并替換

除了整行的處理模式之外,sed 還可以用行為單位進(jìn)行部分?jǐn)?shù)據(jù)的搜尋并替換,基本上 sed 的搜尋與替代的 vi 相當(dāng)?shù)念愃疲悬c(diǎn)像這樣:

sed 's/要被取代的字符串/新的字符串/g'

查看 newtestfile 文件內(nèi)容:

[root@127-0-0-1 yoon]# cat newtestfile.txt

eth0 Link encap:Ethernet HWaddr 00:90:CC:A6:34:84

inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

inet6 addr: fe80::290:ccff:fea6:3484/64 Scope:Link

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

本機(jī)的ip是192.168.1.100,將 IP 前面的部分予以刪除:

[root@127-0-0-1 yoon]# cat newtestfile.txt | grep 'inet addr:' | sed 's/^.*addr://g'

192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0

接下來則是刪除后續(xù)的部分,亦即: 192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0,將 IP 后面的部分予以刪除:

[root@127-0-0-1 yoon]# cat newtestfile.txt | grep 'inet addr:' | sed 's/^.*addr://g' | sed 's/Bcast.*$//g'

192.168.1.100

多點(diǎn)編輯  sed -e 'cmd' -e 'cmd' filename

一條 sed 命令,將第三行到末尾的數(shù)據(jù)刪除,并將opterating替換成 newopterating:

[root@127-0-0-1 yoon]# nl testfile.txt

     1HELLO LINUX!

     2Linux is a free unix-type opterating system.

     3This is a linux testfile!

     4good good study!

     5hello world

     6Linux test

多條sed:

[root@127-0-0-1 yoon]# nl testfile.txt | sed '3,$d' | sed 's/opterating/newopterating/'

     1HELLO LINUX!

     2Linux is a free unix-type newopterating system.

 一條sed:

 [root@127-0-0-1 yoon]# nl testfile.txt | sed -e '3,$d' -e  's/opterating/newopterating/'

     1HELLO LINUX!

     2Linux is a free unix-type newopterating system.

( -e 表示多點(diǎn)編輯,第一個 -e 編輯第三行到最后一行刪除,第二個 -e 搜索 opterating替換成 newopterating)

直接修改文件內(nèi)容

將 testfile 文件中的每一行結(jié)尾為 . 修改為 !:

[root@127-0-0-1 yoon]# cat testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

Linux test.

[root@127-0-0-1 yoon]# sed 's/\.$/\!/g' testfile.txt

HELLO LINUX!

This is a linux testfile!

good good study!

hello world!

Linux test!

利用 sed 直接在最后一行輸入 #This is good !

[root@127-0-0-1 yoon]# sed -i '$a #This is good !' testfile.txt

[root@127-0-0-1 yoon]# cat testfile.txt

HELLO LINUX.

This is a linux testfile.

good good study.

hello world.

Linux test.

以上就是如何理解Linux sed命令,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:如何理解Linuxsed命令
網(wǎng)站鏈接:http://chinadenli.net/article30/gidiso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、關(guān)鍵詞優(yōu)化Google移動網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)自適應(yīng)網(wǎng)站

廣告

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

h5響應(yīng)式網(wǎng)站建設(shè)