sed的簡單使用

sed主要用於替換指定的字符,sed中有一個pattern space,sed每次只能處理一行,將內容放到pattern space中,處理完後會清空pattern space。

圖片轉載

語法
用法: sed [選項]... {腳本(如果沒有其他腳本)} [輸入文件]...

  -n, --quiet, --silent
                 取消自動打印模式空間
  -e 腳本, --expression=腳本
                 添加“腳本”到程序的運行列表
  -f 腳本文件, --file=腳本文件
                 添加“腳本文件”到程序的運行列表
  --follow-symlinks
                 follow symlinks when processing in place; hard links
                 will still be broken.
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied).
                 The default operation mode is to break symbolic and hard links.
                 This can be changed with --follow-symlinks and --copy.
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode.
                 While this will avoid breaking links (symbolic or hard), the
                 resulting editing operation is not atomic.  This is rarely
                 the desired mode; --follow-symlinks is usually enough, and
                 it is both faster and more secure.
  -l N, --line-length=N
                 指定“l”命令的換行期望長度
  --posix
                 關閉所有 GNU 擴展
  -r, --regexp-extended
                 在腳本中使用擴展正則表達式
  -s, --separate
                 將輸入文件視爲各個獨立的文件而不是一個長的連續輸入
  -u, --unbuffered
                 從輸入文件讀取最少的數據,更頻繁的刷新輸出
      --help     打印幫助並退出
      --version  輸出版本信息並退出

按條件過濾
[root@localhost wang]# cat test.txt 
#abc
#abcd
#abcdef
#123
1234
12345
def
abcdef
abc123
def123abc
ABC
#ABCD

#打印包含abc的行,分隔符可以用/,也可以用@,其他符號也行
[root@localhost wang]# sed -n '/abc/'p test.txt 
#abc
#abcd
#abcdef
abcdef
abc123
def123abc
[root@localhost wang]# 

sed不想用轉義符號時,需要加-r參數

#打印1開頭的行
[root@localhost wang]# sed -n '/^1/'p test.txt 
1234
12345

#打印abc結尾的行
[root@localhost wang]# sed -n '/abc$/'p test.txt 
#abc
def123abc
 
#打印第2[root@localhost wang]# sed -n '2'p test.txt 
#abcd

#打印2-7[root@localhost wang]# sed -n '2,7'p test.txt 
#abcd
#abcdef
#123
1234

#打印第5行和後面的所有行
[root@localhost wang]# sed -n '5,$'p test.txt 
1234
12345
def
abcdef
abc123
def123abc
ABC
#ABCD

#不區分大小寫
[root@localhost wang]# sed -n '/abc/'Ip test.txt 
#abc
#abcd
#abcdef
abcdef
abc123
def123abc
ABC
#ABCD
[root@localhost wang]# 

刪除滿足條件的行
#不顯示前5行,但文件中未刪除
[root@localhost wang]# sed '1,5'd test.txt 

1234

12345


def
abcdef

abc123
def123abc

ABC
#ABCD
[root@localhost wang]# cat test.txt 
#abc
#abcd
#abcdef
#123


1234

12345


def
abcdef

abc123
def123abc

ABC
#ABCD


#文件中前5行刪除
[root@localhost wang]# sed -i '1,5'd test.txt 
[root@localhost wang]# cat test.txt 

1234

12345


def
abcdef

abc123
def123abc

ABC
#ABCD
[root@localhost wang]# 
替換
#全局替換1-10行的abc爲ABC,g爲全局替換
[root@localhost wang]# sed  '1,10s/abc/ABC/g' test.txt 
#全局替換所有行的abc爲ABC
[root@localhost wang]# sed  's/abc/ABC/g' test.txt 

#將第一列和最後一列進行替換
[root@localhost wang]# cat 123.txt 
abc:jsak:12njsdjq:kljlkhsa:hahah:wang
wanghuan:kjskjfd:popl9:q2j2q:ruhr
#將abc和wang替換,將wanghuan和ruhr替換
[root@localhost wang]# sed -r 's/([^:]+):(.*):([^:]+)/\3:\2:\1/' 123.txt 
wang:jsak:12njsdjq:kljlkhsa:hahah:abc
ruhr:kjskjfd:popl9:q2j2q:wanghuan
[root@localhost wang]# 
# 分成3段:
([^:]+):
(.*):        #貪婪匹配,直到最後一個冒號
([^:]+)
N的作用

通俗一點理解,N會使sed將當前行和下一行當作一行處理,就而已把兩行變成一行
圖片轉載

[root@localhost wang]# cat 1.txt
12323sdlka
dskladlk
dalweads
dsakdlk32
sdafdsfsd
24324fdsfs
[root@localhost wang]# sed 'N;s/\n/:/' cat 1.txt   //將換行符替換爲冒號
12323sdlka:dskladlk
dalweads:dsakdlk32
sdafdsfsd:24324fdsfs
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章