《 Linux Shell 》筆記之sed

在日常工作中,sed是一個實用的文本文件處理工具。由於它是一個非交互式方式,它處理的文本文件包括:來自鍵盤輸入、文件重定向、字符串、變量、來自管道的文本等等。

sed主要是以行爲單位進行處理,可以將數據行進行替換、刪除、新增、選取等特定工作,下面先了解一下sed的用法

sed命令行格式爲:

         sed[-nefri] ‘command’輸入文本     

sed:全稱stream editor (流編輯器)

sed輸出文本文件的保存,有兩種方式:

  • 使用>符號,輸出到一個新文件 見例子:輸出到新文件
  • 在原始文件上修改且保存 見例子:直接修改原文件內容,替換指定行的所有內容

sed調用的三種方式:

  • 直接在shell命令行上調用見例子:顯示數據
  • sed命令插入腳本文件後,通過sed命令調用腳本見例子:通過sed命令調用腳本
  • sed命令寫入腳本文件後,將其+x變成可執行,需要在文件頭部加上 #!/bin/sed 見例子:將sed命令寫入腳本文件

常用選項:

        -n使用安靜(silent)模式。通常 p 會與參數 sed -n一起運作。(見例子:顯示數據

       -e直接在指令列模式上進行 sed的動作編輯。(見例子:多點編輯)

       -f直接將 sed的動作寫在一個檔案內, -f filename則可以執行 filename內的sed動作;見例子:通過sed命令調用腳本

       -rsed的動作支援的是延伸型正規表示法的語法。(見例子:sed -r 例子)

       -i直接修改讀取的檔案內容,而不是由螢幕輸出。(見例子:直接修改原文件內容,替換指定行的所有內容)       

常用命令:

        a  新增, a 的後面可以接字串,而這些字串會在新的一行出現(目前的下一行) (見例子:在第一行之後,插入tea

       c   取代, c的後面可以接字串,這些字串可以取代 n1,n2之間的行!(見例子:替換第一行

       d   刪除,因爲是刪除啊,所以 d後面通常不接任何咚咚;(見例子:刪除第一行

        i   插入, i的後面可以接字串,而這些字串會在新的一行出現(目前的上一行);(見例子:在第一行之前,插入tea

        p  列印,亦即將某個選擇的資料印出。通常 p會與參數 sed -n一起運作~ (見例子:顯示數據

        s  取代,可以直接進行取代的工作哩!通常這個 s的動作可以搭配正規表示法!格式: s/old/new/g,(見例子:替換一行中的某部分



高級command:

命令

功能描述

h

拷貝pattern space的內容到holding buffer(特殊緩衝區)。

H

追加pattern space的內容到holding buffer。

g

獲得holding buffer中的內容,並替代當前pattern space中的文本

G

獲得holding buffer中的內容,並追加到當前pattern space的後面

n

讀取下一個輸入行,用下一個命令處理新的行而不是用第一個命令。

p

打印pattern space中的第一行。  //大寫

q

退出sed。

w file

寫並追加pattern space到file的末尾。

!

表示後面的命令對所有沒有被選定的行發生作用。

s/re/string

用string替換正則表達式re。

=

打印當前行號碼

替換標記

 

g

行內全面替換,如果沒有g,只替換第一個匹配。

x

互換pattern space和holding buffer中的文本。

y

把一個字符翻譯爲另一個字符(但是不能用於正則表達式)。


舉例:(假設我們有一文件名爲SedTxt,SedTxt_2

SedTxt內容如下:

Hello
Welcometo @LinuxShell World
Goodbye Linux

SedTxt_2內容如下:

TABASENAME=Apple
DATABASEURL="jdbc:oracle:thin:@172.10.10.10:1521:11g2"

##### #####顯示數據###########
#
顯示第一行

test@sha:~/tmp> sed -n'1p' SedTxt    
Hello

#顯示最後一行

test@sha:~/tmp> sed -n'$p' SedTxt
Goodbye Linux

#顯示第一行到第二行

test@sha:~/tmp> sed -n'1,2p' SedTxt
Hello
Welcome to @LinuxShell World

##### #####模糊查詢顯示數據##########
格式:sed -n '/查詢的關鍵字/p'輸入文件
#
查詢包括關鍵字Linux所在所有行

test@sha:~/tmp> sed -n'/Linux/p' SedTxt
Welcome to @LinuxShell World
GoodbyeLinux

#查詢包括關鍵字$所在所有行,使用反斜線\屏蔽特殊含義

格式:sed -n '/關鍵字/p'輸入文件

test@sha:~/tmp> sed -n'/\@/p' SedTxt
Welcome to @LinuxShell World

##### #####新增數據##########
格式:sed '[行位置]a'輸入文件
#在第一行之後,插入tea

test@sha:~/tmp> sed '1a tea' SedTxt
Hello
tea
Welcome to @LinuxShell World
GoodbyeLinux

#在第一行和第二行之後,插入tea

方法1:

test@sha:~/tmp>sed '1,2a tea' SedTxt
Hello
tea
Welcometo @Linux Shell World
insert
GoodbyeLinux

#在第一行之後,插入多行(如tea和or orange)

test@sha:~/tmp>sed '1a tea\nor orange' SedTxt
Hello
tea
or orange
Welcometo @Linux Shell World
GoodbyeLinux

#在第一行之前,插入tea

test@sha:~/tmp> sed '1i tea' SedTxt
tea
Hello
Welcome to @Linux Shell World
GoodbyeLinux

#在行頭和行尾,插入head,tail

test@sha:~/tmp> sed's/^/head&/g' SedTxt
headHello
headWelcome to @LinuxShell World
headGoodbye Linux

test@sha:~/tmp> sed 's/$/&tail/g'SedTxt
Hellotail
Welcome to @LinuxShell Worldtail
Goodbye Linuxtail

#第二行的行頭和行尾,插入head,tail

test@sha:~/tmp> sed '2s/^/head&/g' SedTxt
Hello
headWelcome to @LinuxShell World
Goodbye Linux

test@sha:~/tmp> sed '2s/$/&tail/g'SedTxt
Hello
Welcome to @LinuxShell Worldtail
Goodbye Linux

Note:
^ 表示行頭,$表示行尾
文本替換格式:sed 's/要替換的字符串/新的字符串/g'  (要替換的字符串可以用正則表達式)/g表示每行出現的全部替換

#在所有的行頭和行尾,插入headtail

test@sha:~/tmp> sed '/./{s/^/HEAD&/;s/$/&TAIL/}' SedTxt
HEADHelloTAIL
HEADWelcome to @LinuxShell WorldTAIL
HEADGoodbye LinuxTAIL

##### #####替換數據###### #####

#替換第一行

test@sha:~/tmp>sed '1c Good Morning' SedTxt
GoodMorning
Welcometo @Linux Shell World
GoodbyeLinux

#替換第一行到第二行

test@sha:~/tmp>sed '1,2c Good Morning' SedTxt
GoodMorning
GoodbyeLinux

#替換一行中的某部分
格式:
sed 's/要替換的字符串/新的字符串/g'  (要替換的字符串可以用正則表達式)


#替換Linux爲Python,輸出到其它文件 (只要加上 ><filename>)

test@sha:~/tmp>sed -n '/Linux/p' SedTxt |sed 's/Linux/Python/g' SedTxt> output
Hello
Welcometo @Python Shell World
GoodbyePython

#使用替換,刪除所在行的Linux

test@sha:~/tmp>sed -n '/Linux/p' SedTxt |sed 's/Linux//g' SedTxt
Hello
Welcometo @ Shell World
Goodbye

#搜索Linux,找到welcome對應的行,並且把Linux替換爲redshell
執行後面花括號中的一組命令,每個命令之間用分號分隔
p打印,q退出
格式:
sed /搜索關鍵字/p文件名| sed -n'/對應關鍵字/{s/要替換字符/新的字符/;p;q}'

test@sha:~/tmp> sed/Linux/p SedTxt |sed -n '/Welcome/{s/Linux/redshell/;p;q}'
Welcome to @redshellShell World

#直接修改原文件內容,替換指定行的所有內容
格式:sed 's/要替換的字符串/新的字符串/g'  (要替換的字符串可以用正則表達式)/g表示每行出現的全部替換

替換前(SedTxt_2)

DATABASENAME=apple
jdbc:oracle:thin:@172.10.10.10:1521:11g2

test@sha:~/tmp>sed -i '1 s/[^ ]*$/DATABASENAME=orange/g' SedTxt_2
test@sha:~/tmp>sed -i '2 s/[^ ]*$/jdbc:oracle:thin:@172.20.20.20:1521:11g2/g' SedTxt_2

替換後(SedTxt_2)

DATABASENAME=orange
jdbc:oracle:thin:@172.20.20.20:1521:11g2

Note: [^ ]*$:除空格之外的所有內容

[^] -匹配未包含空格的任意字符,$ -匹配輸入字符串的結束位置“\n”或“\r”,* -  匹配結束位置之前的所有字符

##### #####刪除數據###### #####

#刪除第一行

test@sha:~/tmp>sed '1d' SedTxt
Welcometo @Linux Shell World
GoodbyeLinux

#刪除最後一行

test@sha:~/tmp>sed '$d' SedTxt
Hello
Welcometo @Linux Shell World

#搜索linux,刪除所在行

test@sha-colline-node-h:~/tmp>sed '/Linux/d' SedTxt
Hello

##### ##### 多點編輯##### ######

#刪除第一行,並將Linux替換爲RedShell

test@sha-colline-node-h:~/tmp>sed -e '1d' -e 's/Linux/RedShell/g' SedTxt
Welcometo @RedShellShell World
GoodbyeRedShell

##### #####通過sed命令調用腳本##########

# file1.sed裏的內容

test@sha:~/tmp> cat file1.sed
s/this/it/
s/test/sedtest/

#file2.sed裏的內容

test@sha:~/tmp>cat file2.sed
this is atest

#通過sed命令調用腳本

test@sha:~/tmp>sed -f file1.sed file2.sed
it is ased test

##### ##### sed -r例子##### #####

如果不使用-r,需要在正則表達式中需要使用轉移符\, 如果使用-r,就可以不使用轉移符\

#使用轉移符\

Test@sha:~/tmp>echo "aaa bbb "|sed 's/\(.\)/A/'
Aaa bbb

#不使用轉移符\

test@sha:~/tmp>echo "aaa bbb "|sed  's/(.)/A/'
aaa bbb

#使用-r,代替不使用轉義符

test@sha:~/tmp>echo "aaa bbb "|sed -r 's/(.)/A/'
Aaa bbb 

##### #####sed命令寫入腳本文件後,將其+x變成可執行##### #####

sed命令寫入腳本文件input.sed如下:

#!/bin/bash
#!/bin/sed
echo "aaabbb" |sed -n '1p'

##### ####輸出到新文件##### #####

test@sha:~/tmp>echo "aaa bbb"| sed -n '1p' >output.sed
test@sha:~/tmp>cat output.sed
aaa bbb

test@sha-colline-node-h:~/tmp>chmod+x input.sed
test@sha-colline-node-h:~/tmp>./input.sed
aaa bbb

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章