【Linux进阶命令 03】sed (文本的流编辑器)
  XNNIEyXQkqUG 2023年11月02日 23 0



文章目录

  • 一、简介以及与grep、awk比较
  • 二、sed语法
  • 三、sed使用示例
  • 3.1 字符串替换:s
  • 3.2 删除:d
  • 3.3 打印:p
  • 3.4 追加行:a
  • 3.5 插入行:i
  • 3.6 替换行:c
  • 3.7 字符集替换:y


一、简介以及与grep、awk比较

sed命令是一个流编辑器,用于在Linux上执行基本的文本操作。它可以根据脚本的指令来处理、编辑文本文件。sed命令通常用于查找和替换文件中的字符串,但它还可以执行其他操作,例如删除行、插入行、打印行等。

sed :stream editor for filtering and transforming text 注:在 Linux 中,流通常指从文件、管道或其他程序的输出中读取的数据流。sed 是一种流编辑器,它可以对这些数据流进行操作。

到此,已经介绍到Linux的第三大文本分析工具了,前介绍的有:

  1. 【Linux进阶命令 01】grep(文本的全局搜索与打印)
  2. 【Linux进阶命令 02】awk(更加强大的文本分析工具)

这三个工具都是用于处理文本的,但它们的侧重点各不相同。

  • grep:grep是一个用于在文件中查找模式的工具。它可以搜索文件中的文本,并将包含该模式的行打印到标准输出。grep通常用于查找文件中的特定字符串或模式
  • sed:sed是一个流编辑器,用于在Linux上执行基本的文本操作。它可以根据脚本的指令来处理、编辑文本文件。sed命令通常用于查找和替换文件中的字符串,但它还可以执行其他操作,例如删除行、插入行、打印行等。
  • awk:awk是一种强大的文本处理工具,它可以对文本进行格式化和处理。awk命令通常用于从文件中提取数据并对其进行格式化

注:

  1. 它们不会直接修改源文件数据,只会修改缓冲区数据(然后打印出来);
  2. 它们都是基于行来处理数据的,即一行一行地处理;
  3. 脚本(规则)文件后缀没有特别要求,但最好可读性强,如:rule.awk、rule.txt、rule.sed
  4. 它们都支持正则表达式匹配文本。

二、sed语法

sed [OPTION]... ‘script’ [input-file]...

(方括号表示是可选项,下同)

常用的选项(optinon):

option是可选项,如果留空,则默认打印所有行

  • -n:静默输出,即不打印任何内容。
  • -e:允许对输入数据应用多条sed命令编辑。
  • -i:直接修改文件内容,而不是输出到终端。
  • -r:支持使用扩展正则表达式。
  • -f:从文件中读取sed脚本。
  • -h:禁止输出文件名。
  • -V:显示版本信息。

若果使用了-f选项,就从脚本读取命令;否则就执行 ‘scrip’(这里的script表示一些简单、简短的操作,如果是复杂操作,最好放到脚本文件中,增强可读性)出的命令:

  1. sed ‘’ file.txt:打印文件所有行
  2. sed -f rule.sed file.txt:按照规则文件处理文件
  3. sed 's/foo/bar/g' file.txt:将所有foo字符串替换为bar

以下是一些常见的sed操作(在script中使用):

  • s:替换操作,用于替换文本中的字符串。
  • d:删除操作,用于删除文本中的行。
  • p:打印操作,用于打印文本中的行。
  • a:追加操作,用于在指定行后追加文本。
  • i:插入操作,用于在指定行前插入文本。
  • c:替换操作,用于替换指定行的文本。
  • y:转换操作,用于将字符从一个集合转换为另一个集合。

三、sed使用示例

测试文件:rfc4234.txt

The exact nature of the review within the IETF is not specified,
 but it is expected that documents be posted for review in the relevant WG mailing lists.
 Often no relevant mailing list exists,
 in which case area-specific or IETF main discussion list can be used.
 Individual reviewers, review teams,
 and review boards for specific topics can also be used.
 If no sufficient review has been obtained, the AD should solicit it explicitly.

3.1 字符串替换:s

这是最常用的操作,格式:

sed ' [address-range|pattern-range] s/original-string/replacement-string/ [substitute-flags]' inputfile

其中:

  • address-range 是文本的范围 (行起始位置)。例如,1,10 表示从第一行到第十行;
  • /pattern/ 表示匹配包含 pattern 的行。
  • original-string 是要被替换的字符串,
  • replacement-string 是替换后的字符串。
  • substitute-flags 是可选的标志,用于控制替换操作的行为:

常用的标志包括:

  • g:全局替换,即替换所有匹配的字符串,而不仅仅是第一个。
  • n:打印匹配行以外的所有行。
  • p:打印匹配行(如过只想打印匹配行,使用 -n选项)。
  • i:忽略大小写。
  • 数字(1-512,假设数字是n),只修改某一行匹配到的的第n个
  • w file:将匹配行写入文件 file 中。

例1: 替换所有匹配的字符串
将所有含的of(忽略大小写)替换成0,并打印修改过的行

root@CQUPTLEI:~/Linux_test# sed -n  's/of/0/gpi' rfc4234.txt
The exact nature 0 the review within the IETF is not specified,
 0ten no relevant mailing list exists,

以下两种方法,都没有上述输出效果:

sed  's/of/0/gpi' rfc4234.txt
 sed -n  's/of/0/gi' rfc4234.txt

-n 选项会禁止 sed 输出,但 p 标记会输出修改过的行,将二者匹配使用的效果就是只输出被替换命令修改过的行。

例2: 替换所有匹配得到单词

现在要求只能在匹配到整个单词时才能替换,如often不会被替换

root@CQUPTLEI:~/Linux_test# sed -n  's/\<of\>/0/gpi' rfc4234.txt
The exact nature 0 the review within the IETF is not specified,

\<of\>表示整个单词(正则表达式)

例3: 只替换匹配的第2个

root@CQUPTLEI:~/Linux_test# sed -n  's/review/0/p2' rfc4234.txt
 Individual reviewers, 0 teams,

例4 : 只对某几行匹配

对3到7行进行匹配

sed -n  '3,7s/review/0/p2' rfc4234.txt

3.2 删除:d

用于删除指定的行或行范围。d命令的一般形式为:

sed ' [address-range|pattern-range] d' inputfile

其中,address-range 是文本的范围 (行起始位置)。例如,1,10 表示从第一行到第十行;/pattern/ 表示匹配包含 pattern 的行。

例5: 删除1到4行

root@CQUPTLEI:~/Linux_test# sed '1,4d' rfc4234.txt
 Individual reviewers, review teams,
 and review boards for specific topics can also be used.
 If no sufficient review has been obtained, the AD should solicit it explicitly.

例6: 删除包含is的行

root@CQUPTLEI:~/Linux_test# sed '/is/d' rfc4234.txt
 Individual reviewers, review teams,
 and review boards for specific topics can also be used.
 If no sufficient review has been obtained, the AD should solicit it explicitly.

3.3 打印:p

sed ‘2p’ test 重复打印第 2 行
sed ‘1,3p’ test 重复打印第1~3行
sed -n ‘2p’ test 只打印第 2 行
sed -n ‘1,3p’ test 只打印第 1~3 行
sed -n ‘/hani/p’ test 打印匹配到 hani 的行,类似grep
sed -n ‘/hani/!p’ test ! 反选,打印没有匹配到 hani 的行
sed -n ‘s/old/new/gp’ test 只打印匹配替换的行

3.4 追加行:a

a命令是sed中的新增命令,用于在指定的行后面添加新的文本。a命令的一般形式为:

sed ' [address-range|pattern-range] a\ [text]' inputfile

其中,address-range 是文本的范围 (行起始位置)。例如,1,10 表示从第一行到第十行;/pattern/ 表示匹配包含 pattern 的行。text 是要添加的文本。

例7: 在1到4行,每行,后面添加一行内容

root@CQUPTLEI:~/Linux_test# sed '1,4a\这是新加的' rfc4234.txt
The exact nature of the review within the IETF is not specified,
这是新加的
 but it is expected that documents be posted for review in the relevant WG mailing lists.
这是新加的
 Often no relevant mailing list exists,
这是新加的
 in which case area-specific or IETF main discussion list can be used.
这是新加的
 Individual reviewers, review teams,
 and review boards for specific topics can also be used.
 If no sufficient review has been obtained, the AD should solicit it explicitly.

3.5 插入行:i

和追加一样,只不过是在前面插入一行

3.6 替换行:c

格式同上,替换匹配的行(一整行都替换掉)。

3.7 字符集替换:y

y命令是sed中的转换命令,用于将指定字符集中的字符替换为另一个字符集中的字符。y命令的一般形式为:

sed ' [address-range|pattern-range] y/ [source-chars]/[dest-chars]/' inputfile

其中,address-range 是文本的范围 (行起始位置)。例如,1,10 表示从第一行到第十行;/pattern/ 表示匹配包含 pattern 的行。source-chars 是要替换的字符集,dest-chars 是要替换成的字符集,即将source-chars中的第一个字符替换成dest-chars 中的第一个字符,依次类推。

例8: 将abcd所有小写转为大写

root@CQUPTLEI:~/Linux_test# sed  'y/abcd/ABCD/' rfc4234.txt
The exACt nAture of the review within the IETF is not speCifieD,
 But it is expeCteD thAt DoCuments Be posteD for review in the relevAnt WG mAiling lists.
 Often no relevAnt mAiling list exists,
 in whiCh CAse AreA-speCifiC or IETF mAin DisCussion list CAn Be useD.
 InDiviDuAl reviewers, review teAms,
 AnD review BoArDs for speCifiC topiCs CAn Also Be useD.
 If no suffiCient review hAs Been oBtAineD, the AD shoulD soliCit it expliCitly.


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
XNNIEyXQkqUG