sed编辑器基础——s命令

sed编辑器简介

sed编辑器是流编辑器(stream editor),sed编辑器不会修改原文件中的内容。

sed编辑器操作步骤

  1. 一次从输入中读取一行数据。
  2. 根据所提供的编辑器命令匹配数据。
  3. 按照命令修改流中的数据。
  4. 将新的数据输出到STDOUT。

sed编辑器基础

sed的s命令

s命令的格式:
s/pattern/replacement/flags
s命令会用斜线间指定的第二个文本字符串来替换第一个文本字符串模式。

-> echo "This is a test" | sed 's/test/big test/'
This is a big test 

s命令也可以编辑整个文件,准备一个data1.txt文件

-> cat data1.txt
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 monkey.
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.
-> sed 's/dog/cat/' data1.txt
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 monkey.
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.

从结果中可以看到所有的dog->cat了。

在命令行中使用多个编辑器命令

-> sed -e 's/dog/cat/;s/monkey/dog/' data1.txt
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.
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.

从文件中读取编辑器命令

准备的脚本文件如下:

-> cat script1.sed
s/brown/green/
s/fox/elephant/
s/dog/cat/
-> sed -f script1.sed data1.txt
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy monkey.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.
The quick green elephant jumps over the lazy cat.

s命令中的flags

替换命令在替换多行的文本时能正常工作,默认情况下只替换每行中出现的第一处(当flags为空的时候)。要让替换命令能够替换一行中不同的地方出现的文本必须使用替换标记(flags)。
替换标记:
1. 数字,表明新文本将替换第几处模式匹配的地方;
2. g,表明新文体将会替换所有匹配的文本;
3. p,表明匹配的行要打印出来;
4. w file,将替换的结果写到文件中。

-> cat data2.txt
This is a test of the script.
This is the second test of the test script.
# 指定sed编辑器用新文本替换第几处模式匹配的地方
—> sed 's/test/trial/2' data2.txt
This is a test of the script.
This is the second test of the trial script.
# 匹配全部(g)
-> sed 's/test/trial/g' data2.txt
This is a trial of the script.
This is the second trial of the trial script.
# p会打印与替换命令中指定的模式匹配的行
-> cat data3.txt
This is a test line.
This is a different line.
-> sed 's/test/trial/' data3.txt
This is a trial line.
This is a different line.
# 加上p输出
-> sed 's/test/trial/p' data3.txt
This is a trial line.
This is a trial line.
This is a different line.
# -n选项禁止sed编辑器输出;-n和p只输出被替换命令修改过的行。
-> sed -n 's/test/trial/p' data3.txt
This is a trial line.
# sed编辑器的正常输出是在STDOUT中,只有包含匹配模式的行才会保存在指定的输出文件中
-> sed 's/test/trial/w test.txt' data3.txt
This is a trial line.
This is a different line.
-> cat test.txt
This is a trial line.

s命令中的字符

-> cat data4.txt
/bin/bash/yangyun
/bin/bash/home
# sed编辑器允许选择其他字符来作为替换命令中的字符串分隔符
-> sed 's!/bin/bash!/bin/zsh!' data4.txt
/bin/zsh/yangyun
/bin/zsh/home
# 转义字符"\",不过看起来没有上面的清晰
-> sed 's/\/bin\/bash/\/bin\/zsh/' data4.txt
/bin/zsh/yangyun
/bin/zsh/home

sed的次提示符

-> sed -e '
> s/brown/green/
> s/fox/elephant/
> s/dog/cat/' data1.txt
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy monkey.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.
The quick green fox jumps over the lazy cat.

只要输入第一个单引号标示出sed程序脚本的起始,终端会继续提示你输入更多命令,直到输入了标示出结束的单引号。

猜你喜欢

转载自blog.csdn.net/qq_33704186/article/details/83478033
今日推荐