115

linux-document/Learn sed command with 20 examples.org at master · lujun9972/linu...

 6 years ago
source link: https://github.com/lujun9972/linux-document/blob/master/examples/Learn%20sed%20command%20with%2020%20examples.org?
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

20个例子带你入门sed命令

Sed 命令或者说流编辑器是Linux/Unix系统提供的强力工具. 她主要用于文本替代, 查找并替换.但它也能做其他的文本维护工作,比如插入,删除,搜索等. 使用SED, 我们做到不用打开文件就完成编辑操作. Sed 还支持正则表达式, 这使得 sed 还成为一款强大的测试工具.

本文中, 我们将会通过一些例子学习使用 SED 命令. 使用sed命令的基本语法为,

sed OPTIONS… [SCRIPT] [INPUTFILE…]

https://www.linuxtechi.com/wp-content/uploads/2017/11/Sed-Command-Options.jpg

现在让我们看看一些例子.

  • 例1) 显示文件部分内容

    sed可以让你只查看文件的一部分内容. 使用下面命令来查看指定行的内容,

    [linuxtechi@localhost ~]$ sed -n 22,29p testfile.txt
        

    这里, 选项 ‘n’ 抑制输出所有文件的内容,而命令 ‘p’ 会只输出22行到20行的内容.

  • 例2) 除了某些行外,显示所有内容

    使用下面命令可以输出除了指定行外的所有内容,

    [linuxtechi@localhost ~]$ sed 22,29d testfile.txt
        

    命令 ‘d’ 会将指定行从输出中删除.

  • 例3) 从第N行开始,每3行显示一行内容

    要从第2行开始,显示每3行的内容,使用下面命令

    [linuxtechi@localhost ~]$ sed -n '2-3p' file.txt
        
  • 例4) 删除一行

    使用下面命令删除一行, use the following command,

    [linuxtechi@localhost ~]$ sed Nd testfile.txt
        

    这里 ‘N’ 为行好而 ‘d’ 命令删除指定行. 要删除最后一样,则可以这样

    [linuxtechi@localhost ~]$ sed $d testfile.txt
        
  • 例5) 删除连续的几行

    要删除连续几行的内容,运行

    [linuxtechi@localhost ~]$ sed '29-34d' testfile.txt
        

    这会删除 testfile.txt 中的第29到34行的内容.txt file.

  • 例6) 只保留指定范围的内容

    要删除除了指定行之外的所有内容,使用 ‘!’

    [linuxtechi@localhost ~]$ sed '29-34!d' testfile.txt
        

    这里 ‘!’ 命令的意思是 , 它会反转条件,即不要删除指定的行. 所有行,除了 29-34 行会从文件 testfile.txt 中删掉.

  • 例7) 增加空行/空格

    要在每个非空行后添加一个空行, 使用选项 ‘G’,

    [linuxtechi@localhost ~]$ sed G testfile.txt
        
  • 例8) 搜索并替换字符串

    可以这样搜索并替换字符串,

    [linuxtechi@localhost ~]$ sed 's/danger/safety/' testfile.txt
        

    这里命令 ‘s’ 会搜索每行中第一个单词 ‘danger’ 然后将之替换为 ‘safety’.

  • 例9) 全文搜索并替换字符串

要全部替换文件中的单词,我们可以将命令 ‘g’ 与 ‘s’ 连用

[linuxtechi@localhost ~]$ sed 's/danger/safety/g' testfile.txt
  • 例10) 替换第n次出现的字符串模式

    我们可以只替换第n次出现的字符串. 比如将第二次淑贤的 ‘safety’ 替换为 ‘danger’,

    [linuxtechi@localhost ~]$ sed ‘s/danger/safety/2’ testfile.txt
        

    将每行中的第二次出现的‘danger’替换掉,

    [linuxtechi@localhost ~]$ sed 's/danger/safety/2g' testfile.txt
        
  • 例11) 只替换指定行上的字符串

    只替换指定行上的字符串, 这样做

    [linuxtechi@localhost ~]$ sed '4 s/danger/safety/' testfile.txt
        

    这只会在第四行上做替换. 你也可以指定一系列的行,

    [linuxtechi@localhost ~]$  sed '4-9 s/danger/safety/' testfile.txt
        
  • 例12) 在匹配搜索的后面/前面添加一行

    在匹配位置的后面添加一行内容, 使用命令 ‘a’ ,

    [linuxtechi@localhost ~]$ sed '/danger/a "This is new line with text after match"' testfile.txt
        

    在匹配位置的前面添加一行内容, 使用命令 ‘i’,

    [linuxtechi@localhost ~]$ sed '/danger/i "This is new line with text before match" ' testfile.txt
        
  • Example :13) 修改匹配行的整个内容

    修改匹配行的整个内容使用命令 ‘c’,

    [linuxtechi@localhost ~]$ sed '/danger/c "This will be the new line" ' testfile.txt
        

    这样若某行中包含 ‘danger’, 则整行内容都会改变.

sed高级命令

到目前为止,我们还只是用到了sed的简单表达式,现在我们来看看sed的更高级用法,这些用法往往会用到正则表达式.

  • 例14) 运行多条sed命令

    若要运行多条sed表达式, 使用命令 ‘e’ 来将多个sed命令串起来,

    [linuxtechi@localhost ~]$  sed -e 's/danger/safety/g' -e 's/hate/love/' testfile.txt
        
  • 例15) 修改文件前做个备份

使用选项‘-i.bak’来在修改文件前创建备份,

[linuxtechi@localhost ~]$ sed -i.bak -e 's/danger/safety/g'  testfile.txt

这回创建一个以 .bak 为后缀的备份文件. 当然你也可以选择其他喜欢的扩展名.

  • 例16) 删除以指定模式开头并且以指定模式结尾的行

    删除以指定模式开头并且以指定模式结尾的行,使用以下命令

    [linuxtechi@localhost ~]$ sed -e 's/danger.*stops//g' testfile.txt
        

    这回删除以‘danger’开头且以‘stops’结尾的行,‘.*’ 表示中间可以有任意多个单词.

  • 例17) 在行中添加内容

    使用sed和正则表达式在每行前面添加内容

    [linuxtechi@localhost ~]$ sed -e 's/.*/testing sed &/' testfile.txt
        

    现在每一行前面都加上 ‘testing sed’ 了.

  • 例18) 删除所有注释行以及空行

    删除所有注释的行(以#开头的行)以及空行,

    [linuxtechi@localhost ~]$ sed -e 's/#.*//;/^$/d' testfile.txt
        

    之想删除注释行的话

    [linuxtechi@localhost ~]$ sed -e 's/#.*//' testfile.txt
        
  • 例19) 从 /etc/passwd 中抽取出所有的用户名列表

    用下面命令可以从 /etc/passwd 中抽取出所有的用户名列表

    [linuxtechi@localhost ~]$  sed 's/\([^:]*\).*/\1/' /etc/passwd
        

    所有用户的列表都会显示在屏幕上.

  • 例20) 防止使用sed时覆写了系统链接

‘sed -i’ 命令会删除系链接接并且创建一个常规的文件替代这个链接. 为了防止 ‘sed -i‘ 破坏这个链接, 执行sed时你还需要使用 ‘–follow-symklinks‘ 参数.

假设我想金庸 CentOS 或 RHEL 服务器上的 SELinux,可以这样

[linuxtechi@localhost ~]# sed -i --follow-symlinks 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK