sed基礎用法小結

531968912發表於2017-12-20

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

 

替換

s/pattern/replacement/flags

Flags包括如下

n1-512之間的數字,對第n次匹配進行替換

g:對所有匹配情況進行全域性修改

p:列印模式所有內容

wfile,將模式內容寫入檔案file

                                                                                                                                         

對於replacement,如下字元有特殊含義

&:將其替換為pattern

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

\:跳脫字元

 

&替換成pattern,可\&轉義

[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 –為每行的第二個匹配字元包圍”()”

See Section 19.40  (See Section 18.89)

See Section 19.09

See Section 07.10

 

將每行第2See替代為換行字元

[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  --成功換行,但不太好維護

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個匹配模式,並將其第1/2個元素互換位置

[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個元素互換位置

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-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 –只保留最後一行資料,其餘全刪除

Sdf

 

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

格式如下

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

 

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

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

Text

但個人試驗發現不需要這麼麻煩

 

[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)命令有時比替換更高效,如下:

將所有以”.sp”開頭的行的輸入引數替換為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)

顯示模式空間的內容,將非列印字元顯示為兩個數字的ASCII程式碼,類似vi(:l)或者cat -v

可用於檢測不可見字元;

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選項作用如下:-n, --quiet, --silent suppress automatic printing of pattern space

 

 

5下一行(n)

輸出模式空間的內容,然後讀取輸入的下一行,而不返回指令碼的頂端;

[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的區別,前者會輸出當前匹配行

[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現在變為justin

orange

strawberry

 

 

6讀寫檔案(r/w)

 

寫檔案

從某個檔案讀取符合條件的行,並將其寫入另外一個檔案,語法如下:

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行直到最後一行的資料寫入test8

[oracle@ ~]$ cat test8

mango

orange

strawberry

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

[oracle@ ~]$ cat test8

apple

banana

mango

orange

 

讀檔案 --並不會實際改變檔案內容

Sed ‘/pattern/r outputfile’ inputfile

--將檔案內容寫入sed output

[oracle@ ~]$ cat test9

1apple

1banana

1mango

[oracle@ ~]$ cat test10

2orange

2strawberry

 [oracle@ ~]$ sed '2,$r test10' test9 –test10檔案內容插入test9自第2行直至最後一行

1apple

1banana

2orange

2strawberry

1mango

2orange

2strawberry

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

1apple

2orange

2strawberry

1banana

2orange

2strawberry

1mango

 

 

 

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/25462274/viewspace-2148954/,如需轉載,請註明出處,否則將追究法律責任。

相關文章