sed常見用法,刪除匹配行的上2行,下3行

2022-05-09 21:51:47 字數 864 閱讀 3000

[root@test200 ~]# cat testab

cdef

[root@test200 ~]# sed '/c/' testab

c

定義乙個標籤a,匹配c,然後n把下一行加到模式空間裡,匹配最後一行時,才退出標籤迴圈,然後命令d,把這個模式空間裡的內容全部清除。

if 匹配"c"

:a追加下一行

if 不匹配"$"

goto a

最後退出迴圈,d命令刪除。

[[email protected] /home/ahao.mah/syslog-ng/muahao_tools]

#cat file

hello muahao

i am good

123 1

this is jack

this is jack

fffff

gglike this

[[email protected] /home/ahao.mah/syslog-ng/muahao_tools]

#sed '/muahao/,+1d' file

123 1

this is jack

this is jack

fffff

gglike this

[[email protected] /home/ahao.mah/syslog-ng/muahao_tools]

#sed '/muahao/,+2d' file

this is jack

this is jack

fffff

gglike this

sed刪除匹配行的上一行和下一行

aa string 變數指定匹配字串 sed i e aa e n n.aa p d file 例子 insert into bonusreturnorder values 47 224 1300573 2 1 wx20160203083601539373 hbtk20160204020000110...

sed命令刪除前100行裡匹配的行

sed是linux系統裡非常簡單實用的文字處理小工具,配合正規表示式以及find grep等基本命令,可以非常快速實現一些日常的文字處理。舉幾個簡單的例子吧 1.將當前目錄下所有txt檔案中abc替換為xyz sed i s abc xyz g txt 解釋一下,這裡 i引數是直接修改輸入檔案,s ...

linux系統刪除指定的行(sed命令)

1 使用vim建立測試資料 a.txt root linuxprobe test cat a.txt 1w e t 2s f h 3z c g 4e a g 5a f w 6k h d 7 w f r 2 刪除指定的行 root linuxprobe test sed 3d a.txt 刪除第三行 ...