相关内容
**
cat
tac
rev
more
less
head
tail
cut
sort
uniq
paste
wc
diff
patch
/dev/urandom
- 数字和字母组成的随机数
- 取linux的版本号
- 取网卡的ip号
- 1-N随机数
- 取df中取磁盘的利用率
- 取磁盘利用率最高的值
- /etc/passwd中的user:uid,UID从大到小
- 对web服务器访问的IP地址的前10名,从大到小显示其次数和相应的IP
**
cat
只看文本文件,不能看二进制文件
-b 加行号 文本中的空格不算
-n 加行号 空行也加入行号
-s 删除重复的空行
-A 可以查看文本中的空格$ 回车等看不到的字符
[root@centos7 root2018-07-30]#cat -b test.txt
1 hello world
2 123456
[root@centos7 root2018-07-30]#cat -n test.txt
1 hello world
2
3
4 123456
5
[root@centos7 root2018-07-30]#cat -nsA test.txt
1 hello world$
2 $
3 123456$
4 $
tac
cat的反向显示
[root@centos7 root2018-07-30]#tac test.txt
123456
hello world
rev
对一行完全反向
[root@centos7 root2018-07-30]#cat test.txt
hello world
123456
[root@centos7 root2018-07-30]#rev test.txt
dlrow olleh
654321
head
输入文件或标准输入流的前N部分
-nN 显示前N行
-cN 显示前N个字符
[root@centos7 root2018-07-30]#head -n 2 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@centos7 root2018-07-30]#cat test.txt
hello world
123456
[root@centos7 root2018-07-30]#head -c 5 test.txt
hello
tail
输入文件或标准输入流的后N部分
-n N 显示后N行
-c N 显示最后N个字节
-f 可以跟踪文件,打开并挂起文件,若文件更改可以直接显示tailf = tail -f
-F 跟踪文件
[root@centos7 root2018-07-30]#tail -n 2 /etc/passwd
kittycat:x:2011:2013::/home/kittycat:/bin/bash
www:x:2012:2014::/home/www:/bin/bash
[root@centos7 root2018-07-30]#cat test.txt
hello world
123456
[root@centos7 root2018-07-30]#tail -c 5 test.txt
3456
cut
从文本中截取相应的列
-c 按字符切割
-d 分隔符
-f N 第N列
n-m 第n-m列
x,y,z 第x,y,z列
[root@centos7 root2018-07-30]#cat test2.txt
1 2 3 4 5
a b c d e
[root@centos7 root2018-07-30]#cat test2.txt |cut -d " " -f 1
1
a
[root@centos7 root2018-07-30]#cat test2.txt |cut -d " " -f 1-3,5
1 2 3 5
a b c e
paste
- paste f1 f2…… 横向合并文件
- cat f1 f2…… 纵向合并文件
-s 所有行在一行
[root@centos7 root2018-07-30]#cat f1
1 2 3 4
5 6 7 8
[root@centos7 root2018-07-30]#cat f2
a b c d e
A B C D E
[root@centos7 root2018-07-30]#cat f1 f2
1 2 3 4
5 6 7 8
a b c d e
A B C D E
[root@centos7 root2018-07-30]#paste f1 f2
1 2 3 4 a b c d e
5 6 7 8 A B C D E
[root@centos7 root2018-07-30]#cat f1
1 2 3 4
5 6 7 8
[root@centos7 root2018-07-30]#cat f2
a b c d e
A B C D E
[root@centos7 root2018-07-30]#paste f1 f2
1 2 3 4 a b c d e
5 6 7 8 A B C D E
[root@centos7 root2018-07-30]#paste -s f1
1 2 3 4 5 6 7 8
wc
word count统计单词
-l 行数
-L 最长行的长度,字节
-m 只计算字符总数
-c 只计算字节总数
[root@centos7 root2018-07-30]#cat f1
1 2 3 4
5 6 7 8
[root@centos7 root2018-07-30]#wc f1
2 8 16 f1
行 单词 字节数 文件名
2 8 16 f1
[root@centos7 root2018-07-30]#wc -l f1
2 f1
[root@centos7 root2018-07-30]#wc -L f1
7 f1
sort
文本列排序,相同的文本连续行放置
-t 以什么来做分隔符
-n 将字符转换为数字进行排序
-kN 第N列
-r 结果反向排序
-u 删除连续重复的
-R 随机排序
/etc/passwd中的uid进行排序
[root@centos7 root2018-07-30]#sort -t: -k 3 -n /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
uniq
删除相连的重复行
-c: 显示每行重复出现的次数
-d: 仅显示重复过的行
-u: 仅显示不曾重复的行
==注:连续且完全相同方为重复==
[root@centos7 root2018-07-30]#cat uniqtest
aaa
aaa
bb
bbb
aaa
ccc
ccc
ccc
aa
[root@centos7 root2018-07-30]#uniq -c uniqtest
2 aaa
1 bb
1 bbb
1 aaa
3 ccc
1 aa
[root@centos7 root2018-07-30]#uniq -d uniqtest
aaa
ccc
[root@centos7 root2018-07-30]#uniq -u uniqtest
bb
bbb
aaa
aa
diff
diff file1 file2
-u 显示一些额外的信息
[root@centos7 root2018-07-30]#diff -u f1 f2
--- f1 2018-07-30 10:00:30.159857215 +0800
+++ f2 2018-07-30 10:07:45.063838584 +0800
@@ -1,12 +1,12 @@
aa
-aaa
-aaa
+aaaaaa
+aaaaaa
aa
+bbbb
bb
文本处理工具综合应用
数字和字母组成的随机数
/dev/urandom随机无限个字符
[root@centos7 root2018-07-30]#cat /dev/urandom |tr -dc '[:alnum:]' |head -c 10
4Ud7FNQpzI
取linux的版本号
[root@centos7 root2018-07-30]#cat /etc/redhat-release |tr -dc '[0-9].'
7.5.1804
[root@centos7 root2018-07-30]#cat /etc/redhat-release |tr -dc '[0-9].' |cut -d. -f 1
7
取网卡的ip号
[root@centos7 root2018-07-30]#ifconfig ens33 |tr -d "\n" |tr -s " " ":" |cut -d : -f6
192.168.32.132
[root@centos7 root2018-07-30]#ifconfig ens33 | head -n 2 | tail -n 1 |tr -s " " ":" |cut -d : -f 3
192.168.32.132
1-N随机数
[root@centos7 root2018-07-30]#seq 72 |sort -R |head -n1
9
[root@centos7 root2018-07-30]#seq 72 |sort -R |head -n1
45
取df中的利用率
[root@centos7 root2018-07-30]#df |grep /dev/sd | tr -s " " "%" |cut -d % -f5
8
1
17
取磁盘利用率最高的值
[root@centos7 root2018-07-30]#df |grep /dev/sd |tr -s " " "%" |sort -t % -k 5 -n -r| cut -d % -f 5 | head -n 1
17
[root@centos7 root2018-07-30]#df |grep /dev/sd |tr -s " " "%" | cut -d % -f 5 |sort -nr | head -n 1
17
/etc/passwd中的user:uid,UID从大到小
[root@centos7 root2018-07-30]#cut -d: -f1,3 /etc/passwd | sort -t: -k 2 -rn
nfsnobody:65534
www:2012
kittycat:2011
tommy1:2010
tommy:2009
user10:2008
ansible:2007
对web服务器访问的IP地址的前10名,从大到小显示其次数和相应的IP
[root@centos7 root2018-07-30]#cat access_log |more
192.168.32.7 - - [30/Jul/2018:10:13:28 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:13:28 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:13:28 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:13:28 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:13:28 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:13:28 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:13:28 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:13:28 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
192.168.32.7 - - [30/Jul/2018:10:13:28 +0800] "GET / HTTP/1.0" 403 4961 "-" "ApacheBench/2.3"
[root@centos7 root2018-07-30]#cat access_log |cut -d " " -f1 |sort | uniq -c |sort -rn
2000 192.168.32.17
1100 192.168.32.7
5 192.168.32.5