[Linux] grep、sort、uniq、tr、paste
@grep命令
grep [-ivnc] ‘需要匹配的字符’ 文件名
-i不区分大小写
-c统计包含匹配的行数
-n输出行号
-v反向匹配
[root@xxx ~]# cat test1226
HAHAHAHA
Weekend
weekend
Weekend is coming.
Enjoy your Weekend.
[root@xxx ~]# grep -ci weekend test1226
4
[root@xxx ~]# grep -ni weekend test1226
2:Weekend
3:weekend
4:Weekend is coming.
5:Enjoy your Weekend.
[root@xxx ~]#
@使用sort排序
sort [-ntkr] 文件名
-n采取数字排序
-t指定分隔符
-k指定第几列
-r反向排序
[root@xxx ~]# cat test1226_2
a:2
d:1
c:11
b:22
[root@xxx ~]# sort test1226_2
a:2
b:22
c:11
d:1
[root@xxx ~]# cat test1226_2 | sort
a:2
b:22
c:11
d:1
[root@xxx ~]# cat test1226_2 | sort -t ":" -k 2
d:1
c:11
a:2
b:22
[root@xxx ~]# cat test1226_2 | sort -t ":" -k 2 -n
d:1
a:2
c:11
b:22
[root@xxx ~]#
@使用uniq删除重复内容
uniq [-ic]
-i忽略大小写
-c计算重复行数
说明:需要和sort命令一起使用,因为uniq命令只会对比相邻的行
[root@xxx ~]# cat test1226_3
abc
123
abc
123
xyz
123
[root@xxx ~]# uniq test1226_3
abc
123
abc
123
xyz
123
[root@xxx ~]# cat test1226_3 | sort | uniq
123
abc
xyz
[root@xxx ~]# cat test1226_3 | sort | uniq -c
3 123
2 abc
1 xyz
[root@xxx ~]#
@tr命令做文本转换或删除
[root@xxx ~]# cat test1226_2
a:2
d:1
c:11
b:22
[root@xxx ~]# cat test1226_2 | tr '[a-z]' '[A-Z]'
A:2
D:1
C:11
B:22
[root@xxx ~]# cat test1226_2 | tr -d ':'
a2
d1
c11
b22
[root@xxx ~]# cat test1226_2 | tr -d '1'
a:2
d:
c:
b:22
[root@xxx ~]#
@使用paste做文本合并,默认使用tab分隔
[root@xxx ~]# cat test1226
HAHAHAHA
Weekend
weekend
Weekend is coming.
Enjoy your Weekend.
[root@xxx ~]#
[root@xxx ~]# cat test1226_3
abc
123
abc
123
xyz
123
[root@xxx ~]# paste -d: test1226 test1226_3
HAHAHAHA:abc
Weekend:123
weekend:abc
Weekend is coming.:123
Enjoy your Weekend.:xyz
:123
[root@xxx ~]#
参考书籍:《Linux系统命令及Shell脚本实践指南》王军 著,第5章 字符处理
还没有评论,来说两句吧...