20190806-sed面试题
由于比较难,附上PPT,没事还得看
下载:https://www.lanzous.com/i5cs9aj 密码:arka
1、删除centos7系统/etc/grub2.cfg⽂件中所有以空⽩开头的⾏⾏⾸的空⽩字符。
sed -r 's/[1]+//' /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❌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
[:blank:] ↩︎
20190806-sed面试题的更多相关文章
- sed 面试题
#oldboy my qq num is 49000448.$ not 4900000448. my god ,i am not oldbey,but clsn!$ #oldboy my name i ...
- awk与sed命令面试题整理
1.sed命令123abc456456def123567abc789789def567要求输出:456ABC123123DEF456789ABC567567DEF789答案:sed -r -i 's# ...
- 企业面试题-find结合sed查找替换
题:把/oldboy目录及其子目录下所有以扩展名.sh结尾的文件中包含oldboy的字符串全部替换成oldgirl 解答: 建立测试数据: [root@tan data]# mkdir /oldboy ...
- Liunx面试题
答案待定 1.请用shell查询file1 里面空行的所在行号2.编写ShellScript查询file1 以abc 结尾的行3.打印出file1 文件第1 到第3 行4.如何将本地80 端口的请求转 ...
- Hadoop 之面试题
颜色区别: 蓝色:hive,橙色:Hbase.黑色hadoop 请简述hadoop怎样实现二级排序. 你认为用Java,Streaming,pipe 方式开发map/reduce,各有哪些优缺点: 6 ...
- Linux面试题汇总答案
转自:小女生的Linux技术~~~Linux面试题汇总答案~~ 一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的 ...
- 收藏所用C#技术类面试、笔试题汇总
技术类面试.笔试题汇总 注:标明*的问题属于选择性掌握的内容,能掌握更好,没掌握也没关系. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补 ...
- JSP基本面试的试题
JSP基本面试的试题 1.jsp有哪些内置对象作用分别是什么 答:JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应): request 用户端请求,此请求会包含来自GET/PO ...
- 嵌入式Linux C笔试题积累(转)
http://blog.csdn.net/h_armony/article/details/6764811 1. 嵌入式系统中断服务子程序(ISR) 中断是嵌入式系统中重要的组成部分,这导致了很 ...
- linux运维面试题
一.有文件file1 1.查询file1 里面空行的所在行号 grep -n "^#" file1 or awk ‗{if($0~/^$/)print NR}‘ file or g ...
随机推荐
- Nmon监控结果分析
一:CPU信息 SYS_SUMM图表: 1.折线图中蓝线为cpu占有率变化情况:粉线为磁盘IO的变化情况: 2.下面表各种左边的位磁盘的总体数据,包括如下几个: Avg tps during an i ...
- 聊聊db和缓存一致性的5种实现方式
数据存储在数据库中,为了加快业务访问的速度,我们将数据库中的一些数据放在缓存中,那么问题来了,如何确保db和缓存中数据的一致性呢?我们列出了5种方法,大家都了解一下,然后根据业务自己选择. 方案1 获 ...
- php中代码执行&&命令执行函数
转自i春秋 1.eval()函数 #传入的参数必须为PHP代码,既需要以分号结尾. #命令執行:cmd=system(whoami); #菜刀连接密码:cmd <?php @eval($_POS ...
- phpstorm中xdebug配置和断点调试
今天赶紧把这个搞了 运行环境: phpStorm 2018 PHP 5.45 nts VC9 Xdebug 2.4.1 0x01 PHP安装xdebug扩展 自己之前装一直没装起 原来是版本问题 下 ...
- Spring Boot 2.X(十):自定义注册 Servlet、Filter、Listener
前言 在 Spring Boot 中已经移除了 web.xml 文件,如果需要注册添加 Servlet.Filter.Listener 为 Spring Bean,在 Spring Boot 中有两种 ...
- 5分钟彻底理解Redis持久化
Redis持久化 RDB快照 在默认情况下,Redis将内存数据库快照保存到dump.rdb的二进制文件中. 可以对Redis进行设置,让它在"N秒内数据集至少有N个改动", 这一 ...
- 高性能封装检测浏览器支持css3属性函数
css3出来已经很久了,现在来谈判断浏览器是否支持某个css3的属性虽说有点过时了,但是还是可以谈谈的,然后,此篇主要谈的不是判断是否支持,而是怎么封装更好,为什么这么封装,欢迎吐槽. 入题,判断浏览 ...
- 快速排序方法——python实现
参考博文:http://www.cnblogs.com/jingmoxukong/p/4302891.html 快速排序是一种交换排序. 快速排序由C. A. R. Hoare在1962年提出. 它的 ...
- ES和zookeeper选取帮主之江湖秘闻
ES帮会 某日,ES帮会中决定选取老大统领帮会走向辉煌.大家七嘴八舌,讨论方案,场面一顿混乱.傻牛站起来大喊一声:谁比俺力气大,谁就当老大.(ES集群在启动时,选取集群master,按照nodeId进 ...
- Unity常用协程功能封装
# 1.前言unity开发过程中,经常用到一些特定的协程功能,比如延时触发.等待触发.重复操作等.unity自带Invoke以及InvokeRepeating方法,但这些方法均采用反射机制,性能消耗, ...