linux文本处理三剑客之sed

linux文本处理三剑客之sed

简介

sed(流编辑器)是一种用于文本处理的强大工具,它可以对文本进行查找、替换、删除、插入等操作。sed通过一条或多条命令来操作输入流,并将结果输出到标准输出。sed语句通常由命令和模式组成,它们使用基本的正则表达式来匹配和操作文本。

sed 命令的语法格式:

sed [options] 'command' file

options:选项

-n 静默模式 常使用p命令时会用(关闭默认打印)
-i 改变原文件
-f file 跟脚本文件
-e 可以执行多个命令语句

command命令(简单用法)

a:新增, a 的后面可以接字串,而这些字串会在新的一行出现(目前的下一行)~
c:取代, c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
d:删除,因为是删除啊,所以 d 后面通常不接任何咚咚;
i:插入, i 的后面可以接字串,而这些字串会在新的一行出现(目前的上一行);
p:打印,亦即将某个选择的数据印出。通常 p 会与参数 sed -n 一起运行~
s:取代,通常这个 s 的动作可以搭配正规表示法,例如 1,20s/old/new/g 。
y:大小写转换,和s用法类似,但只能替换大小写
n:读取下一个输入行, 用下一个命令处理新的行
&:经常用来在原文本行中增加字符串。

基本用法

以下是一些常用的sed命令和它们的用法示例:
  1. 打印文本:sed 'p' test - 将文件file.txt的内容打印到标准输出。
    显示文本行号,sed '=' test,显示文件test中内容的行号
    [root@centos2 ~]# cat test       //准备一个文件,写入一些数据进行实验
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    [root@centos2 ~]# sed 'p' abc   //p选项进行打印,会将结果打印一份,但同时也会将原文再次打一份
    hello world
    hello world
    name = root
    name = root
    sed awk grep
    sed awk grep
    192.168.195.100
    192.168.195.100
    255.255.255.0
    255.255.255.0
    [root@centos2 ~]# sed -n 'p' abc  //通常我们会将p选项和-n同时使用,-n是关闭默认的打印功能
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    
    只处理匹配的行:sed '/root/p' test - 只打印文件test中匹配到的行。
    [root@centos2 ~]# sed '/root/p' test     //在不加-n的情况下,虽然将匹配到的打印出来了,但其他的也默认打印了一份
    hello world
    name = root
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    [root@centos2 ~]# sed -n '/root/p' test   //此时需要加上-n选项关闭默认打印,只打印我们需要的行
    name = root
    
    打印行号“=”
    [root@centos2 ~]# sed '=' test    //直接使用=就是将所有行都显示行号
    1
    hello world
    2
    name = root
    3
    sed awk grep
    4
    192.168.195.100
    5
    255.255.255.0
    6
    .Ah "Major Heading"
    7
    See Section 1.4
    8
    See Section 12.9
    9
    (See Section 12.9)
    
    
    [root@centos2 ~]# sed '/^See/=' test   //同样也可以匹配指定的行,显示指定行的行号
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    7
    See Section 1.4
    8
    See Section 12.9
    (See Section 12.9)
    
  2. 指定打印次数后退出
    [root@centos2 ~]# cat test
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    
    See Section 1.4
    See Section 12.9
    
    (See Section 12.9)
    [root@centos2 ~]# sed '4q' test       //打印四行后退出
    hello world
    name = root
    sed awk grep
    192.168.195.100
    [root@centos2 ~]#
    
  3. 替换文本:sed 's/root/yyr/' test - 将文件file.txt中匹配到的第一个pattern替换为replacement。
    [root@centos2 ~]# sed 's/root/yyr/' test
    hello world
    name = yyr
    sed awk grep
    192.168.195.100
    255.255.255.0
    
    [root@centos2 ~]# sed 's/255/yyr/' test    //当所匹配的内容有多个的情况下,默认是将第一个替换
    hello world
    name = root
    sed awk grep
    192.168.195.100
    yyr.255.255.0
    [root@centos2 ~]# sed 's/255/yyr/2' test  //可以再后面加上数字表示第几个
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.yyr.255.0
    
    [root@centos2 ~]# sed 's/255/yyr/g' test  //加上g的话就是全部替换
    hello world
    name = root
    sed awk grep
    192.168.195.100
    yyr.yyr.yyr.0
    
  4. 位置替换,通过分组的方式,将组替换位置
    [root@centos2 ~]# echo "hello world yyr" | sed -r 's/sed (.*) (.*)/sed \2 \1/'
    hello world yyr
    
  5. 特殊符号的替换,例如tab键
    [root@centos2 ~]# cat tab
    hello world
    name = root
    sed awk grep
    192.	168.195.	100      //此处两个空出均为tab键
    255.255.255.0
    
    //“\t”就是tab键的意思,“\n”则是换行的意思,此处就是将第二个tab键替换为换行,tab键后面的内容另起一行显示
    [root@centos2 ~]# sed 's/\t/\n/2' tab
    hello world
    name = root
    sed awk grep
    192.	168.195.
    100
    255.255.255.0
    
  6. 删除行:sed '1d' test 删除文件test的第一行。
    [root@centos2 ~]# cat test
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    
    要删除文件中的特定行,可以使用以下命令:
    sed 'N,Md' test
    其中,N和M是要删除的行的范围。例如,要删除名为test的文件中的第3行和第5行,可以运行以下命令:
    [root@centos2 ~]# sed '3,5d' test
    hello world
    name = root
    [root@centos2 ~]# sed '1d' test     //可以指定删除哪一行
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    
    [root@centos2 ~]# sed '$d' test     //当文本内容过多的时候,想要删除掉最后一行,可以用$表示
    hello world
    name = root
    sed awk grep
    192.168.195.100
    
    [root@centos2 ~]# sed '/^&/d' test    //删除文本中的空行
    
  7. 插入行:sed '1i\123' test - 在文件test的第一行前插入一行123的文本。
    [root@centos2 ~]# sed '1i\123' test
    123
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    
  8. 新增行: sed 'hello world/a\123'将文件test中匹配到的内容后面另起一行123的文本。
    [root@centos2 ~]# cat test
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    [root@centos2 ~]# sed '/hello world/a123' test
    hello world
    123
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    [root@centos2 ~]# sed '/hello world/a   123' test     //可以看出,在不加\进行换行的情况下,现在新加的内容前面输入空格是不起作用的
    hello world
    123
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    [root@centos2 ~]# sed '/hello world/a\    123' test  //在加上\后可以在内容前面输出空格符
    hello world
        123
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    
  9. 读取匹配到的下一个输入行, 用下一个命令处理新的行
    [root@centos2 ~]# cat test
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    
    See Section 1.4
    See Section 12.9
    
    (See Section 12.9)
    [root@centos2 ~]# sed '/\.Ah/{n;/^$/d}' test   //如果匹配到的后面一行为空行则删除
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    See Section 1.4
    See Section 12.9
    
    (See Section 12.9)
    [root@centos2 ~]# sed '/name/{n;d}' test   //删除匹配到的后面一行
    hello world
    name = root
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    
    See Section 1.4
    See Section 12.9
    
    (See Section 12.9)
    [root@centos2 ~]# sed '/hello/{n;/root/s/root/ftx/}' test   //匹配的后面一行若有root,则将root替换为ftx
    hello world
    name = ftx
    sed awk grep
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    
    See Section 1.4
    See Section 12.9
    
    (See Section 12.9)
    
  10. 取代行:sed '2ftx' test ,c 的后面可以接字串,这些字串可以取代 n1,n2 之间的行!
    [root@centos2 ~]# cat test
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    [root@centos2 ~]# sed '2cftx' test
    hello world
    ftx
    sed awk grep
    192.168.195.100
    255.255.255.0
    
    [root@centos2 ~]# sed '2,4cftx' test   //2,4就是2到4行,此处则是将2,3,4这三行换成了ftx这一行
    hello world
    ftx
    255.255.255.0
    
    如果想要取代为多行,则可以加上“\n”符
    [root@centos2 ~]# sed '2,4chello\nworld' test
    hello world
    hello
    world
    255.255.255.0
    
  11. 使用&命令,在匹配的文本中增加字符串。
    //此处的&表示的是tom本身,也就是说$现在就是tom,所以1&2就是1tom2
    [root@centos2 ~]# echo 'hello world tom' | sed -r 's/tom/1&2/'
    hello world 1tom2
    
    //在文本中由于内容过多,所以我们可以先匹配到哪一行,然后再进行添加字符串
    [root@centos2 ~]# sed -r '/hello/s/world/& tom/' test
    hello world tom
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    
    //将所有匹配到的内容都加上(),针对的是内容,没有针对整行
    [root@centos2 ~]# cat test
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    See Section 1.4
    See Section 12.9
    (See Section 12.9)
    [root@centos2 ~]# sed -r 's/See Section [1-9][0-9]?\.[1-9][0-9]?/(&)/' test
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    (See Section 1.4)
    (See Section 12.9)
    ((See Section 12.9))
    
    [root@centos2 ~]# sed -r 's/[1-9][0-9]?\.[1-9][0-9]?/(&)/' test
    hello world
    name = root
    sed awk grep
    1(92.16)8.195.100
    2(55.25)5.255.0
    .Ah "Major Heading"
    See Section (1.4)
    See Section (12.9)
    (See Section (12.9))
    
  12. 大小写转换
    [root@centos2 ~]# cat test
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    See Section 1.4
    See Section 12.9
    (See Section 12.9)
    [root@centos2 ~]# sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' test
    HELLO WORLD
    NAME = ROOT
    SED AWK GREP
    192.168.195.100
    255.255.255.0
    .AH "MAJOR HEADING"
    SEE SECTION 1.4
    SEE SECTION 12.9
    (SEE SECTION 12.9)
    
    1. 把每个单词的第一个小写字母变大写:
    [root@centos2 ~]# sed 's/\b[a-z]/\u&/g' test
    Hello World
    Name = Root
    Sed Awk Grep
    192.168.195.100
    255.255.255.0
    .Ah "Major Heading"
    See Section 1.4
    See Section 12.9
    (See Section 12.9)
    
    
    2. 把所有小写变大写:
    [root@centos2 ~]# sed 's/[a-z]/\u&/g' test
    HELLO WORLD
    NAME = ROOT
    SED AWK GREP
    192.168.195.100
    255.255.255.0
    .AH "MAJOR HEADING"
    SEE SECTION 1.4
    SEE SECTION 12.9
    (SEE SECTION 12.9)
    
    3. 大写变小写:
    sed 's/[A-Z]/\l&/g' filename
    [root@centos2 ~]# sed 's/[A-Z]/\l&/g' test
    hello world
    name = root
    sed awk grep
    192.168.195.100
    255.255.255.0
    .ah "major heading"
    see section 1.4
    see section 12.9
    (see section 12.9)
    
    
  13. 执行多个编辑命令,sed允许串联多个编辑命令,以实现多个操作。
例如,将名为test的文件中的"hello"替换为"ftx",并删除包含"name"的行:
[root@centos2 ~]# sed -e 's/hello/ftx/g' -e '/name/d' test
ftx world
sed awk grep
192.168.195.100
255.255.255.0
.Ah "Major Heading"

[root@centos2 ~]# sed -e 's/hello/ftx/g;/name/d' test   //也可以用;进行多个编辑命令
ftx world
sed awk grep
192.168.195.100
255.255.255.0

当我们的某行文本内容需要进行多次编辑时就需要用到此方法,例如:
将下列内容:
.Ah "Major Heading"
替换为:
@A HEAD = Major Heading

此时我们需要做的第一步便是先将.Ah 替换为 @A HEAD
[root@centos2 ~]# sed  -n '/.Ah/s/.Ah/@A HEAD =/p' test
@A HEAD = "Major Heading"

然后再将"去掉,也就是替换为空
[root@centos2 ~]# sed '/.Ah/s/"//g' test
hello world
name = root
sed awk grep
192.168.195.100
255.255.255.0
.Ah Major Heading

结合起来结果如下:
[root@centos2 ~]# sed  -n '/.Ah/s/.Ah/@A HEAD =/p;s/"//g' test
@A HEAD = "Major Heading"
     ~~~



10. ##### 要仅对匹配特定模式的行执行操作,可以使用以下命令:

#####         例如,仅删除名为test的文件中非包含"name"的行:

```
[root@centos2 ~]# cat test
hello world
name = root
sed awk grep
192.168.195.100
255.255.255.0 
[root@centos2 ~]# sed '/name/!d' test
name = root
```



## 使用场景

下面列举了一些sed语句的常见使用场景:

1. 批量替换文本:sed可用于批量替换文本文件中的特定字符串。

 ~~~powershell
 //我们需要的内容是下列引号中的内容,其他的删除或进行替换
 [root@centos2 ~]# cat abc
 .Ah "Comment"
 .Ah "Substitution"
 .Ah "Delete"
 .Ah "Append, Insert and Change"
 .Ah "List"
 
 //首先第一步我们要将符合条件的行匹配出来,然后将引号替换为空,然后再将.Ah替换为空。
 [root@centos2 ~]# sed -r '/^\.Ah/{p;s/"//g;s/^\.Ah//}' abc
 .Ah "Comment"
  Comment
 .Ah "Substitution"
  Substitution
 .Ah "Delete"
  Delete
 .Ah "Append, Insert and Change"
  Append, Insert and Change
 .Ah "List"
  List
  1. 删除或提取特定行:通过使用sed,可以删除或提取文件中特定模式的行,例如删除空行或提取包含特定关键字的行。

  2. 批量添加或删除内容:使用sed可以在文件的特定位置添加或删除内容,例如在文件开头添加版权声明或删除特定标记。

  3. 格式化文本:sed提供了一些强大的格式化功能,例如调整文本的缩进、对齐列等。

  4. 大规模数据处理:由于sed是一种流编辑器,效率较高,因此常用于大规模数据处理任务,如日志分析、数据清洗等。

高级sed命令

多行模式空间

在sed命令中,多行模式空间是一种特殊的模式,它允许对多行文本块进行处理。通常情况下,sed命令逐行读取输入文本并对每一行进行处理。但是,当使用多行模式空间时,sed命令可以将多个连续的行(以及可能包含的特定模式)存储在一个缓冲区中(可以理解为工作室,在工作室中进行处理),并对它们进行操作。
在多行模式空间中,sed命令使用特定的命令和模式匹配规则来选择和处理多个行。一旦多行模式匹配成功,可以执行一系列操作,如替换、删除、插入等。可以通过在命令行中使用-n选项来关闭默认的打印输出,以便只输出符合匹配条件的行。
sed命令的多行模式空间在处理大型文件或复杂文本结构时非常有用,可以提供更灵活的文本处理能力。
案例:

多行模式空间在处理需要跨越多行的文本块时非常有用,可以实现更复杂的文本操作。通过组合使用N命令和P命令,可以根据需要在模式空间中操作、打印和处理多行文本。

  1. N命令:将下一行添加到模式空间中。
  2. P命令:打印模式空间的第一行。
使用这两个命令的组合,可以操作和打印多行文本块。
[root@centos2 ~]# cat test
hello world
name = root
sed awk grep
192.168.195.100
255.255.255.0
.Ah "Major Heading"

See Section 1.4
See Section 12.9

(See Section 12.9)

//{N;P} 是命令序列,表示对于匹配到的行,执行N命令将下一行也添加到模式空间,并执行P命令打印模式空间的第一行,执行p命令的话就是将模式空间的全部打印
[root@centos2 ~]# sed -n '/name/{N;p}' test
name = root
sed awk grep
[root@centos2 ~]# sed -n '/name/{N;P}' test
name = root
使用N命令,可以将模式空间里面两行的内容转换为一行内容
同样是用上述的test文件
[root@centos2 ~]# cat test
hello world
name = root
sed awk grep
192.168.195.100
255.255.255.0
.Ah "Major Heading"

See Section 1.4
See Section 12.9

(See Section 12.9)

//将IP地址和子网掩码部分写到一行,文本中分为两行,我们则匹配到IP地址这一行,使用N命令将下面一行的子网掩码部分也添加到模式空间,一起进行编辑,按照文本内容可以得知,IP地址后面我们是输入了一个换行符,也就是\n,我们就可以用下述方式将\n换成其他的符号,这样就可以将两行变成一行
[root@centos2 ~]# sed '/192/{N;s/100\n255/100 255/}' test
hello world
name = root
sed awk grep
192.168.195.100 255.255.255.0
.Ah "Major Heading"

See Section 1.4
See Section 12.9

(See Section 12.9)

多行删除

删除命令(d)删除模式空间的内容并导致读入新的输入行,从而在脚本的顶端重新使用编辑方法。删除命令(D)稍微有些不同:它删除模式空间中直到第一个换行符的这一部分内容,也就是只删除第一行。它不会导致读入新的输入行,相反,它返回到脚本的顶端,将这些指令应用于模式空间剩余的内容,我们可以编写一个实现查找一系列空行并输出单个空行的脚本,以看看它们之间的区别。下面的语句使用了删除命令(d) :

[root@centos2 ~]# cat b
this line is followed by 1 blank line.

this line is followed by 2 blank line.


this line is followed by 3 blank line.



this line is followed by 4 blank line.




this is the end.
[root@centos2 ~]# sed '/^$/{N;/^\n$/d}' b   //小写d可以将偶数空行全部删除,奇数空行变为一个
this line is followed by 1 blank line.

this line is followed by 2 blank line.
this line is followed by 3 blank line.

this line is followed by 4 blank line.
this is the end.
[root@centos2 ~]# sed '/^$/{N;/^\n$/D}' b   //使用大写D就可以将多个空行变为一个空行
this line is followed by 1 blank line.

this line is followed by 2 blank line.

this line is followed by 3 blank line.

this line is followed by 4 blank line.

this is the end.

当有偶数个空行时,所有的空行都会被删除。仅当有奇数个空行时,有一行被保留下来。这是因为删除命令清除的是整个模式空间。一旦遇见第一个空行,就会读入下一行,如果下一行也为空行,则两行都被删除。如果遇到第三个空行,但是下一个不为空行,那么删除命令就不会被执行,因此空行被输出。

多行Delete命令完成工作的原因是,当遇到两个空行时,Delete命令只删除两个空行的第一个。下一次遍历该脚本时,这个空行将导致另一方被读入模式空间。如果那行不为空,那么两行都输出,因此确保了输出一个空行。换句话说,当模式空间中有两个空行时。只有第一个空行被删除。当一个空行后面跟有文本时,模式空间可以正常输出。

多行打印

多行打印(Print)命令与小写字母的print命令稍有不同。该命令输出多行模式空间的第一部分,直到第一个嵌入的换行符为止。在执行完脚本的最后一个命令之后,模式空间的内容自动输出 (-n 选项或#n抑制这个默认的动作)。因此,当默认的输出被抑制或者脚本中的控制流更改,以至不能到达脚本的底部时,需要使用打印命令(P或p) .Print命令经常出现在Next命令之后和Delete命令之前。这3个命令能建立一个输入/输出循环,用来维护两行的模式空间,但是一次只输出一行。这个循环的目的是只输出模式空间的第一行,然后返回到脚本的顶端将所有的命令应用于模式空间的第二行。没有这个循环,当执行脚本中的最后一个命令时,模式空间中的这两行都将被输出。创建多行模式空间以匹配第一行结尾处的“UNIX”和第二行开始处的“System”。如果发现“UNIX System”跨越两行,那么我们将它变成“UNIX OperatingSystem”。建立这个循环以返回到脚本的顶端,并寻找第二行结尾处的“UNIX”。

案例:
替换命令匹配“\nSystem”,并且用“Operating\nSystem”取代它。保留换行是很重要的,否则模式空间中只有一行,注意Print和Delete命令的顺序。
准备一个测试文本:	
[root@centos2 ~]# cat c
Here are examples of the UNIX
System. 
Where UNIX
System appears, it should be the UNIX
Operating System.
[root@centos2 ~]# sed '/UNIX$/{N;/\nSystem/{s// Operating &/;P;D}}' c
Here are examples of the UNIX Operating 
System. 
Where UNIX Operating 
System appears, it should be the UNIX
Operating System.

sed:这是用于进行文本替换和编辑的流式文本编辑器。
'/UNIX$/':这是一个正则表达式模式,匹配以"UNIX"结尾的行。
{}:括号内的命令将仅在与模式匹配的行上执行。
N:该命令用于将下一行添加到模式空间中(即缓冲区)。
'/\nSystem/':这是另一个正则表达式模式,匹配包含"System"的行,其中\n表示换行符。
s// Operating &/:这是一个替换命令,将匹配的行替换为" Operating System",其中的"&“表示匹配到的文本,即"System”。

包含那一行

模式空间是容纳当前输入行的缓冲区。还有一个称为保持空间(hold space)的顶留(set-aside)缓冲区。模式空间的内容可以复制到保持空间,而且保持空间的内容也可以复制到模式空间。有一组命令用于在保持空间和模式空间之间移动数据。保持空间用于临时存储。单独的命令不能寻址保持空间或者更改它的内容。
保持空间最常的用途是,当改变模式空间中的原始内容时,用于保留当前输入行的副本。影响模式空间的命令有:

命令缩写功能
Holdh或H将模式空间的内容复制或追加到保持空间
Getg或G将保持空间的内容复制或追加到模式空间
Exchangex交换保持空间和模式空间的内容

这些命令中的每一条都可以利用一个地址来指定一行或行范围。Hole(h,H)命令将数据移至保持空间、而get(g,G)命令将保持空间的数据移回模式空间。同一命令的小写字母和大写字母之间的差别是,小写字母命令改写目的缓存区的内容,而大写字母命令追加缓存区的现有内容。

下面的例子中演示了如何用 h 和 g 命令来将数据在 sed 缓冲区之间移动。
[root@centos2 ~]# cat a
1
2
11
22
111
222

匹配1,11,111到模式空间然后追加到保存空间,清空模式空间,在将保存空间内容逐行追加到模式空间
[root@centos2 ~]# sed '/1/{h;d};/2/{G}' a
2
1
22
11
222
111

//x的用法,交换保持空间和模式空间的内容
[root@centos2 ~]# cat test
hello world
name = root
sed awk grep
192.168.195.100
255.255.255.0
.Ah "Major Heading"

See Section 1.4
See Section 12.9

(See Section 12.9)

首先匹配到包含hello的行,将其复制到保持空间,然后再匹配到包含name的行,此时我们的保持空间是hello的行,这时候使用x命令就可以将保持空间的和模式空间的内容交换	
[root@centos2 ~]# sed '/hello/h;/name/x' test
hello world
hello world
sed awk grep
192.168.195.100
255.255.255.0
.Ah "Major Heading"

See Section 1.4
See Section 12.9

(See Section 12.9)

大小写转换

在sed基础命令用法中,我们介绍了转换命令(y),并且描述了在一行上如何将小写字母转换为大写字母。因为y命令作用于模式空间的所有内容,所以对行的一部分进行逐字转换有点繁琐。
在文件内容过少的情况下我们也可以使用替换的方式将部署字母转换为大写,但我们只针对文本内容过少的文件可以这样处理,如果但文件内容过多,再使用替换的命令进行操作就会是一件冗长乏味的工作内容
[root@centos2 ~]# cat b
find the Match statement
Consult the Get statement.
Using the Read statement to retrieve data
[root@centos2 ~]# sed 's/find the Match statement/find the MATCH statement/' b
find the MATCH statement
Consult the Get statement.
Using the Read statement to retrieve data

转换命令可以进行小写字母到大写字母的转换,但它将转换应用于整个行。使用保持空间可以实现以上任务,因为可以用保持空间来存储输入行的备份而将语句名独立出来,并在模式空间进行转换。

同样是上面的文件:
[root@centos2 ~]# cat b
find the Match statement
Consult the Get statement.
Using the Read statement to retrieve data

我们要呈现的效果:
find the MATCH statement
Consult the GET statement.
Using the READ statement to retrieve data

//首先第一步,我们先匹配到所要处理的行,使用h命令将他们放到保持空间,此时在模式空间和保持空间中就都有一份未处理的原文内容,我们再将模式空间的内容进行处理,使用s替换,将整行匹配,把所要处理的部分用.*表示并将其分组,使用这种方式将我们想处理的东西单独取出,效果如下:
[root@centos2 ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/}' b
Match
Get
Read

//第二步则是将匹配出来的内容进行大小写转换,以下两种方法均可进行大小写转换,一个是y命令,一个是s命令
[root@centos2 ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/;y/acdeghmrt/ACDEGHMRT/}' b
MATCH
GET
READ

[root@centos2 ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/;s/[a-z]/\u&/g}' b
MATCH
GET
READ

//第三步则是将保持空间的内容重新追加到我的模式空间,效果如下:
[root@centos2 ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/;y/acdeghmrt/ACDEGHMRT/;G}' b
MATCH
find the Match statement
GET
Consult the Get statement.
READ
Using the Read statement to retrieve data


//最后一步我们则是将模式空间中每轮匹配的两行进行处理,将所需要重新排序的内容分组,再将内容重新排序,最终达到我们想要的结果,也就是下面的执行结果:
[root@centos2 ~]# sed -r '/the .* statement/{h;s/.*the (.*) statement.*/\1/;y/acdeghmrt/ACDEGHMRT/;G;s/(.*)\n(.*the ).*( statement.*)/\2\1\3/g}' b
find the MATCH statement
Consult the GET statement.
Using the READ statement to retrieve data

注:()里面放的是我们需要留下来的东西
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值