由于比较难,附上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. 浏览器渗透框架BeEF使用笔记(一)

    0x00 前言 BeEF,全称The Browser Exploitation Framework,是一款针对浏览器的渗透测试工具. 用Ruby语言开发的,Kali中默认安装的一个模块,用于实现对XS ...

  2. 爬虫4:pdf页面+pdfminer模块+demo

    本文介绍下pdf页面的爬取,需要借助pdfminer模块 demo一般流程: 1)设置url url = 'http://www.------' + '.PDF' 2)requests模块获取url ...

  3. JavaScript回调函数和递归函数

    一.回调函数--通过函数的指针来调用函数 把一个函数的指针作为另一个函数的参数,当调用这个参数的时候,这个函数就叫做回调函数 在链式运动上会用到回调函数,之后运动会见到 A.通过指针来调用函数 B.通 ...

  4. mysqldump导出 timestamp类型数据 时区偏差8小时

    1 出现原因 1.1 查看机器时区和MySQL的时区 #查看linux的机器时区: [root@mysql-150 ~]# date -R Wed, 23 Oct 2019 14:10:04 +080 ...

  5. Vuex的简单应用

    ### 源码地址 https://github.com/moor-mupan/mine-summary/tree/master/前端知识库/Vuex_demo/demo 1. 什么是Vuex? Vue ...

  6. prefer-object-spread

    eslint报错: Use an object spread instead of `Object.assign` eg: `{ ...foo }`.(prefer-object-spread) 即: ...

  7. Swagger--解决日期格式显示为Unix时间戳格式 UTC格式

    在swagger UI模型架构上,字段日期显示为“日期”:“2018-10-15T09:10:47.507Z”但我需要将其作为“日期”:“2018-9-26 12:18:48”. tips:以下这两种 ...

  8. java中多线程 - 如何创建多线程

    线程 什么是线程: 线程是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源 表面上 ...

  9. calendar类-时间处理类

    calendar类 calendar类是时间处理类 比如在scala中 //字符串转化日期格式 val df = new SimpleDateFormat("yyyy-MM-dd hh:mm ...

  10. Kubernetes入门学习--在Ubuntu16.0.4安装配置Minikube

    目 录 一. 安装minikube环境 1.1. 安装前准备 1.2. 安装Lantern 1.2.1. Lantern下载网站 1.2.2. Lantern下载地址 1.2.3. Lantern安装 ...