第四周sed一章面试题

<font color=red>由于比较难,附上PPT,没事还得看</font>

下载:https://www.lanzous.com/i5cs9aj 密码:arka


1、删除centos7系统/etc/grub2.cfg⽂件中所有以空⽩开头的⾏⾏⾸的空⽩字符。

sed -r 's/^[[:blank:]]+//' /etc/grub2.cfg

2、删除/etc/fstab⽂件中所有以#开头,后⾯⾄少跟⼀个空⽩字符的⾏的⾏⾸的# 和空⽩字符。

[root@centos7 ~]# sed -r 's/^#[[:space:]]+//g' /etc/fstab

3、在centos6系统/root/install.log每⼀⾏⾏⾸增加#号。

[root@qqq tmp]# sed 's/^/#/g' /root/install.log

4、在/etc/fstab⽂件中不以#开头的⾏的⾏⾸增加#号。

[root@centos7 ~]# sed -r 's/^[^#]/#&/g' /etc/fstab

[root@centos7 ~]# sed -r '/^[^#]/s@^@#@' /etc/fstab

5、处理/etc/fstab路径,使⽤sed命令取出其⽬录名和基名。

[root@centos7 ~]# echo "etc/fstab/dd/" | sed -r 's@^(.*)/(.+)$@\1@'
[root@centos7 ~]# echo "etc/fstab/dd/" | sed -r 's@^(.*)/(.+)$@\2@'
dd/

6、利⽤sed 取出ifconfig命令中本机的IPv4地址

[root@centos7 ~]# ifconfig eth0 | sed -rn '/netmask/s#.*net (.*)  net.*#\1#p'
192.168.38.128

7、统计centos安装光盘中Package⽬录下的所有rpm⽂件的以.分隔倒数第⼆个字段的重复次数。

[root@centos7 ~]# ls /misc/cd/Packages/*.rpm | sed -r 's/.*\.(.*)\.rpm/\1/g' | sort | uniq -c | sort -rn
   2311 x86_64
    928 noarch
      4 i686

8、统计/etc/init.d/functions⽂件中每个单词的出现次数,并排序(⽤grep和 sed两种⽅法分别实现)。

[root@centos7 ~]# egrep -o "\<[[:alpha:]]+\>"   /etc/init.d/functions  | sort | uniq -c | sort -n

9、将⽂本⽂件的n和n+1⾏合并为⼀⾏, n为奇数⾏

[root@centos7 ~]#  seq 10 | sed "1~2N;s/\n/ /" 
1 2
3 4
5 6
7 8
9 10

面试题

1、linux系统中,命令可以从文本文件的每一行中截取指定的内容的数据。

cut,awk

2、在每一行后增加一空行?

[root@centos7 ~]# sed G /etc/fstab

[root@qqq tmp]# sed -r 's/$/\n/' /etc/passwd

3、在匹配regex的行之后插入一空行?

[root@centos7 ~]# sed '/regex/G' A.txt

[root@qqq tmp]# sed -r '/root/s@$@\n@' /etc/passwd

4、计算文件行数?

[root@centos7 ~]# wc -l /etc/passwd

6、sed将文件test中第50行中的haiwao改为haiwai?

[root@centos7 ~]# sed '50s/haiwao/haiwai/g' test

7、替换一个文件/etc/passwd里的这root:x:0:0:root:/root:/bin/bash一行第二个root为test?

cat /etc/passwd| sed '/^root/!d'|sed 's/root/test/2'

[root@qqq tmp]# sed /^root/p -n /etc/passwd | sed 's/root/test/2'
root:x:0:0:test:/root:/bin/bash

8、打印/etc/passwd的奇数行?

[root@qqq tmp]# sed 1~2p /etc/passwd -n


实验

1、利⽤sed 取出ifconfig ens33命令中本机的IPv4地址

[qqq@ubutnu ~]$ ifconfig ens33 | sed -n 2p | sed -r 's/.*inet (.*)  net.*/\1/'
192.168.38.130

2、删除/etc/fstab⽂件中所有以#开头,后⾯⾄少跟⼀个空⽩字符的⾏的⾏⾸的#和空⽩字符

[qqq@ubutnu ~]$ sed 's/^#[[:space:]+]//'  /etc/fstab

3、把/etc/httpd/conf/httpd.conf⽂件内的Linsten 80改为Listen 8081

sed -i 's/Listen 80/Listen 81/g' /etc/httpd/conf/httpd.conf

4、把pets⽂件中所有的dog修改为cat

sed 's/dog/cat/g' pets -i

5、删除pets⽂件中的第2⾏

sed 2d pets -i

6、仅显⽰pets⽂件的第2⾏

sed -n 2p pets

7、把pets⽂件的第2⾏显⽰2遍

sed 2p pets

8、显⽰pets⽂件的最后1⾏

sed '$p' pets -n

9、显⽰pets⽂件中包含dog字符串的所有的⾏

sed /dog/p pets -n

10、显⽰pets⽂件中,包含2或4的⾏之间的所有⾏

[root@centos7 ~]# sed -r '/2/,/4/p'  pets -n
c2aaadog
4
ddog2

sa 4
2

a

11、显⽰pets⽂件中,第1⾏到第3⾏之间的所有⾏

[root@centos7 ~]# sed 1,3p pets -n

12、显⽰pets⽂件中第2⾏及后⾯的1⾏

[root@centos7 ~]# sed 2,+1p pets -n

13、显⽰pets⽂件中第1⾏和dog字符串之间的⾏

[root@centos7 ~]# sed -nr '1,/dog/p' pets 
a
  a
    Q   A
    regex
dd
b
c2aaadog

14、显⽰pets⽂件的奇数⾏

[root@centos7 ~]# sed 1~2p pets -n

15、显⽰pets⽂件的偶数⾏

[root@centos7 ~]# sed 2~2p pets -n

16、在pets⽂件的第2⾏的下⼀⾏添加hello

[root@centos7 ~]# sed '2a hello' pets -i

17、在pets⽂件的第2⾏的下⼀⾏添加2⾏内容为hello和world

[root@centos7 ~]# sed '2a hello\nworld' pets -i

18、在pets⽂件的第2⾏的前⼀⾏添加2⾏内容为hello和world

[root@centos7 ~]# sed '2i hello\nworld' pets -i

19、把pets⽂件的第2⾏替换为hello

[root@centos7 ~]# sed '2c hello' pets -i
[root@centos7 ~]# seq 4 | sed '2c hello' 
1
hello
3
4

20、把pets⽂件的第1-3⾏内容,另存为test.txt⽂件

[root@centos7 ~]# sed 1,3p pets -n > test.txt

21、在第2⾏后读⼊test.txt⽂件

[root@centos7 ~]# sed '2r test.txt' pets

22、不显⽰第2⾏

[root@centos7 ~]# sed '2!p' test.txt -n

23、把pets⽂件中的每⾏内容前都编序号显⽰

[root@centos7 ~]# cat -n pets
[root@centos7 ~]# sed  '=' pets

猜你喜欢

转载自blog.51cto.com/14012942/2427099