cut分割命令:
-d 参数:指定分割符号,-f 参数:指定段数,-c 参数:指定第几个字符
[root@localhost ~]#cat 1.txt
root:x:0:0:root:/root:/bin/bash
[root@localhost ~]#cat 1.txt |cut -d ":" -f 1
root
[root@localhost ~]#cat 1.txt |cut -d ":" -f 1,2
root:x
[root@localhost ~]#cat 1.txt |cut -d ":" -f 1-5
root:x:0:0:root
[root@localhost ~]#cat 1.txt |cut -c 4
t
**sort排序命令:**将每行的内容以ASCII码从小到大排序
[root@localhost ~]#
[root@localhost ~]#cat 1.txt
abc
aa_1
a
abbop
#test
<zxcv
404
2018
5
[root@localhost ~]#sort 1.txt
2018
404
5
a
aa_1
abbop
abc
#test
<zxcv
-n 参数:以数字从小到大排序(字母和特殊符号开头的行会被默认为0)
[root@localhost ~]#cat 1.txt
abc
aa_1
a
abbop
#test
<zxcv
404
2018
5
[root@localhost ~]#sort -n 1.txt
a
aa_1
abbop
abc
#test
<zxcv
5
404
2018
-r 参数:倒序排序
[root@localhost ~]#sort -n 1.txt
a
aa_1
abbop
abc
#test
<zxcv
5
404
2018
[root@localhost ~]#sort -nr 1.txt
2018
404
5
<zxcv
#test
abc
abbop
aa_1
a
wc统计命令:
-l 参数:统计行数
[root@localhost ~]#cat test.txt
abc
aa_1
a
abbop
#test
<zxcv
404
2018
5
[root@localhost ~]#wc -l test.txt
9 test.txt
-m 参数:统计字符数
[root@localhost ~]#cat a.txt
2019
0917
[root@localhost ~]#wc -m a.txt
10 a.txt
*cat查看文件内容显示只有8个字符,但wc -m显示10个字符是因为还有隐藏换行符 “$”
[root@localhost ~]#cat -A a.txt
2019$
0917$
-w 参数:统计词数(以空格分隔)
[root@localhost ~]#cat a.txt
2019
0917 hello,world test
[root@localhost ~]#wc -w a.txt
4 a.txt
uniq去重命令:
uniq只能在相邻的行之间去重,所以uniq一般配合sort使用,先排序再去重:
[root@localhost ~]#cat a.txt
2019
hello world
test
test
1
1
2019
[root@localhost ~]#uniq a.txt
2019
hello world
test
1
2019 #该行未被去重
结合sort排序命令使用:
[root@localhost ~]#cat a.txt
2019
hello world
test
test
1
1
2019
[root@localhost ~]#sort a.txt | uniq
1
2019
hello world
test
-c 参数:统计去重次数
[root@localhost ~]#sort a.txt | uniq -c
2 1
2 2019
1 hello world
2 test
tee重定向命令:(与 > 重定向类似,区别在于tee命令在重定向时会打印重定向的内容)
[root@localhost ~]#sort a.txt | uniq -c
2 1
2 2019
1 hello world
2 test
[root@localhost ~]#sort a.txt |uniq -c |tee b.txt
2 1
2 2019
1 hello world
2 test
[root@localhost ~]#cat b.txt
2 1
2 2019
1 hello world
2 test
-a 参数:追加重定向
[root@localhost ~]#cat b.txt
2 1
2 2019
1 hello world
2 test
[root@localhost ~]#sort a.txt |uniq -c |tee -a b.txt
2 1
2 2019
1 hello world
2 test
[root@localhost ~]#cat b.txt
2 1
2 2019
1 hello world
2 test
2 1
2 2019
1 hello world
2 test
tr替换命令:(可以替换单个、多个及所以字符)
[root@localhost ~]#echo "hello world"
hello world
[root@localhost ~]#echo "hello world" |tr 'h' 'H'
Hello world
[root@localhost ~]#echo "hello world" |tr '[hw]' '[HW]'
Hello World
[root@localhost ~]#echo "hello world" |tr '[a-z]' '[A-Z]'
HELLO WORLD
[root@localhost ~]#echo "hello world" |tr '[a-z]' '0'
00000 00000
split切割命令:(通常用于切割大日志文件)
-b参数:指定切割大小(不指定单位的情况下默认是字节)
[root@localhost ~]#find /etc/ -type f -exec cat {} > log.txt \;
[root@localhost ~]#ls -lh
总用量 27M
-rw-r--r--. 1 root root 27M 9月 17 22:44 log.txt
[root@localhost ~]#split -b 10M log.txt
[root@localhost ~]#du -sh *
27M log.txt
10M xaa
10M xab
6.1M xac
切割的同时可以指定文件前缀:
[root@localhost ~]#split -b 10M log.txt testlog.
[root@localhost ~]#ls
log.txt testlog.aa testlog.ab testlog.ac
-l 参数:按行数切割
[root@localhost ~]#split -l 60000 log.txt
[root@localhost ~]#wc -l *
170640 log.txt
60000 xaa
60000 xab
50640 xac
341280 总用量