二十一 sed命令

2020-05-05  本文已影响0人  supermanto

sed替换命令讲解

1 sed 的模式空间

sed的基本工作方式是:
 - 将文件以行为单位读取到内存(模式空间)
 - 使用sed的每个脚本对该行进行操作
 - 处理完成后输出改行

2 sed替换命令

sed的替换命令s:
 - sed 's/old/new/' filename
 - sed -e 's/old/new/' -e 's/old/new/' filename ...
 - sed -i 's/old/new/' 's/old/new/' filename ...

带正则表达式的替换命令s:
 - sed 's /正则表达式/new' filename
 - sed -r 's /扩展正则表达式/new' filename

(1) 将a字符替换为bb,可以发现只有第一个a替换为bb了,这是sed命令的程序控制的。替换后不会影响原文件

--- ~/shell_practice » echo a a a > test.txt
--- ~/shell_practice » cat test.txt                                        
a a a
--- ~/shell_practice » sed 's/a/bb/' test.txt
bb a a
--- ~/shell_practice » cat test.txt
a a a

(2) 多次替换,可以使用一下两种方法:

--- ~/shell_practice » sed -e 's/a/bb/' -e 's/bb/cc/' test.txt              1 ↵
cc a a
--- ~/shell_practice » sed 's/a/bb/;s/bb/cc/' test.txt
cc a a

(3) 多个文件的替换

--- ~/shell_practice » cat test.txt
a a a
--- ~/shell_practice » cat test1.txt
a a a
--- ~/shell_practice » sed 's/a/bb/;s/bb/cc/' test.txt test1.txt
cc a a
cc a a

(4) 将替换结果写入到原文件

--- ~/shell_practice » sed -i '' 's/a/bb/' test.txt
--- ~/shell_practice » cat test.txt
bb a a

(5) 正则表达式

--- ~/shell_practice » cat test.txt                                         1 ↵
Hello marry!
hello mike!
hello xie!
hello marry!
# 删除前面三个字符
--- ~/shell_practice » sed 's/...//' test.txt
lo marry!
lo mike!
lo xie!
lo marry!
# 将开头的H替换为a
--- ~/shell_practice » sed 's/^H/a/' test.txt
aello marry!
hello mike!
hello xie!
hello marry!
# 
--- ~/shell_practice » sed 's/H*e/a/' test.txt
allo marry!
hallo mike!
hallo xie!
hallo marry!
# 这里的*是指元字符,匹配0个或多个前面的字符
--- ~/shell_practice » cat bfile
b
a
aa
aaa
ab
abb
abbb
--- ~/shell_practice » sed 's/ab*/!/' bfile
b
!
!a
!aa
!
!
!

(6) 扩展元字符

# 扩展元字符+,表示匹配前面出现过1次或多次
--- ~/shell_practice » sed -r 's/ab+/!/' bfile
b
a
aa
aaa
aa
!
!
# 扩展元字符?,表示匹配前面出现0次或1次
--- ~/shell_practice »sed -r 's/ab?/!/' bfile
b
!
!a
!aa
!a
!b
!bb
# 扩展元字符|,表示匹配前面任一个字符
--- ~/shell_practice » sed -r 's/a|b/!/' bfile
!
!
!a
!aa
!a
!bb
!bbb
# 如果要匹配多个字符,要加个括号()分组
--- ~/shell_practice » sed -r 's/(aa)|(bb)/!/' bfile
b
a
!
!a
!
a!
a!b
# ()还可以实现回调,待补充

sed替换指令加强版

  • 全局替换
  • 标志位
  • 寻址
  • 分组
  • sed脚本文件

(1) 全局替换
以passwd文件为例子

--- ~/shell_practice » head -2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
# 全局替换,在替换命令后面加个g
--- ~/shell_practice » head -2 /etc/passwd | sed 's/root/!/g'
!:x:0:0:!:/!:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin

(2) 标志位

s/old/new/标志位

  • 数字,第几次出现才替换
  • g,每次出现都进行替换
  • p 打印模式空间的内容
     sed -n script' filename 阻止默认输出
  • w file 将模式空间的内容写入到文件
# 只替换第一个匹配的位置,默认情况也是指替换第一个匹配位置
--- ~/shell_practice » head -2 /etc/passwd | sed 's/root/!/1'
!:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
# 替换第二个匹配位置
--- ~/shell_practice » head -2 /etc/passwd | sed 's/root/!/2'
root:x:0:0:!:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
# 只输出替换的行
--- ~/shell_practice » head -2 /etc/passwd | sed -n 's/root/!/p'
!:x:0:0:root:/root:/bin/bash
# 将模式空间的内容写入到文件
--- ~/shell_practice » head -2 /etc/passwd | sed -n 's/root/!/w cfile'
--- ~/shell_practice » cat cfile
!:x:0:0:root:/root:/bin/bash

(3) 寻址

默认对每行进行操作,增加寻址后对匹配的行进行操作

  • /正则表达式/s/old/new/g
  • 行号s/old/new/g
     行号可以是具体的行,也可以是最后一行$符号
  • 可以使用两个寻址符号,也可以混合使用行号和正则地址
# 指定行号替换:第三行的匹配项才进行替换
--- ~/shell_practice » head -3 /etc/passwd | sed '3s/usr/!/'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/!/sbin/nologin
# 指定行号替换:第三四行的匹配项才进行替换
--- ~/shell_practice » head -4 /etc/passwd | sed '3,4s/usr/!/'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/!/sbin/nologin
sys:x:3:3:sys:/dev:/!/sbin/nologin
# 按照正则表达式替换: 
--- ~/shell_practice » head -4 /etc/passwd | sed '/^daemon/s/usr/!/'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/!/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
# 指定行号和正则来替换:以daemon开头这一行开始到最后一行
head -4 /etc/passwd | sed '/^daemon/,$s/usr/!/'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/!/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/!/sbin/nologin
sys:x:3:3:sys:/dev:/!/sbin/nologin

(4) 分组

寻址可以匹配多条命令
/regular/ { s/old/new/ ; s/old/new/ }

(5) 脚本文件

可以将选项保存为文件,使用-f加载脚本文件
sed -f sedscript filename

sed的其他命令

  • 删除命令
  • 追加、插入、更改
  • 打印
  • 下一行
  • 读文件和写文件
  • 退出命令

sed的多行模式

为什么要有多行模式
 配置文件一般是单行出现,但是也有使用xml和json格式的配置文件,为多行出现
多行模式处理命令N、D、P

sed的保持空间

(待补充)

上一篇下一篇

猜你喜欢

热点阅读