sed和awk

sed

sed 文本三剑客之二

grep 就是查找文本当中的内容,扩展表达式。

sed是一种流编器,一次处理一行内容。

如果只是展示,会放在缓冲区(模式空间),展示结束之后,会从模式空间把操作结果删除。

一行一行处理,处理完当前行才会处理下一行。直到文件的末尾。

sed命令格式和操作选项

sed-e ‘操作符’ 文件1 文件2

-e 有多个操作符加 只有一个操作符-e可以省略

sed -e ‘操作符1’;‘操作符2’  文件1 文件2

选项

-e 用于执行多个操作命令

-f 在脚本定义好了操作符,然后根据脚本内容的操作符对文件进行操作

-i 直接修改目标文件(慎用)

-n 仅显示scrip处理后的结果(不加-n会有两个输出结果,加了-n会把默认输出屏蔽,只显示一个结果)

操作符

p:打印结果

r:使用扩展正则表达式

s:替换,替换字符串

c:替换,替换行

y:替换,替换单个字符,多个字符替换必须和替换内容的长度保持一致

d:删除,删除行

a:增加/插入,在指定行的下面一行插入内容

i:在指定行的上面一行插入内容

r: 在行后增加内容

$a: 在最后一行插入新的内容

$i:在倒数第二行插入新的内容

$r: 读取其他文件的内容然后插入到对象文件的最后一行

打印功能 p
[root@test2 opt]# sed -e 'p' test1.txt 
987-123-4567
987-123-4567
987 456-1230
987 456-1230
(123) 456-7890
(123) 456-7890
[root@test2 opt]# sed -n 'p' test1.txt 
987-123-4567
987 456-1230
(123) 456-7890

1、寻址打印 按照指定的行打印

显示行号打印
[root@test2 opt]# sed -n '=;p' test1.txt 
1
987-123-4567
2
987 456-1230
3
(123) 456-7890
打印行号
[root@test2 opt]# sed -n '=' test1.txt 
1
2
3

2、 按照指定的行打印

[root@test2 opt]# cat -n test1.txt | sed -n '2p'
     2  987 456-1230

3、打印最后一行

[root@test2 opt]# sed -n '$p' test1.txt 
(123) 456-7890

4、行号范围打印

[root@test2 opt]# sed -n '2,4p' test1.txt 
2 987 456-1230
3 (123) 456-7890
4  adasd
指定打印哪几行
[root@test2 opt]# sed -n '2p;4p' test1.txt 
2 987 456-1230
4  adasd

5、如何打印奇数行和偶数行

奇数行
[root@test2 opt]# sed -n 'p;n' test1.txt 
1 987-123-4567
3 (123) 456-7890
5 ad
7saafdf
偶数行
[root@test2 opt]# sed -n 'n;p' test1.txt 
2 987 456-1230
4  adasd
6sdaa
8adaf

引号里‘n’作用 跳过一行,打印下一行

6、如何对文本内容进行过滤 '/要过滤的内容/p'

过滤并打印包含a的行,从上到下查找打印
[root@test2 opt]# sed -n '/a/p' test1.txt 
4  adasd
5 ad
6sdaa
7saafdf
8adaf

7、使用正则表达式对文本内容进行过滤

[root@test2 opt]# sed -n '/^root/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@test2 opt]# sed -n '/bash$/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
wbl:x:1000:1000:wbl:/home/wbl:/bin/bash

从指定行开始,打印到第一个以bash结尾的行

[root@test2 opt]# sed -n '42,/bash$/p' /etc/passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
wbl:x:1000:1000:wbl:/home/wbl:/bin/bash

8、扩展表达式要加-r

[root@test2 opt]# sed -rn '/(99:){2,}/p' /etc/passwd
nobody:x:99:99:Nobody:/:/sbin/nologin

以什么开头以什么结尾

[root@test2 opt]# sed -rn '/^root|bash$/p' /etc/passwd
root:x:0:0:root:/root:/bin/bash
wbl:x:1000:1000:wbl:/home/wbl:/bin/bash

面试题1

如何免交互删除文本内容?不删除文件!

cat /dev/null > 文件名
sed -i 'd'  文件名
root@test2 opt]# cat test2.txt 
1
2
3
4                       只删除文本内容不删除文件
5
6
[root@test2 opt]# sed -i 'd' test2.txt 
[root@test2 opt]# cat test2.txt 
[root@test2 opt]# 
[root@test2 opt]# cat /dev/null > test1.txt 
[root@test2 opt]# cat test1.txt 
[root@test2 opt]# 
sed删除操作

删除指定的行并打印

[root@test2 opt]# sed -n '3d;p' test1.txt 
1
2
4
5

删除指定范围行

[root@test2 opt]# sed -n '5,$d;p' test1.txt 
1
2
3
4

除了指定的行其他的全部删除

[root@test2 opt]# sed -n '1!d;p' test1.txt 
1

删除除了指定几行到几行

[root@test2 opt]# sed -n '4,6!d;p' test1.txt 
4
5
6

删除除了几行和几行

[root@test2 opt]# sed -i -e '1,3d' -e '5d' -e '7,$d' test1.txt
[root@test2 opt]# cat test1.txt
4
6

面试题2

如何免交互的删除空行?

[root@test2 opt]# grep -v "^$" test1.txt
​
[root@test2 opt]# cat test1.txt | tr -s "\n"
​
[root@test2 opt]# sed -i '/^$/d' test1.txt
替换字符串

s:替换,替换字符串

c:替换,替换行

y:替换,替换单个字符,多个字符替换必须和替换内容的长度保持一致

只替换第一个
[root@test2 opt]# sed -n 's/root/test/p' /etc/passwd
test:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/test:/sbin/nologin
指定替换哪个
[root@test2 opt]# sed -n 's/root/test/2p' /etc/passwd
root:x:0:0:test:/root:/bin/bash
替换所有
[root@test2 opt]# sed -n 's/root/test/gp' /etc/passwd
test:x:0:0:test:/test:/bin/bash
operator:x:11:0:operator:/test:/sbin/nologin

注释掉所有

[root@test2 opt]# sed -n 's/^/#/p' test1.txt

注释掉指定行

[root@test2 opt]# sed -n '4 s/^/#/p' test1.txt
#4agfgrg

把首字母变成大写

u&转换首字母大写的特殊符号,\转义符

[root@test2 opt]# sed 's/[a-z]/\u&/' test1.txt
A1adadaggsfh              [A-Z] l&  首字母换小写
B2frgtsh
C3frgteshg
D4agfgrg
E5SGertg
F6aFrawg

所有小写变成大写

[root@test2 opt]# sed 's/[a-z]/\u&/g' test1.txt
A1ADADAGGSFH
B2FRGTSH
C3FRGTESHG
D4AGFGRG
E5SGERTG
F6AFRAWG

把大写转换成小写

[root@test2 opt]# sed 's/[A-Z]/\L&/g' test1.txt
a1adadaggsfh
b2frgtsh
c3frgteshg
d4agfgrg
e5sgertg
f6afr
整行替换
[root@test2 opt]# sed '/aaa/c wblnizhenshuai' test1.txt
wblnizhenshuai
ddd
ggg

面试题3

替换IP地址

sed '/替换前内容/c 替换后的内容' 路径

单字符替换

[root@test2 opt]# sed 'y/ggg/123/' test1.txt
aaa
ddd
111
fff
hhh

添加行

/a在指定行下插入整行

[root@test2 opt]# sed '/ddd/a wbl' test1.txt
aaa
ddd
wbl
ggg

/i在指定行上插入整行

[root@test2 opt]# sed '/ggg/i wbl' test1.txt
aaa
ddd
wbl
ggg
使用sed对字符串和字符的位置进行互换
[root@test2 opt]# echo chengqianshuai | sed -r 's/(cheng)(qian)(shuai)/\3\1\2/'
shuaichengqian

单个字符位置互换

[root@test2 opt]# echo wblzs | sed -r 's/(.)(.)(.)(.)(.)/\5\4\3\2\1/'
szlbw
[root@test2 opt]# sed -f test1.txt test2.txt 
aaaaaaaaaaa
[root@test2 opt]# cat test1.txt 
123aaa456bbb
[root@test2 opt]# cat test2.txt 
aaaaaaaaaaa

面试题4

提取文件中的数字和点

ant-1.9.7.jar ant-launcher-1.9.7.jar antlr-2.7.7.jar antlr-runtime-3.4.jar aopalliance-1.0.jar archaius-core-0.7.6.jar asm-5.0.4.jar aspectjweaver-1.9.5.jar bcpkix-jdk15on-1.64.jar bcprov-jdk15-1.46.jar bcprov-jdk15on-1.64.jar checker-compat-qual-2.5.5.jar

[root@test2 opt]# cat test1.txt | sed -r 's/(.*)-(.*)(\.jar)/\2/'
1.9.7
1.9.7
2.7.7
3.4
1.0
0.7.6
5.0.4
1.9.5
1.64
1.46
1.64
2.5.5
[root@test2 opt]# cat test1.txt | grep -E '[0-9]+\.'
nt-1.9.7.jar                           此时数字和点是红色
ant-launcher-1.9.7.jar
antlr-2.7.7.jar
antlr-runtime-3.4.jar
aopalliance-1.0.jar
archaius-core-0.7.6.jar
asm-5.0.4.jar
aspectjweaver-1.9.5.jar
bcpkix-jdk15on-1.64.jar
bcprov-jdk15-1.46.jar
bcprov-jdk15on-1.64.jar
checker-compat-qual-2.5.5.jar

面试题5

打印指定时间内的日志

[root@test2 opt]# cat /var/log/messages | sed -n '/Jun 21 12:40:01/,/Jun 21 14:01:01/p'Jun 21 12:40:01 test2 systemd: Started Session 404 of user root.
Jun 21 12:50:01 test2 systemd: Started Session 405 of user root.
Jun 21 13:00:01 test2 systemd: Started Session 406 of user root.
Jun 21 13:01:01 test2 systemd: Started Session 407 of user root.
Jun 21 13:10:01 test2 systemd: Started Session 408 of user root.
Jun 21 13:20:01 test2 systemd: Started Session 409 of user root.
Jun 21 13:30:01 test2 systemd: Started Session 410 of user root.
Jun 21 13:40:01 test2 systemd: Started Session 411 of user root.
Jun 21 13:50:01 test2 systemd: Started Session 412 of user root.
Jun 21 14:00:01 test2 systemd: Started Session 413 of user root.
Jun 21 14:01:01 test2 systemd: Started Session 414 of user root.

sed的主要作用就是对文本内容进行增删改查

其中最好用,最强大的是改和增

作业:

使用脚本的形式,结合sed命令,把pxe自动装机做一个自动化部署的脚本。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值