文本编辑器:
vi vim nano gedit emacs
...
txt doc
...
以上编辑器的共同特点就是需要先将文件打开然后再去编辑;交互式的
sed(流编辑器|行编辑器):非交互式
1、通过非交互
3去修改文件内容,默认情况,不会直接修改源文件
2、sed用来将文档或者字符串经过一系列的编辑命令转换成另外一种格式输入
3、sed通过用来匹配一个或者多个正则表达式来进行文本处理
sed工具有两种模式,命令模式|脚本模式
命令行模式:
格式:
sed 选项
'命令清单[地址定位]' filename
说明:引用shell中的变量时,需要使用双引号
选项:
-e:多次编辑处理
-n:取消默认输出(加的话会显示执行命令的结果,不加的话显示所有的)
-f:指定sed的脚本文件
-r:使用扩展正则表达式
-i:直接修改源文件(谨慎使用)
命令清单:
a\(a):在当前行后添加一行或多行
i\(i):在当前行前添加一行或多行
c\(c):用命令后的内容替换当前行
p:打印行
d:删除行
r:读取文件内容
w:将所选行写入文件
!:对所选行以外的行进行处理(取反)
s:搜索替换
g:全局替换
y:将字符替换成另一个字符(一对一),不能对正则应用y命令 类似tr
&:保存查找字符串方便在替换中引用
=:打印行号
地址定位:
x:指定x行 sed -n
'2p' file
x,y:指定x行到y行 sed -n
'1,5p' file
/key/:查找包含key关键字的行 sed -n
'/root/p' file
/key1/,/key2/:查找key1和key2关键字之间的行 sed -n
'/root/,/ftp/p' file
/key1/,x:匹配从关键字行开始到第x行之间的行 sed -n
'/^root/,3p' file
x,/key/:匹配从第x行开始到关键字的行 sed -n
'5,/root/p' file
x,y!:匹配从x行到y行以外的行 sed -n
'1,5!p' file
示例:
1017 sed
'' 1.txt
1018 sed
1.txt
1019 sed
'' 1.txt
1020 sed
'1p' 1.txt
1021 sed -n
'1p' 1.txt
1022 sed -n
'4p' 1.txt
1023 sed -n
'$p' 1.txt
1024 tail -
1 1.txt
1025 cat
1.txt
1026 sed
'2d' 1.txt
1027 sed
'1,5d' 1.txt
1028 sed
'$d' 1.txt
1029 cat
1.txt
1030 sed
'ahello world' 1.txt
1031 sed
'3ahello world' 1.txt
1032 sed
'1,3ahello world' 1.txt
1033 sed
'5a\hello
1034 sed '5a\hello\
world\
999' 1.txt
1035 sed '3a\hello world
' 1.txt
1036 cat 1.txt
1037 sed '$ahello world
' 1.txt
1038 sed 'i88888888
' 1.txt
1039 sed '$i88888888
' 1.txt
1040 sed '1,2i88888888
' 1.txt
1041 sed '1,
2i\hello\
world\
hello\
end
' 1.txt
1042 cat 1.txt
1043 sed '/root/cwelcome to uplooking
' 1.txt
1044 sed '/root/c\welcome to uplooking
' 1.txt
1045 sed '5c\aaaaaaaa
' 1.txt
1046 sed '$c\aaaaaaaa
' 1.txt
1047 cat 1.txt
1048 sed '/root/r /etc/hosts
' 1.txt
1049 sed '/root/r/etc/hosts
' 1.txt
1050 sed '/root/5r/etc/hosts
' 1.txt
1051 sed '5r /etc/hosts
' 1.txt
1052 cat 1.txt
1053 sed '/root/w
2.txt
' 1.txt
1054 ll
1055 cat 2.txt
1056 sed '1,5w
2.txt
' 1.txt
1057 ll
1058 cat 2.txt
1059 sed -n '1,3p
' 1.txt
1060 sed -n '1,
3!p
' 1.txt
1061 sed -n '/root/p
' 1.txt
1062 sed -n '/root!/p
' 1.txt
1063 sed -n '/root/!p
' 1.txt
1064 cat 1.txt
1065 sed 's/root/hello
' 1.txt
1066 sed -n 's/root/hello/p
' 1.txt
1067 sed 's/root/hello/p
' 1.txt
1068 sed -n 's/root/hello/gp
' 1.txt
1069 sed -n 's/root/hello/p
' 1.txt
1070 cat 1.txt
1071 sed -n '5s
1072 sed
'5s#/sbin/nologin#uplooking#gp' 1.txt
1073 sed
's/\/sbin\/nologin/uplooking/gp' 1.txt
1074 sed -n
's/\/sbin\/nologin/uplooking/gp' 1.txt
1075 cat
1.txt
1076 sed -n
'1y/root/ROOT/gp' 1.txt
1077 sed -ne
'1y/root/ROOT/gp' 1.txt
1078 sed
'1yroot/ROOT' 1.txt
1079 man sed
1080 sed -n
'y/root/ROOT/p' 1.txt
1081 sed -n
'y/root/ROOT/' 1.txt
1082 sed -ne
'y/root/ROOT/' 1.txt
1083 sed
'y/root/ROOT/' 1.txt
1084 vim
1.txt
1085 sed
'y/root/ROOT/' 1.txt
1086 sed
'y/rot/ROT/' 1.txt
1087 sed
'y/roat/ROT/' 1.txt
1088 cat
1.txt
1089 sed
'y/[a-z:]/[A-Z@]/' 1.txt
1090 sed
'y/[a-z]/[A-Z]/' 1.txt
1091 sed
'y/[0-9]/[A-Z]/' 1.txt
1092 sed
'y/[abcd]/[ABCD]/' 1.txt
1093 cat
1.txt
1094 sed
'1y/:/@/' 1.txt
1095 sed
'1,5y/:/@/' 1.txt
1096 sed -n
'1,5y/:/@/p' 1.txt
1097 sed
'1,5y/:/@/p' 1.txt
1098 sed
'1,5y/:/@/' 1.txt
1099 cat
1.txt
1100 sed -n
'1,5p' 1.txt
1101 sed -n
'1,5/#&p' 1.txt
1102 sed -n
'1,5#&p' 1.txt
1103 cat
1.txt
1104 sed
'/root/#&' 1.txt
1105 sed -n
'/root/#&p' 1.txt
1106 sed -n
's/root//p' 1.txt
1107 sed -n
's/root/#&/p' 1.txt
1108 sed -n
'1,5s/^/#&/p' 1.txt
1109 sed -n
'1,5s/^//p' 1.txt
1110 sed -n
'1,5s/$//p' 1.txt
1111 grep -n root passwd
1112 sed -e
'/root/=' -e
'/root/p' 1.txt
1113 sed -ne
'/root/=' -ne
'/root/p' 1.txt
1114 sed -ne
'/root/=' -ne
'/root/p' passwd
说明:以上历史命令有错有对,自己先理解再测试
课堂练习:
cp /etc/passwd /tmp/
1、将文件中任意数字替换成空或者制表符
1222 sed -n
's/[0-9]//gp' passwd |head
1223 sed -n
's/[0-9]/\t/gp' passwd |head
2、去掉文件
1-
5行的数字、冒号、斜杠
3、匹配root关键字的行或者关键字替换成hello abc,并保存到test.txt文件中
4、删除vsftpd.conf、smb.conf、man.cf配置文件中所有注释掉的行(只保留有用的行),不要直接修改源文件
5、使用sed命令截取自己的ip地址
6、截取自己的ip、广播、子网掩码,显示如下:
IP地址是:
广播地址是:
子网掩码是:
7、注释掉文件的
2-
3行和匹配到以root开头或者以ftp开头的行
作业:
1、写一个初始化系统的脚本
1> 自动修改主机名(如:IP是
10.1.1.10,则主机名改为server10.uplook.com)
2> 自动配置可用的yum源
3> 自动关闭防火墙和selinux
2、写一个自动搭建ftp服务的脚本,要求如下:
1> 不支持本地用户登录
2> 匿名用户可以上传、新建、删除
3> 匿名用户的限速为500kbps