由于比较难,附上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. Int类的129为什么转成byte就变成-127了?

    作为一个java开发人员,接触的基本都是上层的,都是以应用为主,根据业务实现功能,但今天无意间发现了一个小问题,int类型的129转成byte类型变成了-127,我知道是因为位数截取的原因,但是还没有 ...

  2. Git推送到多个远程仓库

    Git推送到多个远程仓库 Grey 原文地址 准备工作 在码云和Github上分别新建两个不包括任何文件的空仓库(若是两个已经有文件的仓库,请参见关联已经存在的项目) https://github.c ...

  3. opencv::opencv_traincascade.exe

    opencv_traincascade 是一个新程序,使用OpenCV .x API 以C++ 编写. 这二者主要的区别是 opencv_traincascade 支持 Haar 和 LBP 两种特征 ...

  4. Mqtt-Client

    客户端选择:paho MQTT client. 使用vs2013编译成库 需要用到paho-mqtt3a库和paho-mqtt3c库.

  5. java源码解析

    String深入解析 String具有不变性的原因: String被final修饰,它不可能被继承,也就是任何对String的操作方法,都不会被继承覆写 String中保存数据的是一个char数组的v ...

  6. echarts折线图动态改变数据时的一个bug

    echarts折线图中当增加dataZoom,修改start大于0的时候,会出现折线混乱,变成竖直的线,绘制有问题. 解决方法,在dataZoom中增加filterMode: 'empty' http ...

  7. 12.Nginx代理与负载均衡

    1.什么是代理? 代为办理 --> 代理 2.Nginx正向代理.反向代理 正向代理: --> 上网 | 路由器替代 反向代理: --> 正向与反向代理的区别: 区别在于形式上服务的 ...

  8. 【原创】(九)Linux内存管理 - zoned page frame allocator - 4

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  9. Orleans 知多少 | 3. Hello Orleans

    1. 引言 是的,Orleans v3.0.0 已经发布了,并已经完全支持 .NET Core 3.0. 所以,Orleans 系列是时候继续了,抱歉,让大家久等了. 万丈高楼平地起,这一节我们就先来 ...

  10. 百万年薪python之路 -- 面向对象之:类空间问题以及类之间的关系

    面向对象之:类空间问题以及类之间的关系 1.从空间角度研究类 1.何处添加对象属性 class A: def __init__(self,name): self.name = name def fun ...