Linux/Unix中的Sed命令及其示例

UNIX中的SED命令代表流编辑器,它可以对文件执行搜索、查找和替换、插入或删除等功能。尽管在UNIX中,SED命令最常用于替换或查找和替换。通过使用SED,即使不打开文件也可以编辑文件,这比先在VI编辑器中打开文件,然后再更改文件更快地查找和替换文件中的内容。

null
  • SED是一个强大的文本流编辑器。可以进行插入、删除、搜索和替换(替换)。
  • unix中的SED命令支持正则表达式,这允许它执行复杂的模式匹配。

语法:

sed OPTIONS... [SCRIPT] [INPUTFILE...] 

例子: 考虑下面的文本文件作为输入。

$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.

示例命令

  1. 替换或替换字符串: Sed命令主要用于替换文件中的文本。下面的简单sed命令将文件中的“unix”替换为“linux”。
    $sed 's/unix/linux/' geekfile.txt
    

    输出:

    linux is great os. unix is opensource. unix is free os.
    learn operating system.
    linux linux which one you choose.
    linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
    

    这里的“s”指定替换操作。“/”是分隔符。“unix”是搜索模式,“linux”是替换字符串。

    默认情况下,sed命令将替换每行中第一次出现的图案,而不会替换第二次、第三次出现的图案。

  2. 替换一行中第n次出现的图案: 使用/1、/2等标志替换一行中第一个、第二个出现的模式。下面的命令将第二次出现的单词“unix”替换为一行中的“linux”。
    $sed 's/unix/linux/2' geekfile.txt
    

    输出:

    unix is great os. linux is opensource. unix is free os.
    learn operating system.
    unix linux which one you choose.
    unix is easy to learn.linux is a multiuser os.Learn unix .unix is a powerful.
    
  3. 替换一行中所有出现的图案: 替换标志/g(全局替换)指定sed命令来替换行中所有出现的字符串。
    $sed 's/unix/linux/g' geekfile.txt
    

    输出:

    linux is great os. linux is opensource. linux is free os.
    learn operating system.
    linux linux which one you choose.
    linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.
    
  4. 从第n个引用替换为一行中的所有引用: 使用/1、/2等和/g的组合替换一行中第n次出现的所有图案。下面的sed命令将第三个、第四个、第五个……“unix”单词替换为一行中的“linux”单词。
    $sed 's/unix/linux/3g' geekfile.txt
    

    输出:

    unix is great os. unix is opensource. linux is free os.
    learn operating system.
    unix linux which one you choose.
    unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.
    
  5. 将每个单词的第一个字符括起来: 本示例打印括号中每个单词的第一个字符。
    $ echo "Welcome To The Geek Stuff" | sed 's/([A-Z])/(1)/g'
    

    输出:

    (W)elcome (T)o (T)he (G)eek (S)tuff
    
  6. 替换特定行号上的字符串: 可以限制sed命令替换特定行号上的字符串。例如
    $sed '3 s/unix/linux/' geekfile.txt
    

    输出:

    unix is great os. unix is opensource. unix is free os.
    learn operating system.
    linux linux which one you choose.
    unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
    

    上面的sed命令仅替换第三行上的字符串。

  7. 使用/p标志复制替换的行: p打印标志在终端上打印替换的行两次。如果一行没有搜索模式且未被替换,则/p只打印该行一次。
    $sed 's/unix/linux/p' geekfile.txt
    

    输出:

    linux is great os. unix is opensource. unix is free os.
    linux is great os. unix is opensource. unix is free os.
    learn operating system.
    linux linux which one you choose.
    linux linux which one you choose.
    linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
    linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
    
  8. 仅打印替换的行: 使用-n选项和/p打印标志仅显示替换的行。在这里,-n选项抑制由/p标志生成的重复行,并只打印一次替换的行。
    $sed -n 's/unix/linux/p' geekfile.txt
    

    输出:

    linux is great os. unix is opensource. unix is free os.
    linux linux which one you choose.
    linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
    

    如果单独使用-n而不使用/p,则sed不会打印任何内容。

  9. 替换一系列行上的字符串: 可以为sed命令指定一系列行号来替换字符串。
    $sed '1,3 s/unix/linux/' geekfile.txt
    

    输出:

    linux is great os. unix is opensource. unix is free os.
    learn operating system.
    linux linux which one you choose.
    unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
    

    在这里,sed命令用范围从1到3的行替换这些行。另一个例子是

    $sed '2,$ s/unix/linux/' geekfile.txt
    

    输出:

    unix is great os. unix is opensource. unix is free os.
    learn operating system.
    linux linux which one you choose.
    linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful
    

    此处$表示文件中的最后一行。因此sed命令将文本从文件的第二行替换为最后一行。

  10. 从特定文件中删除行: SED命令还可用于从特定文件中删除行。SED命令用于执行删除操作,甚至不打开文件 例如: 1.要删除某一行,请在本例中使用n
    Syntax:
    $ sed 'nd' filename.txt
    Example:
    $ sed '5d' filename.txt
    

    2.删除最后一行

    Syntax:
    $ sed '$d' filename.txt
    

    3.删除x到y范围内的行

    Syntax:
    $ sed 'x,yd' filename.txt
    Example:
    $ sed '3,6d' filename.txt
    

    4.从第n行删除到最后一行

    Syntax:
    $ sed 'nth,$d' filename.txt
    Example:
    $ sed '12,$d' filename.txt
    

    5.删除模式匹配行

    Syntax:
    $ sed '/pattern/d' filename.txt
    Example:
    $ sed '/abc/d' filename.txt
    

Linux中的SED命令| Set 2

本文由 阿克谢·拉吉普特 莫哈克·阿格拉瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享