溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

sed的基礎(chǔ)用法是怎么樣的

發(fā)布時(shí)間:2021-11-08 16:23:29 來源:億速云 閱讀:127 作者:柒染 欄目:建站服務(wù)器

今天就跟大家聊聊有關(guān)sed的基礎(chǔ)用法是怎么樣的,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Sed包含多個(gè)功能,最常用的是替換命令

替換

s/pattern/replacement/flags

Flags包括如下

n1-512之間的數(shù)字,對(duì)第n次匹配進(jìn)行替換

g:對(duì)所有匹配情況進(jìn)行全局修改

p:打印模式所有內(nèi)容

wfile,將模式內(nèi)容寫入文件file

                                                                                                                                         

對(duì)于replacement,如下字符有特殊含義

&:將其替換為pattern

\n:匹配第n個(gè)字串,在pattern中用”\(“”\)”指定

\:轉(zhuǎn)義字符

 

&替換成pattern,可\&轉(zhuǎn)義

[oracle@ ~]$ cat test

ORA

[oracle@ ~]$ cat test | sed 's/ORA/O’ Reilly \& Associates, Inc./g'

O’ Reilly & Associates, Inc.

[oracle@ ~]$ cat test | sed 's/ORA/O’ Reilly & Associates, Inc./g'

O’ Reilly ORA Associates, Inc.

 

[oracle@ ~]$ cat test1

See Section 19.40  See Section 18.89

See Section 19.09

See Section 07.10

 [oracle@ ~]$ sed 's/See Section [1-9][0-9]*\.[1-9][0-9]*/(&)/2' test1 –為每行的第二個(gè)匹配字符包圍”()”

See Section 19.40  (See Section 18.89)

See Section 19.09

See Section 07.10

 

將每行第2個(gè)See替代為換行字符

[oracle@ ~]$ sed 's/See/\\n/2' test1  --不起作用

See Section 19.40  \n Section 18.89

See Section 19.09

See Section 07.10

[oracle@ ~]$ sed 's/See/\

> /2' test1  --成功換行,但不太好維護(hù)

See Section 19.40 

 Section 18.89

See Section 19.09

See Section 07.10

[oracle@ ~]$ sed 's/See/\n/2' test1  --使用\n替代

See Section 19.40 

 Section 18.89

See Section 19.09

See Section 07.10

 

搜索每行第2個(gè)匹配模式,并將其第1/2個(gè)元素互換位置

[oracle@ ~]$ sed 's/\(See Section\) \([1-9][0-9]\.[1-9][0-9]\)/\2 \1/2' test1

See Section 19.40  18.89 See Section

See Section 19.09

See Section 07.10

[oracle@ ~]$ sed '/0[1-9]\./ s/\(See Section\) \([0-9][0-9]*\.[1-9][0-9]\)/\2 \1/' test1 –先搜索包含”0[1-9].”的行,如果其滿足匹配模式,則將其第1/2個(gè)元素互換位置

See Section 19.40  See Section 18.89

See Section 19.09

07.10 See Section

 

[oracle@ ~]$ cat test1 | grep UNIX

UNIX

[oracle@ ~]$ sed 's/UNIX/\\s-1&\\s0/' test1  --使用&替代前面的UNIX

\s-1UNIX\s0

 [oracle@ ~]$ sed 's/UNIX/\s-1\&\\s0/' test1 –使用\&將其作為普通字符,\\s改為\s,而s不是特殊字符所以\s等同于s

s-1&\s0

 [oracle@ ~]$ sed 's/UNIX/s-1\&\\s0/' test1

s-1&\s0

 

忽略大小寫

[oracle@ ~]$ cat test5
who finger w last
[oracle@ ~]$ sed 's/FINger/www/gI' test5
who www w last

 

2 刪除(d)

如果行匹配則刪除該行

/^$/d –刪除空行

[oracle@ ~]$ cat test3

adf

 

sdf

[oracle@ ~]$ sed '/^$/d' test3  --刪除空行

adf

sdf

[oracle@ ~]$ sed '/./!d' test3 --刪除空行,!表示not,/./!即不帶有字符的行

adf

sdf

[oracle@ ~]$ sed '/./d' test3 – 刪除非空行

 

[oracle@ ~]$ sed '$d' test3  --刪除最后一行

adf

 

[oracle@ ~]$ sed '2d' test3 –刪除第2

adf

sdf

[oracle@ ~]$ sed '$!d' test3 –只保留最后一行數(shù)據(jù),其余全刪除

Sdf

 

3 追加(a)/插入(i)/更改(c)

格式如下

[line-address][a|i|c] text

 

注:<<sed and awk>>(O’Reilly)指出 “這些命令必須在多行上指定, (\用于轉(zhuǎn)義行尾)

[line-address][a|i|c]\

Text

但個(gè)人試驗(yàn)發(fā)現(xiàn)不需要這么麻煩

 

[oracle@ ~]$  cat test3

adf

 

sdf

在第1行和最后1行前后分別添加信息

[oracle@ ~]$ sed -e '1i insert before the first line' -e '$a append after the last line' test3

insert before the first line

adf

 

sdf

append after the last line

使用更改(c)命令有時(shí)比替換更高效,如下:

將所有以”.sp”開頭的行的輸入?yún)?shù)替換為5

[oracle@ ~]$ cat test4

.sp 1.5

.sp 4

.sp

5.sp 67

[oracle@ ~]$ sed '/^\.sp/c \.sp 5' test4  --搜索以.sp開頭的行,將整行替代為.sp 5

.sp 5

.sp 5

.sp 5

5.sp 67

 

4 列表(l)

顯示模式空間的內(nèi)容,將非打印字符顯示為兩個(gè)數(shù)字的ASCII代碼,類似vi(:l)或者cat -v;

可用于檢測(cè)不可見字符;

oracle@ ~]$ cat -v test5

who finger w last^M

^M

^M

[oracle@ ~]$ sed -e "l" test5

who finger w last\r$

who finger w last

\r$

 

\r$

 

[oracle@ ~]$ sed -n -e "l" test5

who finger w last\r$

\r$

\r$

其中-n選項(xiàng)作用如下:-n, --quiet, --silent suppress automatic printing of pattern space

 

 

5下一行(n)

輸出模式空間的內(nèi)容,然后讀取輸入的下一行,而不返回腳本的頂端;

[oracle@usuwsoadb06 ~]$ cat -n test7

     1  apple

     2  banana

     3  mango

     4  orange

     5  strawberry

[oracle@usuwsoadb06 ~]$ sed '/ban/{n;d;}' test7  --刪除匹配模式的下一行

apple

banana

orange

strawberry

 

--Nn的區(qū)別,前者會(huì)輸出當(dāng)前匹配行

[oracle@usuwsoadb06 ~]$ sed -n '/ban/ {n; p;}' test7

mango

[oracle@usuwsoadb06 ~]$ sed -n '/ban/ {N; p;}' test7

banana

mango

 

尋找包含ban字符的下一行,將其替換為justin

[oracle@usuwsoadb06 ~]$ sed '/ban/ {n; s/.*/justin/;}' test7

apple

banana

justin  --原本的mango現(xiàn)在變?yōu)閖ustin

orange

strawberry

 

 

6讀寫文件(r/w)

 

寫文件

從某個(gè)文件讀取符合條件的行,并將其寫入另外一個(gè)文件,語法如下:

Sed ‘/pattern/w outputfile’ inputfile

 

[oracle@ ~]$ cat -n test7

     1  apple

     2  banana

     3  mango

     4  orange

     5  strawberry

 [oracle@ ~]$ sed -n '2,4w test8' test7 –test72-4行輸入到test8

[oracle@ ~]$ cat test8

banana

mango

orange

[oracle@ ~]$ sed -n '/mango/,$w test8' test7 –test7從包含mango行直到最后一行的數(shù)據(jù)寫入test8

[oracle@ ~]$ cat test8

mango

orange

strawberry

[oracle@ ~]$ sed -n '$!w test8' test7 –除去最后一行將test7所有內(nèi)容寫入test8

[oracle@ ~]$ cat test8

apple

banana

mango

orange

 

讀文件 --并不會(huì)實(shí)際改變文件內(nèi)容

Sed ‘/pattern/r outputfile’ inputfile

--將文件內(nèi)容寫入sed output

[oracle@ ~]$ cat test9

1apple

1banana

1mango

[oracle@ ~]$ cat test10

2orange

2strawberry

 [oracle@ ~]$ sed '2,$r test10' test9 –test10文件內(nèi)容插入test9自第2行直至最后一行

1apple

1banana

2orange

2strawberry

1mango

2orange

2strawberry

[oracle@ ~]$ sed '$!r test10' test9—test10插入test9除最后1行的其余行之后

1apple

2orange

2strawberry

1banana

2orange

2strawberry

1mango

看完上述內(nèi)容,你們對(duì)sed的基礎(chǔ)用法是怎么樣的有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

sed
AI