<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? 件中所有以 #开头, 后?? 少跟? 个空? 字符的? 的?? 的# 和空? 字符.
[[email protected] ~]# sed -r 's/^#[[:space:]]+//g' /etc/fstab
3, 在 centos6 系统 / root/install.log 每???? 增加 #号.
[[email protected] tmp]# sed 's/^/#/g' /root/install.log
4, 在 / etc/fstab? 件中不以 #开头的? 的?? 增加 #号.
- [[email protected] ~]# sed -r 's/^[^#]/#&/g' /etc/fstab
- [[email protected] ~]# sed -r '/^[^#]/[email protected]^@#@' /etc/fstab
5, 处理 / etc/fstab 路径, 使? sed 命令取出其? 录名和基名.
- [[email protected] ~]# echo "etc/fstab/dd/" | sed -r '[email protected]^(.*)/(.+)[email protected]\[email protected]'
- [[email protected] ~]# echo "etc/fstab/dd/" | sed -r '[email protected]^(.*)/(.+)[email protected]\[email protected]'
- dd/
6, 利? sed 取出 ifcon?g 命令中本机的 IPv4 地址
- [[email protected] ~]# ifconfig eth0 | sed -rn '/netmask/s#.*net (.*) net.*#\1#p'
- 192.168.38.128
7, 统计 CentOS 安装光盘中 Package? 录下的所有 rpm? 件的以. 分隔倒数第? 个字段的重复次数.
- [[email protected] ~]# 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 两种? 法分别实现).
[[email protected] ~]# egrep -o "\<[[:alpha:]]+\>" /etc/init.d/functions | sort | uniq -c | sort -n
9, 将? 本? 件的 n 和 n+1? 合并为??, n 为奇数?
- [[email protected] ~]# seq 10 | sed "1~2N;s/\n/ /"
- 1 2
- 3 4
- 5 6
- 7 8
- 9 10
面试题
1,Linux 系统中, 命令可以从文本文件的每一行中截取指定的内容的数据.
cut,awk
2, 在每一行后增加一空行?
- [[email protected] ~]# sed G /etc/fstab
- [[email protected] tmp]# sed -r 's/$/\n/' /etc/passwd
3, 在匹配 regex 的行之后插入一空行?
- [[email protected] ~]# sed '/regex/G' A.txt
- [[email protected] tmp]# sed -r '/root/[email protected][email protected]\[email protected]' /etc/passwd
4, 计算文件行数?
[[email protected] ~]# wc -l /etc/passwd
6,sed 将文件 test 中第 50 行中的 haiwao 改为 haiwai?
[[email protected] ~]# 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'
- [[email protected] tmp]# sed /^root/p -n /etc/passwd | sed 's/root/test/2'
- root:x:0:0:test:/root:/bin/bash
8, 打印 / etc/passwd 的奇数行?
[[email protected] tmp]# sed 1~2p /etc/passwd -n
实验
1, 利? sed 取出 ifcon?g ens33 命令中本机的 IPv4 地址
- [[email protected] ~]$ ifconfig ens33 | sed -n 2p | sed -r 's/.*inet (.*) net.*/\1/'
- 192.168.38.130
2, 删除 / etc/fstab? 件中所有以 #开头, 后?? 少跟? 个空? 字符的? 的?? 的 #和空? 字符
[[email protected] ~]$ 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 的? 之间的所有?
- [[email protected] ~]# sed -r '/2/,/4/p' pets -n
- c2aaadog
- 4
- ddog2
- sa 4
- 2
- a
11, 显? pets? 件中, 第 1? 到第 3? 之间的所有?
[[email protected] ~]# sed 1,3p pets -n
12, 显? pets? 件中第 2? 及后? 的 1?
[[email protected] ~]# sed 2,+1p pets -n
13, 显? pets? 件中第 1? 和 dog 字符串之间的?
- [[email protected] ~]# sed -nr '1,/dog/p' pets
- a
- a
- Q A
- regex
- dd
- b
- c2aaadog
14, 显? pets? 件的奇数?
[[email protected] ~]# sed 1~2p pets -n
15, 显? pets? 件的偶数?
[[email protected] ~]# sed 2~2p pets -n
16, 在 pets? 件的第 2? 的下?? 添加 hello
[[email protected] ~]# sed '2a hello' pets -i
17, 在 pets? 件的第 2? 的下?? 添加 2? 内容为 hello 和 world
[[email protected] ~]# sed '2a hello\nworld' pets -i
18, 在 pets? 件的第 2? 的前?? 添加 2? 内容为 hello 和 world
[[email protected] ~]# sed '2i hello\nworld' pets -i
19, 把 pets? 件的第 2? 替换为 hello
- [[email protected] ~]# sed '2c hello' pets -i
- [[email protected] ~]# seq 4 | sed '2c hello'
- 1
- hello
- 3
- 4
20, 把 pets? 件的第 1-3? 内容, 另存为 test.txt? 件
[[email protected] ~]# sed 1,3p pets -n> test.txt
21, 在第 2? 后读? test.txt? 件
[[email protected] ~]# sed '2r test.txt' pets
22, 不显? 第 2?
[[email protected] ~]# sed '2!p' test.txt -n
23, 把 pets? 件中的每? 内容前都编序号显?
- [[email protected] ~]# cat -n pets
- [[email protected] ~]# sed '=' pets
来源: http://www.bubuko.com/infodetail-3146512.html