由于比较难,附上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

  1. [:blank:] ↩︎

20190806-sed面试题的更多相关文章

  1. sed 面试题

    #oldboy my qq num is 49000448.$ not 4900000448. my god ,i am not oldbey,but clsn!$ #oldboy my name i ...

  2. awk与sed命令面试题整理

    1.sed命令123abc456456def123567abc789789def567要求输出:456ABC123123DEF456789ABC567567DEF789答案:sed -r -i 's# ...

  3. 企业面试题-find结合sed查找替换

    题:把/oldboy目录及其子目录下所有以扩展名.sh结尾的文件中包含oldboy的字符串全部替换成oldgirl 解答: 建立测试数据: [root@tan data]# mkdir /oldboy ...

  4. Liunx面试题

    答案待定 1.请用shell查询file1 里面空行的所在行号2.编写ShellScript查询file1 以abc 结尾的行3.打印出file1 文件第1 到第3 行4.如何将本地80 端口的请求转 ...

  5. Hadoop 之面试题

    颜色区别: 蓝色:hive,橙色:Hbase.黑色hadoop 请简述hadoop怎样实现二级排序. 你认为用Java,Streaming,pipe 方式开发map/reduce,各有哪些优缺点: 6 ...

  6. Linux面试题汇总答案

    转自:小女生的Linux技术~~~Linux面试题汇总答案~~ 一.填空题:1. 在Linux系统中,以 文件 方式访问设备 .2. Linux内核引导时,从文件 /etc/fstab 中读取要加载的 ...

  7. 收藏所用C#技术类面试、笔试题汇总

    技术类面试.笔试题汇总 注:标明*的问题属于选择性掌握的内容,能掌握更好,没掌握也没关系. 下面的参考解答只是帮助大家理解,不用背,面试题.笔试题千变万化,不要梦想着把题覆盖了,下面的题是供大家查漏补 ...

  8. JSP基本面试的试题

    JSP基本面试的试题 1.jsp有哪些内置对象作用分别是什么 答:JSP共有以下9种基本内置组件(可与ASP的6种内部组件相对应):      request 用户端请求,此请求会包含来自GET/PO ...

  9. 嵌入式Linux C笔试题积累(转)

    http://blog.csdn.net/h_armony/article/details/6764811 1.   嵌入式系统中断服务子程序(ISR) 中断是嵌入式系统中重要的组成部分,这导致了很 ...

  10. linux运维面试题

    一.有文件file1 1.查询file1 里面空行的所在行号 grep -n "^#" file1 or awk ‗{if($0~/^$/)print NR}‘ file or g ...

随机推荐

  1. go 学习笔记之解读什么是defer延迟函数

    Go 语言中有个 defer 关键字,常用于实现延迟函数来保证关键代码的最终执行,常言道: "未雨绸缪方可有备无患". 延迟函数就是这么一种机制,无论程序是正常返回还是异常报错,只 ...

  2. VBS定时关闭软件

    用TXT写以下代码另存为VBS格式,代码如下:doset bag=getobject("winmgmts:\\.\root\cimv2")set pipe=bag.execquer ...

  3. NOIP_TG

    本博客主要记录一些在刷题的途中遇到的一些巧妙的题目 砝码称重 一开始想到可以DP递推标记能凑成的数量 但发现同一种砝码可能有多个于是想多开一维状态存当前还剩多少砝码 真是愚蠢至极 直接把所有砝码单独看 ...

  4. Tomcat基本知识(一)

    顶层架构先上一张Tomcat的顶层结构图(图A),如下: Tomcat中最顶层的容器是Server,代表着整个服务器,从上图中可以看出,一个Server可以包含至少一个Service,用于具体提供服务 ...

  5. PHP 组件注册的例子

    <?php namespace Test; abstract class Plugin { protected $pluginName = null; abstract public funct ...

  6. DM7经常使用的命令汇总

    由于DM7兼容oracle ,所以当你不知道某个命令时,大抵就是可以参照oracle的命令及语法,当然有极少的情况会不一样.常用命令如下: 1.连接登录 disql SYSDBA/SYSDBA@223 ...

  7. Python开发【第三篇】数据类型

    1.数字类型 int 整数 1 2 3 float 浮点数 1.1 0.9 0.99 complex 复数 (2+0j) str 字符串 'hello world' "hello world ...

  8. 《深入理解Java虚拟机》-----第10章 程序编译与代码优化-早期(编译期)优化

    概述 Java语言的“编译期”其实是一段“不确定”的操作过程,因为它可能是指一个前端编译器(其实叫“编译器的前端”更准确一些)把*.java文件转变成*.class文件的过程;也可能是指虚拟机的后端运 ...

  9. SpringBoot学习(三)探究Springboot自动装配

    目录 什么是自动装配 何时自动装配 原理分析 注:以下展示的代码springboot的版本为2.0.3版.因源码过长,大家选择展开代码 ㄟ( ▔, ▔ )ㄏ 什么是自动装配 自动装配还是利用了Spri ...

  10. Python 中的for....else....

    在一个for循环中,当循环遇到break语句之后程序就会跳出循环,执行for循环之后的语句:但是,当整个循环都没有遇上break语句,而且你想在这种情况下做一些事情的话,你就可以通过结合else来完成 ...