歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> Linux命令 >> Linux命令之sed命令使用介紹

Linux命令之sed命令使用介紹

日期:2017/2/28 9:56:39   编辑:Linux命令
Linux命令

sed是linux輕量級流編輯器,用於行的操作,主要用來數據的選取、替換、刪除、新增等。從輸入文件中一次一行的讀取,

按順序將列表中的命令應用到每一行,並將其編輯過的行,寫到標准輸出。

權 限:所有用戶

位 置:/bin/sed

用 法:sed [-nefr] [動作] file

參數:

-n

不加-n的時候,sed處理的數據和不處理的都會輸出,加上-n只會輸出sed操作的行,

注意:與動作p和起來用,不要與其他動作一起用

-e

直接在命令行模式上進行sed的動作編輯

例如:sed -e 's/aaa/b/g; s/aa/b/g' file 也即多個動作同時操作,注意中間的分號

-f

直接將sed動作寫在一個文件內,-f file 則可以執行filename內的sed動作

-r

sed的動作支持擴展的正則表達式(默認支持基礎正則表達式)

-i

直接修改讀取的文件內容

注意:多個選項參數一起用的時候,例如:sed -i -r ... 不可以這樣:sed -ir ...

動作說明:[n1[,n2]] function

不見得會存在,一般代表選擇動作的行數,如果動作是在5到15行之間進行替換,則"10,20c"

a 新增,當前行的下一行

sed '2a hello world' file 輸出在第2行之後插入hello world


sed '2a hello world\

my name is csdn' file

輸出在2行下面插入兩行分別為hello world 和my name is scdn

特別注意hello world 後面的"\"

c 替換,是整行替換

sed '3,6c hello csdn' file

輸出把3至6行的只替換為 hello csdn(第3至6行只剩下一行hello csdn,不是3至6行的每一行都為hello csdn)

d 刪除

sed '3,6d' file 輸出刪除file的3至6行之後的內容

i 插入,當前行的上一行

與a動作類似,參照a動作用法

p 打印

sed -n '2,5p' file 打印file的第2至5行

s 替換

sed 's/oldString/newString/g' file oldString和newString支持基礎正則表達式和擴展的正則表達式

cat -n nginx | sed '1,99s/[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}\.[0-9]\{1,\}//g' 把nginx日志的1至99行的第一個字段IP地址替換為空

結果為:

    91	 - - [15/Dec/2014:23:00:10 +0800],GET /identified?key=this-is-TMP-apikey&digest=914EBB2FD551844CBE15513C0126E6EE&digest-algorithm=ed2k&hash=9D37BB2988F26973371FAB028FEEEB93D5542300& HTTP/1.1,200 65,-,XMP Windows Client 4.9,-,ed2k://|file|ki141213.wmv|1119747351|914EBB2FD551844CBE15513C0126E6EE|h,00E04C377725NG4E,ki141213.wmv,1119747351
    92	 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,-
    93	 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,-
    94	 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,-
    95	 - - [15/Dec/2014:23:00:10 +0800],-,400 0,-,-,-,-,-,-,-
    96	 - - [15/Dec/2014:23:00:10 +0800],GET /identified?key=this-is-TMP-apikey&digest=76BB7C854168DCD1BF15FCB92E4114FFD4CFE7DE&digest-algorithm=magnet&hash=28003498535& HTTP/1.1,200 65,-,Thunder Windows Client 7.9,-,magnet:?xt=urn:btih:76BB7C854168DCD1BF15FCB92E4114FFD4CFE7DE,EC55F9E92A5212LQ,ABP-209R,1067178826
    97	 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,-
    98	 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,-
    99	 - - [15/Dec/2014:23:00:10 +0800],-,400 0,-,-,-,-,-,-,-
   100	121.12.57.154 - - [15/Dec/2014:23:00:10 +0800],GET /identified? HTTP/1.1,401 112,-,-,-,-,-,-,-


動作其他用法1:sed '/模式/動作' 文件

可以指定某種模式的行進行動作操作,如下(這裡的文件名是a,注意cat a的輸出):

1、以p開頭的行前加[TAB]

$ cat a
pa:11:a
sa:32:c
app:5:b
stort:1:d
pear:4:aa
hello:3:f

$ sed '/^p/s/^/\t/' a
	pa:11:a
sa:32:c
app:5:b
stort:1:d
	pear:4:aa
hello:3:f

2、刪除以a開頭的行,(那麼下面的輸出,以a開頭的行就沒了)

$ sed '/^a/d' a
pa:11:a
sa:32:c
stort:1:d
pear:4:aa
hello:3:f
3、反向匹配(文件a中,輸出只保留了a開頭的行)

$ sed '/^a/!d' a
app:5:b

4、等價與grep -v (輸出文件a中,非p字母開頭的行)

$ grep  -v '^h' a
pa:11:a
sa:32:c
app:5:b
stort:1:d
pear:4:aa

$ sed -n '/^h/!p' a
pa:11:a
sa:32:c
app:5:b
stort:1:d
pear:4:aa


動作其他用法2:

也可以使用成對的正則表達式或行號與正則表達式的結合,來選取一定范圍的行,如下:

1、sed '1,3d' file 刪除1至3行

2、sed -n '1,3p' file 打印1至3行

3、sed '1, /^$/d' file 刪除范圍:第一行至第一個空行 (注意格式)

4、sed '/^$/, /^app/d' file 刪除范圍:第一個空行至以app開頭的行

5、sed '$d' file 刪除最後一行

注意:反向匹配行,是動作前加感歎號 (!d !s !c等等)


如果前面的命令直接加入-i,則直接修改文件

Copyright © Linux教程網 All Rights Reserved