Linux sed命令使用

來源:互聯網
上載者:User

標籤:linux sed命令使用

sed基本用法:

sed:stream Editor 行編輯器

sed:模式空間

預設不編輯源檔案,僅對模式空間中的資料做處理,處理結束後,將模式空間內容列印出來

sed  [options]‘AddressCommand‘ file ...

是對file中的檔案中的符合Address指定位址範圍內的行執行Command編輯命令

     options:

     -n:靜默模式

     不顯示模式空間中的內容,僅顯示被匹配的內容

     -i:直接修改原檔案

     -e SCRIPT(指令碼) -e SCRIPT:可以同時執行多個指令碼 每個-e就可以執行一個 指令碼

     -f /path/to/sed_srcipt:將-e的指令碼儲存在一個檔案中,通過-f讀取此檔案執行

     sed -f /path/to/sed_srcipt

     -r:表示使用擴充的Regex 

Address方式:

1. StartLine,EndLine

格式例如:1,100,從第1行到第100行

$:表示最後一行

2./Pattern(Regex)/

例如:/^root/  表示以root開頭的行

3. /pattern1/,/pattern2/

表示:第1次被pattern1匹配到的行開始,第1次被pattern2匹配到的行結束,這中間的所有行

4.LineNumber

指定的行

5.StartLine,+N

表示:從指定的StartLine行開始,向後的N行


Command:

d:刪除合格行

   $ sed ‘2d‘ example-----刪除example檔案的第二行。  

*  

$ sed ‘2,$d‘ example-----刪除example檔案的第二行到末尾所有行。  

*  

$ sed ‘$d‘ example-----刪除example檔案的最後一行。  

*  

$ sed ‘/test/‘d example-----刪除example檔案所有包含test的行。

例如:

[[email protected] data]# sed "1,2d" /etc/issue   ---> 表示刪除第1和第2行 

Mage Education Learning Services

http://www.magedu.com


[[email protected] data]# cat /etc/issue

CentOS release 6.6 (Final)

Kernel \r on an \m


Mage Education Learning Services

http://www.magedu.com

[[email protected] data]# sed "1d" /etc/issue    --->表示刪除第1行

[[email protected] data]# sed "1,+3d" /etc/issue -->表示刪除第1行以及往後的3行,共4行

[[email protected] data]# sed "/^\//d" /etc/issue --->表示刪除以/開頭的行,模式必須要//夾起來


p:顯示合格行

例如:

[[email protected] data]# cat ./test 

he like her

she is a good boy

what are you doing

[[email protected] data]# sed /he/p ./test  --->將行中包含he的行顯示出來

he like her

he like her---->從執行結果看,符合模式比對的行顯示了2次,原因是sed命令的預設意思

she is a good boy將模式空間中的命令列處理過後還要顯示出來,顯示的那一行在模式空間中

she is a good boy還存在,sed命令模式是顯示模式空間,因此能匹配到的顯示2次,如果不想

what are you doing          顯示模式空間中的,可以是用-n,顯示靜默模式,不顯示模式空間的

[[email protected] data]# sed -n /he/p ./test   --->靜默模式,只顯示合格行

he like her

she is a good boy

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

   例如:

   [email protected] ruby]# cat ab

    Hello!

    ruby is me,welcome to my blog.

    end

   [[email protected] data]# sed  ‘1,2c hi‘ ./linux 

hi

end

a \string:在指定的行後面追加新行,內容為string

例如:

[[email protected] data]# sed "/he/a \welcom to linux world" ./test -->在行中有he的行後追加一行

he like her

welcom to linux world

she is a good boy

welcom to linux world

what are you doing 

[[email protected] data]# sed "/he/a \welcom to linux world\nthe two line" ./test --->追加兩行,

在字元間加"\n"表示換行


i \string:在指定的行前面追加新行,內容為string

例如:

[[email protected] data]# sed "/he/i \welcom to linux world" ./test 

welcom to linux world

he like her

welcom to linux world

she is a good boy

what are you doing

r file(檔案路徑):將指定的檔案的內容,添加至合格行處

例如:

[[email protected] data]# sed ‘2r ./first.sh‘ ./test  --->此處必須要用單引號,雙引號就出錯

he like her

she is a good boy

#!/bin/bash

U=`wc -l  /etc/rc.d/rc.sysinit |cut -d‘ ‘ -f1`

G=`wc -l /etc/rc.d/init.d/functions |cut -d‘ ‘ -f1` 

sum=$[$U+$G]

echo $sum

what are you doing 

[[email protected] data]# sed ‘1,2r ./first.sh‘ ./test --->在第1行和第2行後都添加


w file(檔案路徑):將指定範圍內的內容另存在指定的檔案中

例如:

[[email protected] data]# sed ‘/he/w ./xx.txt‘ ./test -->將./test檔案中合格行儲存至./xx.txt

he like her檔案中,事先xx.txt可以不存在,如果多次執行

she is a good boy儲存在xx.txt中是覆蓋追加的,此處還顯示在螢幕

what are you doing 上是因為,工作在模式空間而不是靜默模式

[[email protected] data]# cat ./xx.txt 

he like her

she is a good boy

s/pattern/string/修飾符:將每行中符合pattern模式的行替換為string,預設只替換每行中第一次被模式比對到的字串

或[email protected]@@修飾符或s###修飾符都行,string是字串,不能是Regex 

如果要全部替換需要加修飾符

g:全域替換

i:尋找時忽略大小寫

&:參考模式匹配整個串

例如:

[[email protected] data]# sed ‘s/o/T/‘ ./test --->將尋找的每行中第1個"o"的替換為T

he like her

she is a gTod boy

what are yTu doing 

[[email protected] data]# sed ‘s/o/@/g‘ ./test -->加上g修飾符,則全部替換

he like her

she is a [email protected]@d [email protected]

what are [email protected] [email protected] 

[[email protected] data]# sed ‘[email protected]\(l..e\)@\[email protected]‘ ./test 

he liker her

she is a good boy

what are you doing

[[email protected] data]# sed ‘[email protected]@&[email protected]‘ ./test --->此處@代表之前的整個模式l..e,\1和&在不同的地方有不同的方便

he liker her

she is a good boy

what are you doing


本文出自 “小小色痞子” 部落格,請務必保留此出處http://501steve.blog.51cto.com/9608615/1577200

Linux sed命令使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.