Linux三剑客之sed命令详解

1、概述

Linux三剑客:grep、sed、awk。grep主打查找功能,sed主要是编辑行,awk主要是分割列处理。本篇文章我们详细介绍sed命令。

sed 是stream editor的简称,也就是流编辑器。它是文本处理中非常重要的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

2、语法+实例

[root@liuchao ~]# sed --help
用法: sed [选项] '命令' [输入文件]

2.1、常用命令 

  • y:替换字符,通常y命令的用法是这样的:y/Source-chars/Dest-chars/,分割字符/可以用任意单字符代替,用Dest-chars中对应位置的字符替换掉Soutce-chars中对应位置的字符,Dest-chars字符长度必须和Soutce-chars长度一致
# 要查找的文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1
this is a page2
this is a page3
this is a page4
# 将文件内的所有‘p’字符替换为'd' 字符 将替换结果打印到屏幕上
[root@liuchao sed_test]# sed 'y/p/d/' sedtest_01.txt
this is a dage1
this is a dage2
this is a dage3
this is a dage4
# 源文件其实是未替换的
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1
this is a page2
this is a page3
this is a page4
[root@liuchao sed_test]#
# 目标字符长度和源字符长度不一致 报错
[root@liuchao sed_test]# sed 'y/page/dd/' sedtest_01.txt
sed:-e 表达式 #1,字符 10:“y”命令的字符串长度不同
  • s:替换字符串,通常s命令的用法是这样的:1,$s/Regexp/Replacement/Flags,分隔字符/可以用其他任意单字符代替,用Replacement替换掉匹配字符串
# 查找文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1
this is a page2
this is a page3
this is a page4

# s和y 功能相识,唯一有区别的是目标字符长度和源字符长度可以不一致
[root@liuchao sed_test]# sed 's/pa/ddd/' sedtest_01.txt
this is a dddge1
this is a dddge2
this is a dddge3
this is a dddge4
[root@liuchao sed_test]#
  • a\:追加行,a\的后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选择的行的后面都加上字符串s
  • c\:替换行,c\后面跟上字符串s(多行字符串可以用\n分隔),则会将当前选中的行替换成字符串s
  • i\:插入行,i\后面跟上字符串s(多行字符串可以用\n分隔),则会在当前选中的行的前面都插入字符串s
  • d:删除行delete,该命令会将当前选中的行删除
# 测试文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1  Page5  page6
test is a page10

this is a page2
this is a page3
this is a page4
this is a Page7
test is a page9

# 删除所有空白行
[root@liuchao sed_test]# sed '/^$/d' sedtest_01.txt
this is a page1  Page5  page6
test is a page10
this is a page2
this is a page3
this is a page4
this is a Page7
test is a page9

# 删除第二行
[root@liuchao sed_test]# sed '2d' sedtest_01.txt
this is a page1  Page5  page6

this is a page2
this is a page3
this is a page4
this is a Page7
test is a page9

# 删除第二行至最后一行
[root@liuchao sed_test]# sed '2,$d' sedtest_01.txt
this is a page1  Page5  page6

# 删除最后一行
[root@liuchao sed_test]# sed '$d' sedtest_01.txt
this is a page1  Page5  page6
test is a page10

this is a page2
this is a page3
this is a page4
this is a Page7
test is a page9

# 删除所有以 ‘test’ 开头的行
[root@liuchao sed_test]# sed '/^test/'d sedtest_01.txt
this is a page1  Page5  page6

this is a page2
this is a page3
this is a page4
this is a Page7

[root@liuchao sed_test]#
  • p:打印print,该命令会打印当前选择的行到屏幕上
# 测试文件内容
[root@liuchao sed_test]# cat sedtest_2.txt
this is a page1  Page5  page6
this is a page2
this is a page3
this is a page4
this is a Page7
[root@liuchao sed_test]#

# 打印所有行 可以看出所有行打印了两边
[root@liuchao sed_test]# sed p sedtest_2.txt
this is a page1  Page5  page6
this is a page1  Page5  page6
this is a page2
this is a page2
this is a page3
this is a page3
this is a page4
this is a page4
this is a Page7
this is a Page7

# 将匹配出的结果打印一遍 
[root@liuchao sed_test]# sed '/page2/p' sedtest_2.txt
this is a page1  Page5  page6
this is a page2
this is a page2
this is a page3
this is a page4
this is a Page7
[root@liuchao sed_test]#
  • =:打印当前行号码。
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1
this is a page2
this is a page3
this is a page4
[root@liuchao sed_test]# sed = sedtest_01.txt
1
this is a page1
2
this is a page2
3
this is a page3
4
this is a page4
[root@liuchao sed_test]#

2.2、常用选项

  • -n 使用安静silent模式。在一般sed的用法中,所有来自stdin的内容一般都会被列出到屏幕上。但如果加上-n参数后,则只有经过sed特殊处理的那一行(或者动作)才会被列出来
  • -e 直接在指令列模式上进行 sed 的动作编辑
  • -f 直接将 sed 的动作写在一个文件内,-f filename则可以执行filename内的sed命令
  • -r 让sed命令支持扩展的正则表达式(默认是基础正则表达式)
  • -i 直接修改读取的文件内容,而不是由屏幕输出
# 测试文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1  page5  page6
this is a page2
this is a page3
this is a page4
# 通过 s g 查找所有匹配的字符串 并打印再屏幕上  注意:源文件内容未被更改
[root@liuchao sed_test]# sed 's/pa/dd/g' sedtest_01.txt
this is a ddge1  ddge5  ddge6
this is a ddge2
this is a ddge3
this is a ddge4
# 再次查看源文件内容,源文件未被更改
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1  page5  page6
this is a page2
this is a page3
this is a page4
# 通过 s g 查找所有匹配的字符串 并更改源文件内容
[root@liuchao sed_test]# sed -i 's/pa/dd/g' sedtest_01.txt
[root@liuchao sed_test]#
# 可以看出源文件内容被更改
[root@liuchao sed_test]# cat sedtest_01.txt
this is a ddge1  ddge5  ddge6
this is a ddge2
this is a ddge3
this is a ddge4
[root@liuchao sed_test]#

2.3、替换选项

  • \digit:Replacement中可含有后向引用中的\digit(digit是1至9),引用前面定义的子表达
  • &:代表模版空间中的整个匹配部分
  • \L:将在其后的替换部分转换成小写字母,直到发现一个\U或\E,GNU扩展功能
  • \l:将下一个字符转换成小写字母,GNU扩展功能
  • \U:将在其后的替换部分转换成大写字母,直到发现一个\L或\E,GNU扩展功能
  • \u:将下一个字符转换成大写字母,GNU扩展功能
  • \E:停止由\L或\U指示开始的大小写转换,GNU扩展功能

2.4、标志选项

  • g:将用Replacement替换模版空间中所有匹配Regexp的部分,则不仅仅是第一个匹配部分

加上g标识,将会替换行所有匹配的字符串

# 查找的文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1  page5  page6
this is a page2
this is a page3
this is a page4
[root@liuchao sed_test]#
# 通过s 查找每行第一个匹配的
[root@liuchao sed_test]# sed 's/pa/dd/' sedtest_01.txt
this is a ddge1  page5  page6
this is a ddge2
this is a ddge3
this is a ddge4
# 通过 s g 组合查找每行所有匹配
[root@liuchao sed_test]# sed 's/pa/dd/g' sedtest_01.txt
this is a ddge1  ddge5  ddge6
this is a ddge2
this is a ddge3
this is a ddge4
[root@liuchao sed_test]#
  • digit:只用Replacement替换模版空间中第digit(digit是1至9)个匹配Regexp的部分
  • p:若发生了替换操作,指示显示模版空间中新的数据
  • w file-name:若发生了替换操作,指示将模版空间中新的数据写入指定的文件file-name中
  • i:表示进行Regexp匹配时,是不区分大小写字母的
# 查找文件内容
[root@liuchao sed_test]# cat sedtest_01.txt
this is a page1  Page5  page6
this is a page2
this is a page3
this is a page4
this is a Page7
[root@liuchao sed_test]#
# 通过 s i 查找每行第一个匹配的字符串 并替换打印在屏幕
[root@liuchao sed_test]# sed 's/pa/dd/i' sedtest_01.txt
this is a ddge1  Page5  page6
this is a ddge2
this is a ddge3
this is a ddge4
this is a ddge7
[root@liuchao sed_test]#
# 通过 s  ig 标志位组合方式查找每行所有匹配的字符串,并替换打印在屏幕
[root@liuchao sed_test]# sed 's/pa/dd/ig' sedtest_01.txt
this is a ddge1  ddge5  ddge6
this is a ddge2
this is a ddge3
this is a ddge4
this is a ddge7
[root@liuchao sed_test]#

猜你喜欢

转载自blog.csdn.net/u011837804/article/details/129857920