欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux Sed编辑器基础命令操作

程序员文章站 2022-06-21 15:34:13
...

一、替换命令(substitute)

  • 替换命令默认情况下,只替换每行中出现的第一处。要替换不同地方出现的文本必须使用【替换标记】,替换标记会在替换命令字符串之后设置替换标记(flags)

  • 格式:s/pattern/replacement/flags

  • 有四种可用的替换标记:

    • 数字:表明新文件将替换第几处模式匹配的地方
    • g :表明新文件将会替换所有匹配的文本
    • p : 表明原先行的内容要打印出来
    • w file :将替换的结果写入到文件中
$ cat data4
This is a tests of the tests script
This is the second tests of the tests script
$ sed 's/tests/trial/' data4
This is a trial of the tests script
This is the second trial of the tests script
#替换每行中的第2处匹配的文本
$ sed 's/tests/trial/2' data4
This is a tests of the trial script
This is the second tests of the trial script
#替换整个文件中的匹配文本
$ sed 's/tests/trial/g' data4
This is a trial of the trial script
This is the second trial of the trial script
  • -n选项 会禁止sed编辑器输出,p替换标记会输出修改后的行。二者结合使用就是:只输出被替换命令修改后的行
$ sed -n  's/second/last/p' data4
This is the last tests of the tests script
  • -i 选项直接修改读取的文件内容,而不是输出到终端。直接对文本文件进行操作

##1.使用地址

  • 默认情况下,替换命令作用于文本的所有行,若要作用于特定行或某些行***,则使用【行寻址】,两种形式的行寻址:*

    • 以数字形式表示行区间;
    • 用文本模式过滤器。
$ cat data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

1)指定单个行号

$ sed '2s/dog/cat/' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog

2)用起始行号,逗号,结尾行号指定一定范围区间的行

$ sed '2,3s/dog/cat/' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy dog

3)从某行开始的所有行,使用特殊地址 $ 符号

$ sed '2,$s/dog/cat/' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat

4)使用文本模式过滤器

  • 格式:/pattern/command (必须用正斜线将指定的pattern封起来)
$ sed '/fox/s/dog/cat/' data1
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat
The quick brown fox jumps over the lazy cat

5)命令组合

格式:  address{
		command1
		command2
		...
	}
$ sed '2{
>s/fox/elphent/
>s/dog/cat/
>}' data1
The quick brown fox jumps over the lazy dog
The quick brown elphent jumps over the lazy cat
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
$ sed '3,${
>s/brown/green/
>s/lazy/cative/
>}' data1
The quick brown fox jumps over the lazy dog
The quick brown fox jumps over the lazy dog
The quick green fox jumps over the cative dog
The quick green fox jumps over the cative dog

二、插入(insert)和附加(append)文本

  • 插入(insert)命令(i)会在指定行前增加一个新行
  • 附加(append)命令(a)会在指定行后增加一个新行
$ cat data6
This is line number 1
This is line number 2
This is line number 3
This is line number 4
#在第3行前增加一行
$ sed '3i\
> This is an inserted line' data6

This is line number 1
This is line number 2
This is an inserted line
This is line number 3
This is line number 4
#在第3行后增加一行
$ sed '3a\
>This is an append line' data6

This is line number 1
This is line number 2
This is line number 3
This is an append line
This is line number 4
#在最后一行增加一行
$ sed '$a\
>This is an new line of test' data6

This is line number 1
This is line number 2
This is line number 3
This is line number 4
This is an new line of 

三、修改行

  • 修改(change)命令(c),必须在sed命令中单独指定新行
$ cat data6
This is line number 1
This is line number 2
This is line number 3
This is line number 4
#替换第2行信息
$ sed '2c\
> This is a change line of tests' data6

This is line number 1
This is a change line of tests
This is line number 3
This is line number 4
#使用文本模式过滤器 替换 匹配到 number 3 模式的行
$ sed '/number 3/c\
>This is a change line of tests' data6

This is line number 1
This is line number 2
This is a change line of tests
This is line number 4
#使用行寻址指定行区间第2行~第3行 ,并进行替换
$ sed '2,3c\       
>This is a change line of tests' data6

This is line number 1
This is a change line of tests
This is line number 4

四、删除行

  • 删除(delete)命令(d),可以通过特定行区间、特殊的文件结尾符 $、模式匹配。可使用两个文本模式来删除某个区间内的行,第一个模式会“打开”行删除功能,第二个模式会“关闭”行删除功能
$ cat data6
This is line number 1
This is line number 2
This is line number 3
This is line number 4
#删除第3行
$ sed '3d' data6
This is line number 1
This is line number 2
This is line number 4
#删除第2、3行
$ sed '2,3d' data6
This is line number 1
This is line number 4
#从第3行删除到最后一行
$ sed '3,$d' data6

This is line number 1
This is line number 2
#使用文本模式过滤器,删除 匹配到 number 2模式的行
$ sed '/number 2/d' data6

This is line number 1
This is line number 3
This is line number 4
#使用两个文本模式来删除,匹配到number 1模式“开启”删除功能,匹配到number 3模式“关闭”删除功能
$ sed '/number 1/,/number 3/d' data6
This is line number 4

五、使用sed处理文件

1.写入文件,使用w命令向文件中写入行

  • 格式:[address] w filename

    • address 支持单个行号、文本模式、行区间等寻址方式
    • filename 可以使用相对路径或绝对路径
#将data6中的第1,2行写入到testwrite文件中
$ sed -n '1,2 w testwrite' data6
$
$ cat testwrite 
This is line number 1
This is line number 2

2.从文件中读取数据

  • 读取(read)命令(r)允许将一个独立的文件中的数据插入到数据流中。sed编辑器会将文件中文本插入到指定地址后

  • 格式:[address] r filename

    • address 支持单个行号、文本模式。
$ cat testread 
This is tests reads from files and add
#将文件中的信息添加到data6文件第3行之后
$ sed '3r testread' data6
This is line number 1
This is line number 2
This is line number 3
This is tests reads from files and add
This is line number 4

六、转换命令

  • 转换(transform)命令(y)可以处理单个字符

  • 格式:[address]y/inchars/outchars/

  • 转换命令会对inchars和outchars的值进行一对一的映射,inchars的第一个字符会被转换成outchars中的第一个字符,依此类推,直到处理完指定的字符。

$ cat data6
This is line number 1
This is line number 2
This is line number 3
This is line number 4
#将1234分别替换为5678
$ sed 'y/1234/5678/' data6
This is line number 5
This is line number 6
This is line number 7
This is line number 8