Linux文件内容处理-sed

2018-05-10  本文已影响0人  Minato666

sed命令是一个管道命令,具有替换,删除,新增, 选取特定行等功能。下面介绍一下其的简单用法:
源文件:

     1  "Open Source" is a good mechanism to develop programs.$
     2  apple is my favorite food.$
     3  Football game is not use feet only.$
     4  this dress doesn't fit me.$
     5  However, this dress is about $ 3183 dollars.^M$
     6  GNU is free air not free beer.^M$
     7  Her hair is very beauty.^M$
     8  I can't finish the test.^M$
     9  Oh! The soup taste good.^M$
    10  motorcycle is cheap than car.$
    11  This window is clear.$
    12  the symbol '*' is represented as start.$
    13  Oh!^IMy god!$
    14  The gd software is a library for drafting programs.^M$
    15  You are the best is mean you are the no. 1.$
    16  The world <Happy> is the same with "glad".$
    17  I like dog.$
    18  google is the best tools for search keyword.$
    19  goooooogle yes!$
    20  go! go! Let's go.$
    21  # I am VBird$
    22  $
[root@192 Desktop]# nl regular_express.txt | sed '2,5d'
     1  "Open Source" is a good mechanism to develop programs.
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.
    10  motorcycle is cheap than car.
    11  This window is clear.
    12  the symbol '*' is represented as start.
    13  Oh! My god!
    14  The gd software is a library for drafting programs.
    15  You are the best is mean you are the no. 1.
    16  The world <Happy> is the same with "glad".
    17  I like dog.
    18  google is the best tools for search keyword.
    19  goooooogle yes!
    20  go! go! Let's go.
    21  # I am VBird
[root@192 Desktop]# nl regular_express.txt | sed '3,$d'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
[root@192 Desktop]# nl regular_express.txt | sed '2a drink tea'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
drink tea
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
[root@192 Desktop]# nl regular_express.txt | sed '2i drink tea'
     1  "Open Source" is a good mechanism to develop programs.
drink tea
     2  apple is my favorite food.
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
[root@192 Desktop]# nl regular_express.txt | sed '2a drink tea or ...... \
> drink beer ?'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
drink tea or ...... 
drink beer ?
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
     5  However, this dress is about $ 3183 dollars.
     6  GNU is free air not free beer.
     ...
[root@192 Desktop]# nl regular_express.txt  | sed '2,5c No 2-5 number'
     1  "Open Source" is a good mechanism to develop programs.
No 2-5 number
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.
    10  motorcycle is cheap than car.
...
[root@192 Desktop]# nl regular_express.txt | sed  '3,7p'
     1  "Open Source" is a good mechanism to develop programs.
     2  apple is my favorite food.
     3  Football game is not use feet only.
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
     4  this dress doesn't fit me.
     5  However, this dress is about $ 3183 dollars.
     5  However, this dress is about $ 3183 dollars.
     6  GNU is free air not free beer.
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
     7  Her hair is very beauty.
     8  I can't finish the test.
     9  Oh! The soup taste good.
    10  motorcycle is cheap than car.
    11  This window is clear.
    12  the symbol '*' is represented as start.
    13  Oh! My god!
    14  The gd software is a library for drafting programs.
    15  You are the best is mean you are the no. 1.
    16  The world <Happy> is the same with "glad".
    17  I like dog.
    18  google is the best tools for search keyword.
    19  goooooogle yes!
    20  go! go! Let's go.
    21  # I am VBird

加-n的时候,3-7行每一行都重复了一次。下面是不加-n的结果。只有被sed操作的行被显示出来。

[root@192 Desktop]# nl regular_express.txt | sed -n '3,7p'
     3  Football game is not use feet only.
     4  this dress doesn't fit me.
     5  However, this dress is about $ 3183 dollars.
     6  GNU is free air not free beer.
     7  Her hair is very beauty.
[root@192 Desktop]# nl regular_express.txt | sed 's/\./***/g'
     1  "Open Source" is a good mechanism to develop programs***
     2  apple is my favorite food***
     3  Football game is not use feet only***
     4  this dress doesn't fit me***
     5  However, this dress is about $ 3183 dollars***
     6  GNU is free air not free beer***
     7  Her hair is very beauty***
     8  I can't finish the test***
     9  Oh! The soup taste good***
    10  motorcycle is cheap than car***
    11  This window is clear***
    12  the symbol '*' is represented as start***
    13  Oh! My god!
    14  The gd software is a library for drafting programs***
    15  You are the best is mean you are the no*** 1***
    16  The world <Happy> is the same with "glad"***
    17  I like dog***
    18  google is the best tools for search keyword***
    19  goooooogle yes!
    20  go! go! Let's go***
    21  # I am VBird
上一篇下一篇

猜你喜欢

热点阅读