Linux文件内容处理-sed

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  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  $
  • 删除2-5行:nl regular_express.txt | sed '2,5d',这里2-5行都被删除。d代表删除 sed后面接的动作必须以两个单引号'括住
[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  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
  • 删除第3到最后一行:nl regular_express.txt | sed '3,$d',其中$代表最后一行
[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.
  • 在第二行后加上(即加在第三行)drink teanl regular_express.txt | sed '2a drink tea',如果在第二行前加上的话:nl regular_express.txt | sed '2i drink tea' 和删除类似,2是行数,a代表第二行后添加,i代表第二行前添加
[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.
  • 添加多行 ,比如在第二行后面添加两行:nl regular_express.txt | sed '2a drink tea or ......\ drink beer ?' 注意其中有一个反斜杠\来进行换行。
[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.
     ...
  • 将第2-5行的内容替换为 NO 2-5 number:nl regular_express.txt | sed '2,5c No 2-5 number', 其中的c代表替换
[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.
...
  • 仅列出 第5~7行:nl regular_express.txt | sed -n '3,7p' -n代表安静模式,在一般sed的用法中,所有来自STDIN的数据一般都会列出到屏幕上。但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者操作)才会被列出来。
    我们对比一下两个结果:
[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  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.
  • 将[.]替换为[*]:nl regular_express.txt | sed 's/.//g'* (sed 's/要被替换的字符串/新的字符串/g' g代表全局替换)
[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  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

你可能感兴趣的:(Linux文件内容处理-sed)