带你了解linux:学习第十课 linux shell 之 sed

今天我们将继续介绍文本编辑三剑客之一的sed 

sed 是一个流编辑器,主要用于对输入流(文件或管道)进行文本转换和处理。它支持多种编辑命令,可以用来替换文本、删除行、插入文本等。下面将详细介绍 sed 的基本用法、常用参数以及具体的使用示例。

基本用法

sed 的基本语法如下:

sed [options] 'command' [file...]
  • options: 可选的选项,用于控制 sed 的行为。
  • command: 编辑命令,可以是一个或多个命令,用分号 ; 分隔。
  • file...: 要处理的文件列表。如果不指定文件,sed 会从标准输入读取数据。

常用参数

  • -e script: 添加脚本到命令列表。
  • -f script-file: 从文件中读取编辑命令。
  • -i: 直接修改文件内容,而不是输出到标准输出。
  • -n: 只打印被处理的行。
  • -r 或 -E: 使用扩展正则表达式。
  • -s: 将每个输入文件视为单独的流。
  • -u: 不缓冲输出,立即输出每一行。

常用命令

  • s/pattern/replacement/flags: 替换命令,将匹配 pattern 的内容替换为 replacement
  • d: 删除匹配的行。
  • p: 打印匹配的行。
  • a\ text: 在匹配的行后面添加一行 text
  • i\ text: 在匹配的行前面插入一行 text
  • c\ text: 将匹配的行替换为 text
  • g: 将模式空间的内容替换为保持空间的内容。
  • G: 将保持空间的内容追加到模式空间的末尾。

示例

假设有一个文件 data.txt,内容如下:

Hello world
This is a test
Replace this line
Delete this line
Insert this line
1. 替换文本

将文件 data.txt 中的 world 替换为 Earth

sed 's/world/Earth/' data.txt

输出:

Hello Earth
This is a test
Replace this line
Delete this line
Insert this line
2. 全局替换

将文件 data.txt 中所有出现的 is 替换为 was

sed 's/is/was/g' data.txt

输出:

Hello world
This was a test
Replace this line
Delete this line
Insert this line
3. 替换特定行

将文件 data.txt 中第2行的 is 替换为 was

sed '2s/is/was/' data.txt

输出:

Hello world
This was a test
Replace this line
Delete this line
Insert this line
4. 删除行

删除文件 data.txt 中包含 Delete 的行:

sed '/Delete/d' data.txt

输出:

Hello world
This is a test
Replace this line
Insert this line
5. 插入行

在文件 data.txt 中包含 Insert 的行后面插入一行 New line

sed '/Insert/a\ New line' data.txt

输出:

Hello world
This is a test
Replace this line
Delete this line
Insert this line
New line
6. 插入行(前面)

在文件 data.txt 中包含 Insert 的行前面插入一行 New line

sed '/Insert/i\ New line' data.txt

输出:

Hello world
This is a test
Replace this line
Delete this line
New line
Insert this line
7. 替换行

将文件 data.txt 中包含 Replace 的行替换为 New replaced line

sed '/Replace/c\ New replaced line' data.txt

输出:

Hello world
This is a test
New replaced line
Delete this line
Insert this line
8. 只打印匹配的行

只打印文件 data.txt 中包含 test 的行:

sed -n '/test/p' data.txt

输出:

This is a test
9. 多个命令

在一个命令中执行多个 sed 操作:

sed -e 's/old/new/g' -e '/delete/d' data.txt

假设 data.txt 内容为:

Hello old
This is a delete
Replace this line
Delete this line
Insert this line

输出:

Hello new
Replace this line
Insert this line
10. 直接修改文件

直接修改文件 data.txt,将 test 替换为 example

sed -i 's/test/example/g' data.txt

修改后的 data.txt 内容:

Hello world
This is a example
Replace this line
Delete this line
Insert this line
11. 使用扩展正则表达式

使用扩展正则表达式将文件 data.txt 中的 test 替换为 example

sed -E 's/test/example/g' data.txt
12. 保留备份文件

直接修改文件 data.txt,并将修改前的文件备份为 data.txt.bak

sed -i.bak 's/test/example/g' data.txt

综合示例

假设有一个文件 data.txt,内容如下:

Hello world
This is a test
Replace this line
Delete this line
Insert this line
1. 替换、删除和插入

Replace this line 替换为 New replaced line,删除包含 Delete 的行,并在包含 Insert 的行后面插入 New inserted line

sed -e 's/Replace this line/New replaced line/' \
    -e '/Delete/d' \
    -e '/Insert/a\ New inserted line' data.txt

输出:

Hello world
This is a test
New replaced line
Insert this line
New inserted line
2. 直接修改文件

直接修改文件 data.txt,将 test 替换为 example,并在包含 world 的行前面插入 New header

sed -i -e 's/test/example/' \
    -e '/world/i\ New header' data.txt

修改后的 data.txt 内容:

New header
Hello world
This is a example
New replaced line
Insert this line
New inserted line

通过以上介绍,你应该对 sed 的基本用法和常用参数有了全面的了解。sed 是一个非常强大的文本处理工具,适用于各种文本编辑和转换任务。如果你有任何问题或需要进一步的帮助,请随时提问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值