📜  Linux/Unix 中的 Sed 命令示例

📅  最后修改于: 2022-05-13 01:57:34.854000             🧑  作者: Mango

Linux/Unix 中的 Sed 命令示例

UNIX 中的 SED 命令代表流编辑器,它可以对文件执行很多函数,如搜索、查找和替换、插入或删除。尽管 UNIX 中 SED 命令的最常见用途是用于替换或查找和替换。通过使用 SED,您甚至可以在不打开文件的情况下对其进行编辑,这比首先在 VI 编辑器中打开该文件然后更改它要快得多。

  • 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 等标志替换一行中第一次、第二次出现的模式。下面的命令在一行中用“linux”替换第二次出现的单词“unix”。
    $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 命令在一行中用“linux”字样替换第三、第四、第五个……“unix”字样。
    $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. 括号每个单词的第一个字符:这个 sed 示例打印括号中每个单词的第一个字符。
    $ echo "Welcome To The Geek Stuff" | sed 's/\(\b[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.
    

    如果不使用 /p 单独使用 -n,则 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
    

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

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

    6. 删除模式匹配行

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


Linux 中的 SED 命令 | 2套