12.Linux三剑客之sed

1.sort命令

sort命令
	用于将文件内容加以排序
[root@localhost ~]# vi 1.txt 
978
654
321
123
456
789
-n # 依照数值的大小排序
[root@localhost ~]# sort -n 1.txt
123
321
456
654
789
978
 -r # 以相反的顺序来排序
[root@localhost ~]# sort -r 1.txt
978
789
654
456
321
123
-k # 以某列进行排序
[root@localhost ~]# vi 1.txt 
97 8
65 4
32 1
12 3
45 6
78 9
[root@localhost ~]# sort  -k2 1.txt
32 1
12 3
65 4
45 6
97 8
78 9
-t # 指定分割符,默认是以空格为分隔符
[root@localhost ~]# vi 1.txt 
|9|7| |8|
|6|5| |4|
|3|2| |1|
|1|2| |3|
|4|5| |6|
|7|8| |9|
[root@localhost ~]# sort -n -k3 -t '|' 1.txt 
|1|2| |3|
|3|2| |1|
|4|5| |6|
|6|5| |4|
|9|7| |8|
|7|8| |9|

2.uniq 命令

用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用
* 去重相邻的
-c : 在每列旁边显示该行重复出现的次数。
[root@localhost ~]# cat 1.txt | sort | uniq -c
      1 |1|2| |3|
      1 |3|2| |1|
      1 |4|5| |6|
      1 |6|5| |4|
      1 |7|8| |9|
      1 |9|7| |8|
-d # 仅显示重复出现的行列。
vi 1.txt 
"""
添加
|9|7| |8|
"""
[root@localhost ~]# cat 1.txt | sort | uniq -d 
|9|7| |8|
-u # 仅显示出一次的行列
[root@localhost ~]# cat 1.txt | sort | uniq -u
|1|2| |3|
|3|2| |1|
|4|5| |6|
|6|5| |4|
|7|8| |9|

3.cut 命令

cut命令用来显示行中的指定部分,删除文件中指定字段
-d # 指定字段的分隔符,默认的字段分隔符为"TAB"
-f # 显示指定字段的内容;
[root@localhost ~]# cat 1.txt | cut -d '|' -f2
9
9
6
3
1
4
7

4.tr命令

替换或删除命令
* 单个字符单个字的替换,不是一组
-d # 删除字符
[root@localhost ~]# cat 1.txt
|9|7| |8|
|9|7| |8|
|6|5| |4|
|3|2| |1|
|1|2| |3|
|4|5| |6|
|7|8| |9|
[root@localhost ~]# cat 1.txt | tr 123 456
|9|7| |8|
|9|7| |8|
|6|5| |4|
|6|5| |4|
|4|5| |6|
|4|5| |6|
|7|8| |9|

6.wc命令

统计,计算数字
-c # 统计文件的Bytes数
-l # 统计文件的行数
-w # 统计文件中单词的个数,默认以空白字符做为分隔符

注:在Linux系统中,一段连续的数字或字母组合为一个词,以空格区分.
[root@localhost ~]# vi 2.txt 
"""
什么都不写.默认有个字节一个^
:wq 
"""
[root@localhost ~]# wc -c 2.txt 

1 2.txt
[root@localhost ~]# vi 2.txt 
"""
123456
:wq
"""
[root@localhost ~]# wc -c 2.txt 
7 2.txt
[root@localhost ~]# wc -l 2.txt
1 2.txt
[root@localhost ~]# echo 'asda!sa' > 2.txt
[root@localhost ~]# wc -w 2.txt
1 2.txt

7.linux三剑客之sed

sed是linux中,流媒体编辑器。
7.1格式
sed [参数] '处理规则' [操作对象]
7.2参数
-e  允许多项编辑
[root@localhost ~]# vi 1.txt
"""  
1
2
3
4
5
6
:wq
"""                                                                                                 
[root@localhost ~]# sed -e '3d' -e '4d' 1.txt 
1
2
5
6
-n :  取消默认输出
# 不显示
[root@localhost ~]# sed -n '3d'  1.txt 
-i :  就地编辑
[root@localhost ~]# sed '5p' 1.txt 
1
2
3
4
5
5
6
[root@localhost ~]# sed -i '5p' 1.txt 
[root@localhost ~]# cat 1.txt 
1
2
3
4
5
5
6
-r :  支持拓展正则
[root@localhost ~]# vi 1.txt
123
2456
3456
4567
5678
6
[root@localhost ~]# sed  -r '/[0-9]{3}/d' 1.txt 
6
-f :  指定sed匹配规则脚本文件
vi x.sh
"""
/[0-9]{3}/d
"""
[root@localhost ~]# sed  -rf x.sh 1.txt 
6
7.3定位
1.数字定位法
指定行号。
sed '3d' xxx
sed '2,3d' xxx
[root@localhost ~]# sed '3,6d' 1.txt
123
2456
2.正则定位法
指定正则定位。
sed '/^1/d' xxx
[root@localhost ~]# sed '/^1/d' 1.txt
2456
3456
4567
5678
6
3.数字和正则定位法
sed '/^1/,4d' xxx
从以1开头的行到第四行
[root@localhost ~]# sed '/^1/,4d' 1.txt
5678
6
4.正则正则定位法
sed '/^g/,/^1/d' xxx
[root@localhost ~]# sed '/2/d' 1.txt
3456
4567
5678
6
7.4编辑模式
1.删除
d :删除
[root@localhost ~]# sed '/2/d' 1.txt
# 被匹配的不显示
3456 
4567
5678
6
2.打印
p :打印
[root@localhost ~]# sed '3p' 1.txt
123
2456
3456  # 多显示一行
3456
4567
5678
6
3.前添加
a : 在当前行后添加一行或多行
[root@localhost ~]# sed '2axxx' 1.txt
123
2456
xxx
3456
4567
5678
6
4.替换
c :用新文本修改(替换)当前行
	整行内容替换
s : 将字符串转换成另一个字符串(每一行只替换一次)
	sed 's/1/2/' xxx
	sed 's/1/2/g' xxx 
g : 全部执行
	sed 's/11/22/g' xxx
y : 将字符转换成另一个字符
	sed 'y/1/2/' xxx
[root@localhost ~]# sed '2cxxx' 1.txt
123
xxx
3456
4567
5678
6
[root@localhost ~]# sed  's/1/2/' 1.txt 
223
2456
3456
4567
5678
6
[root@localhost ~]# sed  'y/1/2/' 1.txt 
223
2456
3456
4567
5678
6
5.插入/忽略大小写
i : 在当前行之前,插入文本(单独使用时)
i : 忽略大小写(跟 s 模式一起使用时)
	sed -i ....
[root@localhost ~]# sed '2ixxx' 1.txt
123
xxx
2456
3456
4567
5678
6
6.读文件插入
r : 在文件中读内容
	sed '2r x.txt' xxx
	      r读 x.txt 的文件内容 插入到xxx文件的第2行后面
[root@localhost ~]# sed  '1r x.sh' 1.txt 
123
/[0-9]{3}/d
2456
3456
4567
5678
6
7.截取文件写入
w : 将指定行写入文件
	sed '2w x.txt' xxx
	w写 读xxx文件的第二行写入 x.txt文件中
[root@localhost ~]# sed  '2w x.txt' 1.txt 
123
2456
3456
4567
5678
6
[root@localhost ~]# cat x.txt 
2456
8.行前插入
& :代表前面匹配到的内容
[root@localhost ~]# sed -r 's/.*/1 &/' 1.txt
1 123
1 2345
1 3456
1 4567
1 5678
1 6

8.练习

8.1练习1
1.txt中的注释行全部去掉
[root@localhost ~]# vi 1.txt 
# 123
# 2456 #1

# 3456
# 4567


# 5678
6#
[root@localhost ~]# sed -r 's/^#//' 1.txt
 123
 2456 #1

 3456
 4567


 5678
6#
8.2练习2
1.txt中每一行之前增加注释
[root@localhost ~]# sed -r 's/.*/# &/' 1.txt
# # 123
# # 2456 #1
# 
# # 3456
# # 4567
# 
# 
# # 5678
# 6#

8.3练习3
替换/ 
[root@localhost ~]# vi 1.txt
123/
2345/
3456
4567
5678
6
[root@localhost ~]# sed -r 's#/#*#' 1.txt 
123*
2345*
3456
4567
5678
6
8.4练习4
修改本机的ip
192.168.15.100 ---> 192.168.15.101
sed -i 's#.100#.101#g' /etc/sysconfig/network-scripts/ifcfg-eth[01]
# 不执行了,执行就改了
8.5练习5
/etc/passwd中的root修改成ROOT
 sed -i 's#root#ROOT#g' /etc/passwd
# 不执行了,执行就改了
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值