Linux培训
达内IT学院
400-996-5531
今天小编要跟大家分享的文章是关于Linux运维教程之处理文本的工具sed。正在从事Linux相关工作的小伙伴们来和小编一起看一看吧,希望本篇文章能够对大家有所帮助。
sed是一种流编辑器,它一次处理一行内容。处理时,把当前处理的行存储在临时 缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的 内容,处理完成后,把缓冲区的内容送往屏幕。然后读入下行,执行下一个循环。如果没有使诸如‘D’的特殊命令,那会在两个循环之间清空模式空间,但不会清 空保留空间。这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重 定向存储输出。
功能:主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等
参考:#/software/sed/manual/sed.html
用法:sed [option] '地址定界编辑命令' inputfile
一、常用选项:
-n:不输出模式空间内容到屏幕,即不自动打印
-e: 多点编辑
-f:/PATH/SCRIPT_FILE: 从指定文件中读取编辑脚本
-r: 支持使用扩展正则表达式
-i.bak: 备份文件并原处编辑
二、 地址定界:
#:#表示数字
$:$表示最后一行;
/regex/:任何能被正则表达式regex所匹配到的行(/可以换成其他符号做为边界符)
#,/regex/:从第#行开始,到第一次被regex所匹配到的行结束,中间的所有行;
#1,#2:从#1行到#2行/regex1/,/regex2/:从第一次被/regex1/匹配到的行开始到第一次被/regex2/匹配到的行结束,中间的所有行
#,+n:从第#行开始,一直到向下的n行
first~step:指定起始行,以及步长如2~2 就是从第二行开始,每两行显示一次即偶数行2,4,6,8。1~2奇数行
三、sed编辑命令:
· d:删除模式空间匹配的行,并立即启用下一轮循环
· p:打印当前模式空间内容,追加到默认输出之后
· a []text:在指定行后面追加文本,支持使用\n实现多行追加
· i []text:在行前面插入文本
· c []text:替换行为单行或多行文本
· w /path/to/somefile:保存模式匹配的行至指定文件
· r /path/from/somefile:读取指定文件的文本至模式空间中匹配到的行后
· !:模式空间中匹配行取反处理
四、查找替换
· s///:查找替换,支持使用其它分隔符,s@@@,s### 替换标记:
§ g: 行内全局替换
§ p: 显示替换成功的行
§ w /PATH/TO/SOMEFILE:将替换成功的行保存至文件中
· sed ‘2p’ /etc/passwd 显示第2行,默认会打印所有行,第二行重复打印
· sed –n ‘2p’ /etc/passwd 只打印第二行
· sed –n ‘1,4p’ /etc/passwd 只打印第一行到第四行
· sed –n ‘/root/p’ /etc/passwd 只打印被root匹配到的行
· sed –n ‘2,/root/p’ /etc/passwd 打印从2行开始到被root匹配到的行
· sed -n ‘/^$/=’ file 显示空行行号
· sed –n –e ‘/^$/p’ –e ‘/^$/=’ file
· sed ‘/root/a\superman’ /etc/passwd 在被模式匹配到的行后添加新行superman
· sed ‘/root/i\superman’ /etc/passwd 行前
· sed ‘/root/c\superman’ /etc/passwd 用superman代替被模式匹配到的行
· sed ‘/^$/d’ file
· sed ‘1,10d’ file
· nl /etc/passwd | sed ‘2,5d’
· nl /etc/passwd | sed ‘2a tea’
· sed 's/test/mytest/g' example
· sed –n ‘s/root/&superman/p’ /etc/passwd 单词后 - sed –n ‘s/root/superman&/p’ /etc/passwd 单词前 - sed -e ‘s/dog/cat/’ -e ‘s/hi/lo/’ pets
· sed –i.bak ‘s/dog/cat/g’ pets
五、 高级编辑命令
· P:打印模式空间开端至\n内容,并追加到默认输出之前
· h: 把模式空间中的内容覆盖至保持空间中
· H:把模式空间中的内容追加至保持空间中
· g: 从保持空间取出数据覆盖至模式空间
· G:从保持空间取出内容追加至模式空间
· x: 把模式空间中的内容与保持空间中的内容进行互换
· n: 读取匹配到的行的下一行覆盖至模式空间
· N:读取匹配到的行的下一行追加至模式空间
· d: 删除模式空间中的行
· D:如果模式空间包含换行符,则删除直到第一个换行符的模式空间中的文本,并不会读取新的输入行,而使用合成的模式空间重新启动循环。如果模式空间 不包含换行符,则会像发出d命令那样启动正常的新循环
练习 1、删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符
cat /etc/grub2.cfg |sed -r 's/^[[:space:]]+//'
2、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的# 和空白字符
cat /etc/fstab |sed -r 's/^#[[:space:]]+//'
3、在centos6系统/root/install.log每一行行首增加#号
cat ./install.log |sed -r 's/^(.*)/#\1/'
4、在/etc/fstab文件中不以#开头的行的行首增加#号
cat /etc/fstab |sed -r 's/^([^#]+)/#\1/'
5、处理/etc/fstab路径,使用sed命令取出其目录名和基名
echo "/etc/fstab/sysconfig" |sed -r 's/^(.*)(\/)([^/]+\/?$)/\3/'
echo "/etc/fstab/sysconfig" |sed -r 's/^(.*)(\/)([^/]+\/?$)/\1/'
6、利用sed 取出ifconfig命令中本机的IPv4地址
ifconfig ens33|sed -nr 's/^(.* inet)(.*)( netmask.*)/\2/p'
7、统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个 字段的重复次数
ls|sed -r 's/^(.*\.)([^.]+)(\.rpm$)/\2/'|sort|uniq -c|sort -nr
8、统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和 sed两种方法分别实现)
cat /etc/init.d/functions|grep -oE "[[:alnum:]]+\\>"|sort |uniq -c|sort -n
cat /etc/init.d/functions|sed -r 's/[^[:alnum:]]+/ /gp'|sed -nr 's/[[:space:]]+/\n/gp'|sed '/^$/d'|sort|uniq-c|sort -n
9、将文```本文件的n和n+1行合并为一行,n为奇数行
seq 1 10|sed 'N;s/\n//g'
10. 用sed取出ifconfig 中的IPV4地址
[root@CentOS7 data]# ifconfig ens33 |sed
'2!d;s/^.*inet //;s/ netmask.*//'
192.168.110.131
[root@CentOS7 data]# ifconfig ens33 |sed -n '2p'|sed -e 's/^.*inet //' -e 's/ netmask.*//'
192.168.110.131
[root@CentOS7 data]# ifconfig ens33 |sed -n 's/^.*inet //pg'|sed -n 's/ etmask.*//pg'
192.168.110.131
11 将/etc/fstab 下不是#开头的,开头加上#号
cat /etc/fstab|sed -r 's/^([^#])/#\1/'
cat /etc/fstab|sed 's/^[^#]/#&/'
六、sed练习题目
1.用sed取出ifconfig中的ipv4地址
[root@CentOS7 ~]# ifconfig|sed -nr '2s/^(.*inet )(.*)( netmask .*)$/\2/p'
192.168.110.131
2.显示/etc/passwd的最后一行
[root@CentOS7 ~]# awk 'END{print$0}' /etc/passwd
user6:x:4063:4067::/home/user6:/bin/bash
[root@CentOS7 ~]# tail -n1 /etc/passwd
user6:x:4063:4067::/home/user6:/bin/bash
[root@CentOS7 ~]# sed -n '$p' /etc/passwd
user6:x:4063:4067::/home/user6:/bin/bash
3.显示/etc/passwd的第一行到第三行
[root@CentOS7 ~]# awk '{if (NR>=1 && NR<=3) print $0}' /etc/passwd
##print 直接后跟变量名,不用加$,$0可以省略,if和()之间有无空格都可,()和print之间也一样
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@CentOS7 ~]#cat /etc/passwd | awk '{if(NR>=1 && NR<=3)print}'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@CentOS7 ~]# head -n3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
[root@CentOS7 ~]# cat /etc/passwd |sed -n '1,3p'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
4.显示/etc/fstab 中以U开头的行
[root@CentOS7 ~]# cat /etc/fstab |grep "^U"
UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
[root@CentOS7 ~]# cat /etc/fstab |sed -nr '/^U/p'
UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
[root@CentOS7 ~]# cat /etc/fstab |awk '/^U/{print}'
UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
5.用sed的regex只显示ifconfig的第二行
[root@CentOS7 ~]# ifconfig |sed -n '2p'
inet 192.168.110.131 netmask 255.255.255.0 broadcast192.168.110.255
[root@CentOS7 ~]# ifconfig |head -n2 |tail -n1
inet 192.168.110.131 netmask 255.255.255.0 broadcast192.168.110.255
[root@CentOS7 ~]# ifconfig |awk '{if (NR==2); print$0}' ##如果NR==2,后面什么都没跟,最后打印所有的行了,这里分号表示if语句结束了
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu1500
inet 192.168.110.131 netmask 255.255.255.0 broadcast 192.168.110.255
inet6 fe80::6771:3c83:1113:d701 prefixlen 64scopeid 0x20<link>
ether 00:0c:29:8e:b3:16 txqueuelen 1000(Ethernet)
RX packets 137 bytes 16052 (15.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 59 bytes 9024 (8.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0collisions 0
ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu1500
inet 172.20.129.161 netmask 255.255.0.0 broadcast172.20.255.255
inet6 fe80::4318:d843:7a34:9275 prefixlen 64scopeid 0x20<link>
ether 00:0c:29:8e:b3:20 txqueuelen 1000(Ethernet)
RX packets 22409 bytes 2432535 (2.3 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5029 bytes 633655 (618.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 50 bytes 5084 (4.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 50 bytes 5084 (4.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0collisions 0
[root@CentOS7 ~]# ifconfig |awk '{if (NR==2) print$0}'
inet 192.168.110.131 netmask 255.255.255.0 broadcast 192.168.110.255
6.只显示磁盘利用率
[root@CentOS7 ~]# df |awk '/\/dev\/sd/{print $1,$5}' ##$1和$5之间用,分隔,就表示空格
/dev/sda2 10%
/dev/sda6 1%
/dev/sda3 1%
/dev/sda1 16%
[root@CentOS7 ~]# df |awk '/\/dev\/sd/{print $1" "$5}' ##$1和$5之间加什么,用“内容”
/dev/sda2 10%
/dev/sda6 1%
/dev/sda3 1%
/dev/sda1 16%
[root@CentOS7 ~]# df |awk -F "[ ]+" '/\/dev\/sd/{print $1"="$5}'
/dev/sda2=10%
/dev/sda6=1%
/dev/sda3=1%
/dev/sda1=16%
[root@CentOS7 ~]# df |awk -F "[[:space:]]+" '/\/dev\/sd/{print $5}'
10%
1%
1%
16%
[root@CentOS7 ~]# df |awk -F "[ ]+" '/\/dev\/sd/{print $5}'
10%
1%
1%
16%
7.显示从第一行往后的三行/etc/passwd
[root@CentOS7 ~]# cat /etc/passwd |sed -n '1,+3p'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@CentOS7 ~]# cat /etc/passwd |awk '{if(NR>=1 && NR<=4) print}'
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
8.显示/etc/passwd从s开头的行到f开头之间的行
[root@CentOS7 ~]# cat /etc/passwd |sed -n '/^s/,/^f/p'
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
sssd:x:998:996:User for sssd:/:/sbin/nologin
libstoragemgmt:x:997:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
colord:x:996:993:User for colord:/var/lib/colord:/sbin/nologin
gluster:x:995:992:GlusterFS daemons:/var/run/gluster:/sbin/nologin
saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
chrony:x:992:987::/var/lib/chrony:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
unbound:x:991:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
geoclue:x:990:984:User for geoclue:/var/lib/geoclue:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
zhu:x:1000:1000:zhu:/home/zhu:/bin/bash
mandriva:x:4004:3004::/home/mandriva:/bin/bash
fedora:x:2003:2003:Feora Community:/home/fedora:/bin/tcsh
ssd:x:4058:4062::/home/ssd:/bin/bash
memcached:x:987:981:Memcached daemon:/run/memcached:/sbin/nologin
user2:x:4059:4063::/home/user2:/bin/sh
user1:x:4060:4064::/home/user1:/bin/bash
user3:x:4061:4065::/home/user3:/bin/bash
user5:x:4062:4066::/home/user5:/bin/bash
user6:x:4063:4067::/home/user6:/bin/bash
9.显示/etc/passwd 第三行到f开头的行
[root@CentOS7 ~]# cat /etc/passwd |sed -n '3,/^f/p'
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
10.只显示/etc/passwd偶数行
[root@CentOS7 ~]# cat -n /etc/passwd |sed -n '2~2p'
2bin:x:1:1:bin:/bin:/sbin/nologin
4adm:x:3:4:adm:/var/adm:/sbin/nologin
6sync:x:5:0:sync:/sbin:/bin/sync
8halt:x:7:0:halt:/sbin:/sbin/halt
10operator:x:11:0:operator:/root:/sbin/nologin
12ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
14systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
16polkitd:x:999:998:User for polkitd:/:/sbin/nologin
18libstoragemgmt:x:997:994:daemon account forlibstoragemgmt:/var/run/lsm:/sbin/nologin
20colord:x:996:993:User forcolord:/var/lib/colord:/sbin/nologin
22saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
24setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
26pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
28rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
30unbound:x:991:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
32usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
34radvd:x:75:75:radvd user:/:/sbin/nologin
36ntp:x:38:38::/etc/ntp:/sbin/nologin
38gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin
40avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
42tcpdump:x:72:72::/:/sbin/nologin
44mandriva:x:4004:3004::/home/mandriva:/bin/bash
46habse:x:988:982::/home/habse:/bin/nologin
48nginx:x:4029:4031::/home/nginx:/bin/bash
50git:x:4033:4036::/home/git:/bin/bash
52u:x:4036:4041::/home/u:/bin/bash
54tcsh:x:4038:4043::/home/tcsh:/sbin/nologin
56nologin:x:4040:4045::/home/nologin:/sbin/nologin
58basher:x:4042:4047::/home/basher:/sbin/nologin
60tomcat1:x:4047:4035::/home/tomcat1:/bin/bash
62wang:x:4049:4053::/home/wang:/bin/bash
64apache:x:80:80::/var/www:/sbin/nologin
66wan:x:4054:4058::/home/wan:/bin/bash
68wan1233:x:4056:4060::/home/wan1233:/bin/bash
70ssd:x:4058:4062::/home/ssd:/bin/bash
72user2:x:4059:4063::/home/user2:/bin/sh
74user3:x:4061:4065::/home/user3:/bin/bash
76user6:x:4063:4067::/home/user6:/bin/bash
11.只显示/etc/passwd奇数行
[root@CentOS7 ~]# cat -n /etc/passwd |sed -n '1~2p'
1root:x:0:0:root:/root:/bin/bash
3daemon:x:2:2:daemon:/sbin:/sbin/nologin
5lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
7shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
9mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
11games:x:12:100:games:/usr/games:/sbin/nologin
13nobody:x:99:99:Nobody:/:/sbin/nologin
15dbus:x:81:81:System message bus:/:/sbin/nologin
17sssd:x:998:996:User for sssd:/:/sbin/nologin
19rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
21gluster:x:995:992:GlusterFS daemons:/var/run/gluster:/sbin/nologin
23abrt:x:173:173::/etc/abrt:/sbin/nologin
25rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
27chrony:x:992:987::/var/lib/chrony:/sbin/nologin
29nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
31tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
33geoclue:x:990:984:User forgeoclue:/var/lib/geoclue:/sbin/nologin
35qemu:x:107:107:qemu user:/:/sbin/nologin
37gdm:x:42:42::/var/lib/gdm:/sbin/nologin
39sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
41postfix:x:89:89::/var/spool/postfix:/sbin/nologin
43zhu:x:1000:1000:zhu:/home/zhu:/bin/bash
45fedora:x:2003:2003:Feora Community:/home/fedora:/bin/tcsh
47gentoo:x:4028:4029:Gentoo Distribution:/home/gentoo:/bin/csh
49varnish:x:4030:4032::/home/varnish:/bin/bash
51mage:x:4035:4039::/home/mage:/bin/bash
53bash:x:4037:4042::/home/bash:/bin/bash
55testbash:x:4039:4044::/home/testbash:/sbin/nologin
57userbasher:x:4041:4046::/home/userbasher:/bin/bash
59user:x:4043:4048::/home/user:/bin/bash
61mysql1:x:4048:4052::/home/mysql1:/bin/bash
63userdf:x:4050:4054::/home/userdf:/bin/bash
65user112:x:4053:4057::/home/user112:/bin/bash
67wan1:x:4055:4059::/home/wan1:/bin/bash
69usd:x:4057:4061::/home/usd:/bin/bash
71memcached:x:987:981:Memcached daemon:/run/memcached:/sbin/nologin
73user1:x:4060:4064::/home/user1:/bin/bash
75user5:x:4062:4066::/home/user5:/bin/bash
12.删除/etc/passwd第二行到第10行
[root@CentOS7 ~]# cat -n /etc/passwd |sed -n '2,10d;p'
1root:x:0:0:root:/root:/bin/bash
11games:x:12:100:games:/usr/games:/sbin/nologin
12ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
13nobody:x:99:99:Nobody:/:/sbin/nologin
14systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
15dbus:x:81:81:System message bus:/:/sbin/nologin
16polkitd:x:999:998:User for polkitd:/:/sbin/nologin
17sssd:x:998:996:User for sssd:/:/sbin/nologin
18libstoragemgmt:x:997:994:daemon account forlibstoragemgmt:/var/run/lsm:/sbin/nologin
19rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
20colord:x:996:993:User forcolord:/var/lib/colord:/sbin/nologin
21gluster:x:995:992:GlusterFS daemons:/var/run/gluster:/sbin/nologin
22saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
23abrt:x:173:173::/etc/abrt:/sbin/nologin
24setroubleshoot:x:993:990::/var/lib/setroubleshoot:/sbin/nologin
25rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
26pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
27chrony:x:992:987::/var/lib/chrony:/sbin/nologin
28rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
29nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
30unbound:x:991:986:Unbound DNS resolver:/etc/unbound:/sbin/nologin
31tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
32usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
33geoclue:x:990:984:User forgeoclue:/var/lib/geoclue:/sbin/nologin
34radvd:x:75:75:radvd user:/:/sbin/nologin
35qemu:x:107:107:qemu user:/:/sbin/nologin
36ntp:x:38:38::/etc/ntp:/sbin/nologin
37gdm:x:42:42::/var/lib/gdm:/sbin/nologin
38gnome-initial-setup:x:989:983::/run/gnome-initial-setup/:/sbin/nologin
39sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
40avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
41postfix:x:89:89::/var/spool/postfix:/sbin/nologin
42tcpdump:x:72:72::/:/sbin/nologin
43zhu:x:1000:1000:zhu:/home/zhu:/bin/bash
44mandriva:x:4004:3004::/home/mandriva:/bin/bash
45fedora:x:2003:2003:Feora Community:/home/fedora:/bin/tcsh
46habse:x:988:982::/home/habse:/bin/nologin
47gentoo:x:4028:4029:Gentoo Distribution:/home/gentoo:/bin/csh
48nginx:x:4029:4031::/home/nginx:/bin/bash
49varnish:x:4030:4032::/home/varnish:/bin/bash
50git:x:4033:4036::/home/git:/bin/bash
51mage:x:4035:4039::/home/mage:/bin/bash
52u:x:4036:4041::/home/u:/bin/bash
53bash:x:4037:4042::/home/bash:/bin/bash
54tcsh:x:4038:4043::/home/tcsh:/sbin/nologin
55testbash:x:4039:4044::/home/testbash:/sbin/nologin
56nologin:x:4040:4045::/home/nologin:/sbin/nologin
57userbasher:x:4041:4046::/home/userbasher:/bin/bash
58basher:x:4042:4047::/home/basher:/sbin/nologin
59user:x:4043:4048::/home/user:/bin/bash
60tomcat1:x:4047:4035::/home/tomcat1:/bin/bash
61mysql1:x:4048:4052::/home/mysql1:/bin/bash
62wang:x:4049:4053::/home/wang:/bin/bash
63userdf:x:4050:4054::/home/userdf:/bin/bash
64apache:x:80:80::/var/www:/sbin/nologin
65user112:x:4053:4057::/home/user112:/bin/bash
66wan:x:4054:4058::/home/wan:/bin/bash
67wan1:x:4055:4059::/home/wan1:/bin/bash
68wan1233:x:4056:4060::/home/wan1233:/bin/bash
69usd:x:4057:4061::/home/usd:/bin/bash
70ssd:x:4058:4062::/home/ssd:/bin/bash
71memcached:x:987:981:Memcached daemon:/run/memcached:/sbin/nologin
72user2:x:4059:4063::/home/user2:/bin/sh
73user1:x:4060:4064::/home/user1:/bin/bash
74user3:x:4061:4065::/home/user3:/bin/bash
75user5:x:4062:4066::/home/user5:/bin/bash
76user6:x:4063:4067::/home/user6:/bin/bash
13.在/etc/passwd 中1~20行每行后追加新行new line
[root@CentOS7 ~]# cat -n /etc/passwd |sed '1,20a \new line'
1root:x:0:0:root:/root:/bin/bash
new line
2bin:x:1:1:bin:/bin:/sbin/nologin
new line
3daemon:x:2:2:daemon:/sbin:/sbin/nologin
new line
4adm:x:3:4:adm:/var/adm:/sbin/nologin
new line
5lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
new line
6sync:x:5:0:sync:/sbin:/bin/sync
new line
7shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
new line
8halt:x:7:0:halt:/sbin:/sbin/halt
new line
9mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
new line
10operator:x:11:0:operator:/root:/sbin/nologin
new line
11games:x:12:100:games:/usr/games:/sbin/nologin
new line
12ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
new line
13nobody:x:99:99:Nobody:/:/sbin/nologin
new line
14systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
new line
15dbus:x:81:81:System message bus:/:/sbin/nologin
new line
16polkitd:x:999:998:User for polkitd:/:/sbin/nologin
new line
17sssd:x:998:996:User for sssd:/:/sbin/nologin
new line
18libstoragemgmt:x:997:994:daemon account forlibstoragemgmt:/var/run/lsm:/sbin/nologin
new line
19rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
new line
20colord:x:996:993:User forcolord:/var/lib/colord:/sbin/nologin
new line
14.在.bashrc 的Source下面加一行newline
[root@CentOS7 ~]# sed '/Source/a \newline' .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias mancn='man -M /usr/man/zh_CN'
# Source global definitions
newline
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias cdnet='cd /etc/sysconfig/network-scripts'
LANG=en_US.utf8
前面加一行
[root@CentOS7 ~]# sed '/Source/i \newline' .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias mancn='man -M /usr/man/zh_CN'
newline
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias cdnet='cd /etc/sysconfig/network-scripts'
LANG=en_US.utf8
加2行
[root@CentOS7 ~]# sed '/Source/i \newline \nnewline2' .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias mancn='man -M /usr/man/zh_CN'
newline
newline2
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias cdnet='cd /etc/sysconfig/network-scripts'
LANG=en_US.utf8
替换source这行
[root@CentOS7 ~]# sed '/Source/c \newline' .bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias mancn='man -M /usr/man/zh_CN'
newline
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
alias cdnet='cd /etc/sysconfig/network-scripts'
LANG=en_US.utf8
15.把/etc/fstab中UUID开头的行,过渡出来,写到新文件/root/f1中
[root@CentOS7 ~]# cat /etc/fstab |sed '/^UUID/w /root/f1'
#
# /etc/fstab
# Created by anaconda on Wed Jul 18 17:26:27 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
[root@CentOS7 ~]# cat f1
UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
16.在UUID开头的行后面读入/etc/issue文件的内容
[root@CentOS7 ~]# cat /etc/fstab |sed '/^UUID/r /etc/issue'
#
# /etc/fstab
# Created by anaconda on Wed Jul 18 17:26:27 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
the tty is \l
the hostname is \n
the current time is \t
\S
Kernel \r on an \m
UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
the tty is \l
the hostname is \n
the current time is \t
\S
Kernel \r on an \m
UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
the tty is \l
the hostname is \n
the current time is \t
\S
Kernel \r on an \m
UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
the tty is \l
the hostname is \n
the current time is \t
\S
Kernel \r on an \m
UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
the tty is \l
the hostname is \n
the current time is \t
\S
Kernel \r on an \m
17.为被regex匹配到的行打印行号
[root@CentOS7 ~]# cat /etc/fstab |sed '/^UUID/= '
#
# /etc/fstab
# Created by anaconda on Wed Jul 18 17:26:27 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
9
UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
10
UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
11
UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
12
UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
13
UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
18.除了模式空间的行其他行的行号全显示出来
[root@CentOS7 ~]# cat /etc/fstab |sed '/^UUID/d;= '
1
2
#
3
# /etc/fstab
4
# Created by anaconda on Wed Jul 18 17:26:27 2018
5
#
6
# Accessible filesystems, by reference, are maintained under '/dev/disk'
7
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8
#
14
19.除了第3行,其他都打印(有行号)
[root@CentOS7 ~]# cat /etc/fstab |sed '3d;='
1
2
#
4
# Created by anaconda on Wed Jul 18 17:26:27 2018
5
#
6
# Accessible filesystems, by reference, are maintained under '/dev/disk'
7
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8
#
9
UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
10
UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
11
UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
12
UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
13
UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
14
[root@CentOS7 ~]# cat -n /etc/fstab |sed -n '3d;p'
1
2#
4# Created by anaconda on Wed Jul 18 17:26:27 2018
5#
6# Accessible filesystems, by reference, are maintained under '/dev/disk'
7# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8#
9UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
10UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
11UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
12UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
13UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
14
[root@CentOS7 ~]# cat -n /etc/fstab |sed -n '3!p'
1
2#
4# Created by anaconda on Wed Jul 18 17:26:27 2018
5#
6# Accessible filesystems, by reference, are maintained under '/dev/disk'
7# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8#
9UUID=50ad6c96-3d5a-4840-9f52-11f045c46a1d / xfs defaults 0 0
10UUID=755562ec-35e1-4ab9-bd08-8b0a691bde48 /boot xfs defaults 0 0
11UUID=48c79d0b-623e-4a61-8b5e-da5b2f0050b6 /data xfs defaults 0 0
12UUID=7067af7e-e829-49ba-b713-55296c046e27 swap swap defaults 0 0
13UUID=d045c3b8-68ba-4479-9b98-ceec0c8aa526 /home xfs defaults 0 0
14
21.将root替换为ROOT,将r..t替换为haha,在r..t后面加er,如rooter,要求将/etc/default/grub 中的LINUX那行quiet后加net.ifnames=0 GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet"
[root@CentOS7 ~]# cat /etc/passwd |sed -nr 's/r..t\>/ROOT/pg'
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
operator:x:11:0:operator:/ROOT:/sbin/nologin
[root@CentOS7 ~]# cat /etc/passwd |sed -nr 's/r..t\>/haha/pg'
haha:x:0:0:haha:/haha:/bin/bash
operator:x:11:0:operator:/haha:/sbin/nologin
[root@CentOS7 ~]# cat /etc/passwd |sed -nr 's/(r..t\>)/\1er/pg'
rooter:x:0:0:rooter:/rooter:/bin/bash
operator:x:11:0:operator:/rooter:/sbin/nologin
[root@CentOS7 ~]# cat /etc/default/grub|sed -n '/LINUX/s/\"$/ et\.ifnames=0"/p'
GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet net.ifnames=0"
[root@CentOS7 ~]# cat /etc/default/grub|sed -nr 's/^(.*quiet)/\1 et\.ifname=0/p'
GRUB_CMDLINE_LINUX="crashkernel=auto rhgb quiet net.ifname=0"
22.使用sed取出IPV4的地址(至少5种方法)
[root@CentOS7 ~]# ifconfig |head -n2 |tail -n1 |tr -s ' ' |cut -d' ' -f3
192.168.110.131
[root@CentOS7 ~]# ifconfig ens33 |grep -w inet |tr -s ' ' |cut -d" " -f3
192.168.110.131
[root@CentOS7 ~]# ifconfig ens33 |sed -nr 's/^(.*inet )(.*)( etmask.*)$/\2/p'
192.168.110.131
[root@CentOS7 ~]# ifconfig ens33 |awk -F "[[:space:]]+" '{if(NR==2) print $3}'
192.168.110.131
[root@CentOS7 ~]# ifconfig ens33 |awk '{if(NR==2) print $2}'##-F "[[:space:]]+"等价于FS="[ ]+"
192.168.110.131
[root@CentOS7 ~]# ifconfig ens33 |awk '{FS="[ ]+"; if(NR==2) print $3}'
192.168.110.131
[root@CentOS7 ~]# ifconfig ens33 |sed '2!d;s/^.*inet//;s/ netmask.*//'
192.168.110.131
[root@CentOS7 ~]# ifconfig ens33 |sed -nr '2s/.* inet (.*) etmask.*/\1/p'
192.168.110.131
23.将一个命令的执行结果进行压缩,重定向到压缩文件中
[root@CentOS7 ~]# ls |xargs tar zcf root.tar.gz
[root@CentOS7 ~]# tar ft root.tar.gz
1.sh
b
Desktop/
Documents/
Downloads/
f1
f2
initial-setup-ks.cfg
Music/
Pictures/
Pictures/Screenshot from 2018-07-21 08-45-39.png
Public/
root.tar.gz
space.txt
Templates/
test.txt
Videos/
24.多点编辑将/etc/passwd 中空行及空行的行号打印出来
25.取出/etc/sysconfig/network-scripts/ 中的基名和目录名
[root@CentOS7 ~]# basename /etc/sysconfig/network-scripts/
network-scripts
[root@CentOS7 ~]# dirname /etc/sysconfig/network-scripts/
/etc/sysconfig
[root@CentOS7 ~]#echo "/etc/sysconfig/network-scripts/"|grep -Eo "[^/]+\/?$"
network-scripts/
[root@CentOS7 ~]# echo "/etc/sysconfig/network-scripts/"|sed -r 's/^(.*\/)([^/]+\/?)/\2/'
network-scripts/
[root@CentOS7 ~]# echo "/etc/sysconfig/network-scripts/"|sed -r 's/^(.*\/)([^/]+\/?)/\1/'
/etc/sysconfig/
[root@CentOS7 ~]# echo "/etc/sysconfig/network-scripts"|sed -r 's/^(.*\/)([^/]+\/?)/\2/'
network-scripts
26.去掉centos6中的/etc/#/#
#VirtualHost*:80 到#</Vitualhost>中的每行开头的#号
[root@CentOS6 ~]# cat /etc/#/# |sed -r
'/^#<VirtualHost/,/^#<\/VirtualHost/s/#//'
<VirtualHost *:80>
ServerAdmin webmaster@#
DocumentRoot /www/docs/#
ServerName #
ErrorLog logs/#-error_log
CustomLog logs/#-access_log common
</VirtualHost>
27.将/etc/fstab中开头不是#加上#
[root@CentOS6 ~]# cat /etc/fstab |sed -r 's/^([^#].*)/#\1/'
#
# /etc/fstab
# Created by anaconda on Wed Jul 18 17:13:36 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
#UUID=959dbbae-a23d-4e36-a306-00ffeeb0ac35 / ext4 defaults 1 1
#UUID=204cb4a7-64cd-4d54-8757-4abaedf6564c /boot ext4 defaults 1 2
#UUID=31ab6aee-936c-4aa4-92e3-31f6348c976c /data ext4 defaults 1 2
#UUID=ccb69c46-a8b7-46ec-9015-56656319c72a swap swap defaults 0 0
#tmpfs /dev/shm tmpfs defaults 0 0
#devpts /dev/pts devpts gid=5,mode=620 0 0
#sysfs /sys sysfs defaults 0 0
#proc /proc proc defaults 0 0
[root@centos7 ~]# cat /etc/fstab |sed -r 's/^([^#].*)/#&/'
#
# /etc/fstab
# Created by anaconda on Sat Aug 18 23:02:43 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
#UUID=d9c33ebe-7198-496d-9211-468ed2aff31d / xfs defaults 0 0
#UUID=61e1da9b-93da-4aa7-bc62-148e74505ccf /boot xfs defaults 0 0
#UUID=d4001f2b-fe46-4479-aaf4-59ecce51eb95 /data xfs defaults 0 0
#UUID=d0c3c003-2fb6-453c-8c74-fc3c1b1acaf8 swap swap defaults 0 0
28.将/etc/fstab中的小写字母转换成大写字母
[root@centos7 ~]# cat /etc/fstab |sed -r 's/[[:alpha:]]/\u&/g'
#
# /ETC/FSTAB
# CREATED BY ANACONDA ON SAT AUG 18 23:02:43 2018
#
# ACCESSIBLE FILESYSTEMS, BY REFERENCE, ARE MAINTAINED UNDER '/DEV/DISK'
# SEE MAN PAGES FSTAB(5), FINDFS(8), MOUNT(8) AND/OR BLKID(8) FOR MORE INFO
#
UUID=D9C33EBE-7198-496D-9211-468ED2AFF31D / XFS DEFAULTS 0 0
UUID=61E1DA9B-93DA-4AA7-BC62-148E74505CCF /BOOT XFS DEFAULTS 0 0
UUID=D4001F2B-FE46-4479-AAF4-59ECCE51EB95 /DATA XFS DEFAULTS 0 0
UUID=D0C3C003-2FB6-453C-8C74-FC3C1B1ACAF8 SWAP SWAP DEFAULTS 0 0
[root@centos7 ~]# cat /etc/fstab |sed -r 's/[[:alpha:]]/\u&/g'|sed -r 's/[[:alpha:]]/\l&/g'
##\l 表示小写\u表示大写\&引用前面被正则匹配到的
#
# /etc/fstab
# created by anaconda on sat aug 18 23:02:43 2018
#
# accessible filesystems, by reference, are maintained under '/dev/disk'
# see man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
uuid=d9c33ebe-7198-496d-9211-468ed2aff31d / xfs defaults 0 0
uuid=61e1da9b-93da-4aa7-bc62-148e74505ccf /boot xfs defaults 0 0
uuid=d4001f2b-fe46-4479-aaf4-59ecce51eb95 /data xfs defaults 0 0
uuid=d0c3c003-2fb6-453c-8c74-fc3c1b1acaf8 swap swap defaults 0 0
29.将sed的一串代码放到文件中 ’s///‘,从文件中读取正则来执行
[root@centos7 ~]# cat sed.script
s/[[:alpha:]]/\u&/g
[root@centos7 ~]# sed -r -f sed.script /etc/fstab
#
# /ETC/FSTAB
# CREATED BY ANACONDA ON SAT AUG 18 23:02:43 2018
#
# ACCESSIBLE FILESYSTEMS, BY REFERENCE, ARE MAINTAINED UNDER '/DEV/DISK'
# SEE MAN PAGES FSTAB(5), FINDFS(8), MOUNT(8) AND/OR BLKID(8) FOR MORE INFO
#
UUID=D9C33EBE-7198-496D-9211-468ED2AFF31D / XFS DEFAULTS 0 0
UUID=61E1DA9B-93DA-4AA7-BC62-148E74505CCF /BOOT XFS DEFAULTS 0 0
UUID=D4001F2B-FE46-4479-AAF4-59ECCE51EB95 /DATA XFS DEFAULTS 0 0
UUID=D0C3C003-2FB6-453C-8C74-FC3C1B1ACAF8 SWAP SWAP DEFAULTS 0 0
[root@centos7 ~]#
30.将/eec/selinux/config 中的SELINUX=enforcing 改为SELINUX=disabled
[root@centos7 ~]# cat /etc/selinux/config |sed -i 's/SELINUX=enforcing/SELINUX=disabled/'
sed: no input files
[root@centos7 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
[root@centos7 ~]# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
31.取出版本号
[root@centos7 etc]# cat /etc/centos-release|sed -r 's/[^0-9]+([0-9]+).*/\1/'
7
[root@centos7 etc]# cat /etc/centos-release|sed -r 's/[^[:digit:]]+([[:digit:]]+).*/\1/'
7
[root@centos7 etc]# cat /etc/centos-release|awk -F "[ .]" '{print $4}'
32 删除/etc/#/# 以空格+#号开头的无用行,以及空行
[root@CentOS6 ~]# sed '/^$/d;/^[ ]*#/d' /etc/#/#
33将奇数行和偶数行合并
[root@centos7 etc]# seq 1 10|sed -r 'N;s#\n##'
12
34
56
78
910
1.awk 'NR%2==1' file / /显示奇数行
2.sed -n '1,$n;p' file / /显示偶数行
3.sed -n '1,$p;n' file / /显示奇数行
4.奇数行:sed '2~2d' file / /显示奇数行
5.偶数行:sed '1~2d' file/ /显示偶数行
34、删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符
35、删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的# 和空白字符
36、在centos6系统/root/install.log每一行行首增加#号
37、在/etc/fstab文件中不以#开头的行的行首增加#号
38、处理/etc/fstab路径,使用sed命令取出其目录名和基名
39、利用sed 取出ifconfig命令中本机的IPv4地址 7、统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个 字段的重复次数
41、统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和 sed两种方法分别实现)
42、将文本文件的n和n+1行合并为一行,n为奇数行
[root@centos7 etc]# seq 1 10|sed 'N;s/\n//'
以上就是小编今天为大家分享的关于Linux运维教程之处理文本的工具sed的文章,希望本篇文章能够对正在从事Linux运维工作的小伙伴们有所帮助,想要了解更多Linux相关知识记得关注达内Linux培训官网,最后祝愿小伙伴们工作顺利,成为一名优秀的Linux运维工程师。
文章来源:原创 朱友生 SRE学习之路
【免责声明:本文图片及文字信息均由小编转载自网络,旨在分享提供阅读,版权归原作者所有,如有侵权请联系我们进行删除。】
填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!
Copyright © 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有
Tedu.cn All Rights Reserved