正则表达式
(1)字符匹配
. :匹配任意单个字符
[] :匹配指定范围内的任意单个字符
[^] :匹配指定范围外的单个字符字符
(2)匹配次数
\? :匹配前面字符0|1次
.* :匹配任意长度任意字符
+ :匹配前面字符1|多次
* :匹配前面字符任意次
{m,}:匹配前面字符至少m次
{m,n}:匹配前面字符 m-n 次
(3)位置锚定
^ :行首锚定
$ :行尾锚定
^$ :匹配空行
^pattern$:用pattern来匹配整行
^[[:space:]]*$:空行或包含空白字符的行
^[[:space:]]. :匹配以空格开头但不为空行的行
\<root\> :词首词尾锚定:匹配单词root,而不是*root,root*,*
(4)分组引用
\(n\):分组
\n :引用第n个分组
sed
非交互式编辑器
sed [option] ... 'script command' file
option:
-n :不输出模式空间 中的内容至屏幕
-e :多点编辑
-r :支持扩展正则
-f file :以file中指定的条件匹配
-i :修改源文件
~ :步长 # 2~2:从第2行开始且步长为2
s :替换
不加参数,只替换每行匹配到的第一个
g :全局替换
p :显示替换成功的行
i :不区分大小写
w file :将结果写入到file中
e :拼接
c :修改整行
script:
n,m :匹配n~m行
n,+m:匹配第n行及其后m行
1,/root/
/root/,1
/root/,/shutdown/
command:
d :删除
p :打印模式空间
i :行前加入指定内容
a :行后加入指定内容
c :覆盖整行
= :输出行号
!:取反
w file :写入文件file
r file :从file中读取
示例
[root@test1 test]# cat datafile
northwest NW Charles Main 3.0 .98 3 34
western WE Sharon Gray 5.3 .97 5 23
southwest SW Lewis Dalsass 2.7 .8 2 18
southern SO Suan Chin 5.1 .95 4 15
southeast SE Patricia Hemenway 4.0 .7 4 17
eastern EA TB Savage 4.4 .84 5 20
northeast NE AM Main Jr. 5.1 .94 3 13
north NO Margot Weber 4.5 .89 5 9
central CT Ann Stephens 5.7 .94 5 13
1、只打印找到north的行
[root@test1 test]# sed -n '/north/ p' datafile
2、删除第三行,其余行输出到屏幕
[root@test1 test]# sed '3 d ' datafile
从第3行到最后一行都删除,将剩余部分输出到屏幕
[root@test1 test]# sed '1,2 !d' datafile
[root@test1 test]# sed '3,$ d' datafile
3、找到datafile中的所有west并替换成north,将替换后的内容输出到屏幕。
[root@test1 test]# sed -n 's/west/north/gp' datafile
4、所有的Hemenway所在的位置都用Jones来取代,而且只有改变的行被打印
[root@test1 test]# sed -n 's/Hemenway/Jones/gp' datafile
5、打印在west和east之间的模式范围内所有行
[root@test1 test]# sed -n '/west/,/east/ p' datafile
6、把指定的行north写入到一个文件newfile中
[root@test1 test]# sed '/north/ w newfile' datafile
7、在north行后添加“central CT Ann Stephens 5.7 .94 5 13”
[root@test1 test]# sed '/north/ a central CT Ann Stephens 5.7 .94 5 13' datafile
1、删除/etc/grub2.conf文件中所有以空白开头的行行首的空白字符
[root@test1 Test]# sed 's/^ *//g' grub2.cfg
2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
[root@test1 Test]# cp /etc/fstab ./
3、在/root/install.log每一行行首增加#号
[root@test1 Test]# sed -r 's/^# +//g' fstab
4、在/etc/fstab文件中不以#开头的行的行首增加#号
[root@test1 Test]# sed -r 's/^[^#].*/#&/ ' fstab
5、利用sed 取出ifconfig命令中本机的IPv4地址
[root@test1 Test]# ifconfig >ifconfig
[root@test1 Test]# sed -n '2 p' ifconfig |sed -r 's/inet (.*) net.*/\1/'
sed高级命令
sed有两个内置的存储空间:在使用高级命令的时候会用到
模式空间:sed内置的一个缓冲区,用来存放,修改从输出文件中读取的内容
保持空间:sed内置的另一个缓冲区,用来存放临时的数据,sed可以 在保持空间和模式空间交换数据,但不能在保持空间执行普通sed命令。
sed 在每次循环读取数据是,模式空间会被清空,但保持空间的内容不变
编辑命令:
h :把模式空间中的内容覆盖至保持空间中
H :把模式空间中的内容追加至保持空间中
g :把保持空间中的内容覆盖至模式空间中
G :把保持空间中的内容追加至模式空间中
x :把模式空间中的内容与保持空间中的内容互换
n :覆盖读取匹配到的行的下一行至模式空间中
N :追加读取匹配到的行的下一行至模式空间中
d :删除模式空间中的行
D :删除多行模式空间中的所有行
示例
1. 把test中的1-3行复制到文件末尾
[root@test1 Test]# sed '1,3H;$G' test
2. 把第二行复制到第四行
[root@test1 Test]# sed '2H;4G' test
注:上面两个示例中,用的H,故模式空间本来会有一个空行,所以添加的内容和源文件中会以空格分开,下面的用h,会覆盖原保持空间内容,故没有空格分开
3. 把第行剪切到文件末尾
[root@test1 Test]# sed '1h;$G' test
4. 把1-2行剪切到文件末尾
[root@test1 Test]# sed '1h;2H;$G' test
5. 把/etc/passwd中能登录的用户及密码(/etc/shadow)提取出来写入user-passwd.txt文件中
(1)先取出/etc/passwd中以bash结尾的行
[root@test1 Test]# sed -n '/bash$/p' /etc/passwd
(2)将上面取出的行截取出来用户名,写入user中
[root@test1 Test]# sed -n '/bash$/p' /etc/passwd|sed -nr 's/(^[^:]*).*/\1/p' >user
(3)根据user文件中的用户名截取/etc/shadow中的密码串
[root@test1 Test]# sed -nf user /etc/shadow
(4)截取(3)取出行的密码串
[root@test1 Test]# sed -nf user /etc/shadow | sed -nr 's/^([^:]*:)(.*)/\2/p'| sed -nr 's/^([^:]*).*/\1/p'
注:这样虽然账号和密码都取出来了,但是没有写入文件中,写入文件会很麻烦,通过写shell脚本来实现
[root@test1 Test]# vim user_passwd.sh
1 #!/bin/bash
2 sed -n '/bash$/ s/:.*//w user.txt' /etc/passwd
3 > user-passwd.txt
4 for user in `cat user.txt`
5 do
6 pass=`sed -n "/$user/ p" /etc/shadow | cut -d : -f 2`
7 echo "用户名: $user 密码:$pass" >> user-passwd.txt
8 done
[root@test1 Test]# sh user_passwd.sh
[root@test1 Test]# cat user-pass.txt
用sed将换行符改成空格
[root@nfs01 ~]# echo "a,b,c,d" |sed 's/,/\n/g' |sed ':label;N;s/\n/:/;t label'
------------------------------------------------------------------------------------------------------- 返回目录