Linux Shell:`sed`命令

本文详细介绍了LinuxShell中的sed命令,包括基本用法如文本替换、删除行和插入,以及高级技巧如多命令执行、使用变量和地址范围。
摘要由CSDN通过智能技术生成

Linux Shell:sed命令

Linux Shell 中的 sed 命令是一种强大的文本处理工具,常用于文本替换、删除、插入和更多复杂的文本操作。在本文中,我们将深入探讨 sed 命令的基本用法和一些高级技巧,帮助你提升在 Linux 环境下处理文本的能力。

在这里插入图片描述

基本用法

sed 命令的基本语法如下:

sed [选项] '命令' 文件名

Sed Help 文档

root@m1-server:~# sed
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...
  -n, --quiet, --silent
                 suppress automatic printing of pattern space
      --debug
                 annotate program execution
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if SUFFIX supplied)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -E, -r, --regexp-extended
                 use extended regular expressions in the script
                 (for portability use POSIX -E).
  -s, --separate
                 consider files as separate rather than as a single,
                 continuous long stream.
      --sandbox
                 operate in sandbox mode (disable e/r/w commands).
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
  -z, --null-data
                 separate lines by NUL characters
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <https://www.gnu.org/software/sed/>.
General help using GNU software: <https://www.gnu.org/gethelp/>.

文本替换

sed 最常见的用途之一是替换文本。例如,要在文本文件中将所有出现的"old"替换成"new",你可以使用如下命令:

sed 's/old/new/g' filename.txt

这里,s 表示替换操作,/old/new/ 是替换模式,g 表示全局替换。如果省略 g,则只替换每行的第一个匹配项。

删除行

使用 sed 可以方便地删除文本中的特定行。例如,删除文件中的第二行:

sed '2d' filename.txt

或者,删除所有包含特定文本的行:

sed '/text_to_delete/d' filename.txt

插入和附加文本

sed 命令也允许在文件中的指定位置插入新的文本行。例如,在第三行之前插入一行文本:

sed '3i\This is the inserted text.' filename.txt

相似地,可以使用 a 命令在特定行之后添加文本:

sed '3a\This is the appended text.' filename.txt

高级技巧

多命令执行

sed 可以通过使用 -e 选项执行多个命令:

sed -e 's/old/new/g' -e '2d' filename.txt

这个命令会将所有"old"替换为"new",然后删除第二行。

使用变量

sed 命令中使用 shell 变量可以提高脚本的灵活性。例如:

text_to_replace="old"
new_text="new"
sed "s/$text_to_replace/$new_text/g" filename.txt

地址范围

sed 允许你指定一个命令应用的行范围。例如,仅在第二行到最后一行之间替换文本:

sed '2,$s/old/new/g' filename.txt

参考链接

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑风风

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值