文件管理命令查找(which whereis)

基础命令讲完 (grep sed awk)

  1. 命令所在的绝对路径在哪?(脚本时,碰到环境不同,使用相对路径执行会报错,可以使用绝对命令)
    2)字符处理类? 统计( 分析文件中哪行内容出现的次数最多,取前3名)
    sort uniq wc cut sed awk
    3)练习:如何提取自己的IP地址

文件命令查找
#which ls #查找ls 命令的绝对路径
whereis ls #查找命令的路径,帮助手册/等
whereis -b ls #仅显示命令所在的路径
type -a ls #查看命令的绝对路径(包括别名)
sort
管道:将左边命令的输出结果,交给管道右边的命令

[root@juw5207 ~]# cat >> file.txt <<EOF

b:3
c:2
a:4
e:5
d:1
f:11
EOF
[root@juw5207 ~]# cat file.txt
b:3
c:2
a:4
e:5
d:1
f:11
[root@juw5207 ~]# sort file.txt
a:4
b:3
c:2
d:1
e:5
f:11
[root@juw5207 ~]# sort -t":" -k2 file.txt
d:1
f:11
c:2
b:3
a:4
e:5
[root@juw5207 ~]# sort -t":" -k2 -n file.txt
d:1
c:2
b:3
a:4
e:5
f:11
[root@juw5207 ~]# sort -t “:” -k2 -n -r file.txt
f:11
e:5
a:4
b:3
c:2
d:1
[root@juw5207 ~]# sort -t":" -k2 -n -r file.txt | head -3
f:11
e:5
a:4
[root@juw5207 ~]# cat >> file2.txt <<EOF
oldxu:20
oldguo:10
oldli:30
oldboy:0
EOF

[root@juw5207 ~]# sort -t":" -k2 -n -r file2.txt
oldli:30
oldxu:20
oldguo:10
oldboy:0
[root@juw5207 ~]#

[root@juw5207 ~]# sort -t":" -k2 -n -r file2.txt | head -3
oldli:30
oldxu:20
oldguo:10


#uniq 去重
先派序,再去重
[root@juw5207 ~]# cat >>file3.txt <<EOF

abc
123
abc
123
EOF
[root@juw5207 ~]# sort file3.txt
123
123
abc
abc
[root@juw5207 ~]# sort file3.txt | uniq
123
abc
[root@juw5207 ~]# sort file3.txt | uniq -c
2 123
2 abc
[root@juw5207 ~]#
每个命令能做的事情都很单一,所以需要使用,管道技术,将命令与命令之间连接起来,能做的事情是无限的
[root@juw5207 ~]# cat >>ip2.txt<<EOF
192.168.3.1
192.168.3.2
192.168.3.3
192.168.2.20
192.168.2.21
192.168.2.22
192.168.0.151
192.168.0.151
192.168.0.152
192.168.0.153
192.168.0.151
192.168.2.22
192.168.1.10
192.168.1.11
192.168.1.12
192.168.0.151
192.168.1.1
192.168.2.2
192.168.0.151
192.168.3.3
192.168.2.20
192.168.1.21
192.168.0.151
192.168.2.22
192.168.0.151
192.168.2.22
192.168.1.152
192.168.0.153
192.168.3.10
192.168.1.11
192.168.2.22
192.168.3.12
EOF
[root@juw5207 ~]# sort ip2.txt | uniq -c | sort -nr | head -3
14 192.168.0.151
10 192.168.2.22
4 192.168.3.3


#CUT

#选项:
-d 指定分隔符
-f 数字,取第几列 –f3,6三列和6列
-c 按字符取(空格也算)

[root@juw5207 ~]# echo “Im oldxu, is QQ 552408925” > oldboy.txt
[root@juw5207 ~]# cut -d " " -f 2,5 oldboy.txt
oldxu, 552408925
[root@juw5207 ~]# cut -d " " -f 2,5 oldboy.txt | sed ‘s#,##g’
oldxu 552408925

[root@juw5207 ~]# awk ‘{print $2,$5}’ oldboy.txt
oldxu, 552408925
[root@juw5207 ~]# awk ‘{print $2,$5}’ oldboy.txt | sed ‘s#,##g’
oldxu 552408925

[root@juw5207 ~]# awk -F “,” ‘{print $1,$2}’ oldboy.txt
Im oldxu is QQ 552408925
[root@juw5207 ~]# awk -F “,” ‘{print $1,$2}’ oldboy.txt | awk ‘{print $2,$5}’
oldxu 552408925
[root@juw5207 ~]# awk -F “[ ,]” ‘{print $2,$6}’ oldboy.txt
oldxu 552408925
[root@juw5207 ~]# awk -F “[ ,]+” ‘{print $2,$5}’ oldboy.txt
oldxu 552408925

高级用法

[ ,]+ +表示重复 前面的字符一次或多次
空格算一个分隔符
逗号算一个分隔符
空格和逗号挨在一起,也算一个分隔符
空格逗号空格,全算一个分隔符

[root@oldboy ~]# #awk -F ‘’ “{}”
[root@oldboy ~]# #sed ‘s###g’
[root@oldboy ~]# #grep “”

[root@oldboy ~]# #awk -F ‘’ ‘{}’
[root@oldboy ~]# #sed ‘s###g’
[root@oldboy ~]# #grep ‘’
都可以


wc [OPTION]… [FILE]…
#选项:
-l显示文件行数
-c显示文件字节
-w显示文件单词
[root@juw5207 ~]# wc -l /etc/services
11176 /etc/services
[root@juw5207 ~]# cat -n /etc/services | tail -1 |awk ‘{print $1}’
11176

练习题: 过滤出/etc/passwd以nologin结尾的内容,并统计有多少行
过滤: grep /etc/passwd
条件: nologin结尾的
并且:
统计出现的内容总共有多少行: wc -l
[root@juw5207 ~]# grep “nologin$” /etc/passwd | wc -l
14
[root@juw5207 ~]#

习题: 分析如下日志,统计每个域名被访问的次数。

[root@student tmp]# cat >>web.log<<EOF
http://www.xuliangwei.com/index.html
http://www.xuliangwei.com/1.html
http://post.xuliangwei.com/index.html
http://mp3.xuliangwei.com/index.html
http://www.xuliangwei.com/3.html
http://post.xuliangwei.com/2.html
EOF
1,提取域名
2,排序去重
3,统计次数
[root@juw5207 ~]# cat >>web.log<<EOF

http://www.xuliangwei.com/index.html
http://www.xuliangwei.com/1.html
http://post.xuliangwei.com/index.html
http://mp3.xuliangwei.com/index.html
http://www.xuliangwei.com/3.html
http://post.xuliangwei.com/2.html
EOF
[root@juw5207 ~]# awk -F ‘/’ ‘{print $3}’ web.log | sort | uniq -c
1 mp3.xuliangwei.com
2 post.xuliangwei.com
3 www.xuliangwei.com
[root@juw5207 ~]# awk -F ‘/’ ‘{print $3}’ web.log | sort | uniq -c | sort -nr
3 www.xuliangwei.com
2 post.xuliangwei.com
1 mp3.xuliangwei.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值