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


1、使用vim創建測試數據 a.txt

[root@linuxprobe test]# cat a.txt 1 w e t 2 s f h 3 z c g 4 e a g 5 a f w 6 k h d 7 w f r

 

2、刪除指定的行

[root@linuxprobe test]# sed '3d' a.txt ##刪除第三行 1 w e t 2 s f h 4 e a g 5 a f w 6 k h d 7 w f r
[root@linuxprobe test]# sed '1,3d' a.txt ## 刪除1到3行 4 e a g 5 a f w 6 k h d 7 w f r
[root@linuxprobe test]# sed '1d;3d' a.txt ## 刪除第一行和第三行 2 s f h 4 e a g 5 a f w 6 k h d 7 w f r
[root@linuxprobe test]# sed '/s/d' a.txt ##刪除匹配s的行 1 w e t 3 z c g 4 e a g 5 a f w 6 k h d 7 w f r [root@linuxprobe test]# sed '/w/d' a.txt ##刪除匹配w的行 2 s f h 3 z c g 4 e a g 6 k h d
[root@linuxprobe test]# sed '/^5/d' a.txt ## 刪除以5開頭的行 1 w e t 2 s f h 3 z c g 4 e a g 6 k h d 7 w f r [root@linuxprobe test]# sed '/^[35]/d' a.txt ## 刪除以3或者5開頭的行 1 w e t 2 s f h 4 e a g 6 k h d 7 w f r
[root@linuxprobe test]# sed '/h$/d' a.txt ## 刪除以h結尾的行 1 w e t 3 z c g 4 e a g 5 a f w 6 k h d 7 w f r [root@linuxprobe test]# sed '/[hw]$/d' a.txt ## 刪除以h或者w結尾的行 1 w e t 3 z c g 4 e a g 6 k h d 7 w f r
[root@linuxprobe test]# cat a.txt ##使用vim編輯器重新編輯測試數據 1 w e t 2 s f 4
3 z c g 4 e a g w a f w 6 k h d t w f 2 [root@linuxprobe test]# sed '/^[0-9]/d' a.txt ## 刪除所有以數字開頭的行 w a f w t w f 2 [root@linuxprobe test]# sed '/^[a-zA-Z]/d' a.txt ## 刪除所有以字母開頭的行 1 w e t 2 s f 4
3 z c g 4 e a g 6 k h d
[root@linuxprobe test]# sed '/[0-9]$/d' a.txt ##刪除所有以數字結尾的行 1 w e t 3 z c g 4 e a g w a f w 6 k h d [root@linuxprobe test]# sed '/[a-zA-Z]$/d' a.txt ##刪除所有以字母結尾的行 2 s f 4 t w f 2
[root@linuxprobe test]# cat a.txt ## 使用vim 編輯器重新編輯測試數據 6 w e g t s f g 2 z c g 2 e a t y a f w 6 k h w t w f 2 [root@linuxprobe test]# sed '/^2.*g$/d' a.txt ## 刪除以2開頭,同時以g結尾的行 6 w e g t s f g 2 e a t y a f w 6 k h w t w f 2

   [root@linuxprobe test]# cat a.txt ## 測試數據
   6 w e g
   t s f g
   2 z c g
   2 e a t
   y a f w
   6 k h w
   t w f 2

[root@linuxprobe test]# sed '/e/,+1d' a.txt ## 刪除包含e及其后1行 2 z c g 6 k h w t w f 2 [root@linuxprobe test]# sed '/z/,+2d' a.txt ## 刪除包含z及其后2行 6 w e g t s f g 6 k h w t w f 2

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM