首页 > 技术文章 > 文件上传下载,命令之wget / curl / which / sort / uniq / cut / wc /tr /sed

gongjingyun123-- 2019-06-21 00:15 原文

命令

1.文件的上传下载

需要下载安装包
[root@oldboyedu ~]# yum install -y lrzsz    #安装包

rz:只能上传文件	(直接拖拽文件)
    1)不支持上传超过4G的文件
    2)不支持断点续传
  rz不能上传目录,需要把目录压缩打包才可以上传

      
sz:下载文件
示例:sz filename	

2.从外网下载文件wget

wget 文件下载
-O 指定地址下载,更改名称
-T 超时时间
-q 安静下载(关闭wget输出)
--spider 网络爬虫
示例:
Wget http://www.baidu.com
如果没有,则安装:yum install -y wget
-O:指定下载的路径,可以改名

3.curl文件下载

-o:指定下载的路径,可以改名

示例:

Curl -o http://www.baidu.com

4.查找命令which

Which查找系统目录下的命令(绝对路径)
[root@centos7 ~]# which rm
alias rm='rm -i'
	/usr/bin/rm


了解
type -a 也可以查命令的路径
whereis也可以查命令的路径

[root@centos7 ~]# whereis rm
rm: /usr/bin/rm /usr/share/man/man1/rm.1.gz
[root@centos7 ~]# type -a rm
rm is aliased to `rm -i'
rm is /usr/bin/rm

[root@oldboyedu ~]# type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
[root@oldboyedu ~]# type -a for
for is a shell keyword

5.字符处理命令-排序sort

-t 指定分隔符
-k 指定第几列的内容(按分隔符),不指定分隔符,默认是空格为分隔符
-n 按照阿拉伯数字的大小顺序排序
-r 倒叙排序
输入文件

[root@centos7 ~]# cat >> sort.txt <<eof
\> A:d:8
\> E:x:2
\> B:c:6
\> eof	

 
排序文件
[root@centos7 ~]# sort sort.txt
A:d:8
B:c:6
E:x:2	

按照字母小写顺序排序
[root@centos7 ~]# sort -t ':' -k 2 sort.txt
B:c:6
A:d:8
E:x:2 

按照字母小写顺序排序
[root@centos7 ~]# sort -t ':' -k 2 -n sort.txt
A:d:8
B:c:6
E:x:2

按照字母小写倒叙
[root@centos7 ~]# sort -t ':' -k 2 -n -r sort.txt
E:x:2
B:c:6
A:d:8

6.字符处理-去重uniq

去重相邻行,不相邻不会去重

-c 显示去重后的数量(count)
-d 只显示重复的行
-u 只显示不重复的行
输入内容:
[root@centos7 ~]# cat >>unip.txt <<eof
\> abc
\> abc
\> 123
\> eof 

文件去重(没有排序无法去重)
[root@centos7 ~]# uniq uniq.txt
abc
123
abc
123

排序文件
[root@centos7 ~]# sort uniq.txt
123
123
abc
Abc

先排序文件,后去重
[root@centos7 ~]# sort uniq.txt |uniq
123
abc	 

先排序文件,后去重并显示去重后的数量
[root@centos7 ~]# sort uniq.txt |uniq -c
2 123
2 abc

7.字符处理-截取cut

-d 指定分隔符
-f 指定第几列
-c 根据字符来取数据
输入内容
[root@centos7 ~]# cat >>info.txt <<eof
\> I’m gjy,20 years old qq 861962063 
\> eof 

\#以空格为分隔符,截取第二个,第六个字符
[root@centos7 ~]# cut -d ' ' -f 2,6 info.txt
gjy,20 861962063	

以空格为分隔符,截取第二个,第六个,再以逗号为分隔符,截取第一个第二个
[root@centos7 ~]# cut -d ' ' -f 2,6 info.txt |cut -d ',' -f 1,2
gjy,20 861962063	


[root@centos7 ~]# cut -d ' ' -f 2,6 info.txt |cut -c 1-3,8-16
gjy861962063

8.字符处理-统计wc

-l 统计行数
-c 统计字节数
-w 统计单词次数,也就是列数
示例:
[root@centos7 ~]# wc /etc/services
 11176  61033 670293 /etc/services 

统计字节:
[root@centos7 ~]# wc -c /etc/services
670293 /etc/services	l

统计行数
[root@centos7 ~]# wc -l /etc/services
11176 /etc/services

统计列数
[root@centos7 ~]# wc -l /etc/services
11176 /etc/services

 

9.tr替换

[root@centos7 ~]# tr '1' 'o' <uniq.txt           #1就全部替换成了o
abc
o23
abc
o23
[root@centos7 ~]# echo "1" >>uniq.txt       #再追加一个 1
[root@centos7 ~]# tr '123' '0ld' <uniq.txt     #单个对单个的替换
abc
0ld
abc
0ld
0

10. sed 文本处理工具,三剑客之一

选项:

-n 取消默认输出
p 打印当前内容
d 删除当前行
i 修改文件的内容
s 替换
g 表示全局
多条命令分隔符,取不连续的多行
-r 支持扩展正则表达式使用
[root@centos7 ~]# cat>sed.txt<<'EOF'           #输入文件内容
> 101,$oldboy,CEO
> 102,$zhangyao,CTO
> 103,$Alex,COO
> 104,$yy,CFO
> 105,$feixue,CIO
> 106,$lidao,UFO
> EOF

[root@centos7 ~]# cat sed.txt              #查看文件
101,$oldboy,CEO
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO
105,$feixue,CIO
106,$lidao,UFO

[root@centos7 ~]# sed -n '2p' sed.txt                  #取出第二行
102,$zhangyao,CTO

[root@centos7 ~]# sed -n '2,4p' sed.txt           #取出第二到四行
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO
 
[root@centos7 ~]# sed -n '2p;4p' sed.txt        #取出第二行和第四行
104,$yy,CFO

[root@centos7 ~]# sed '2d' sed.txt               # 删除第二行
101,$oldboy,CEO
103,$Alex,COO
104,$yy,CFO
105,$feixue,CIO
106,$lidao,UFO


按字符串取
[root@centos7 ~]# sed -n '/oldboy/p' sed.txt           #取出oldboy所在的一行
101,$oldboy,CEO
[root@centos7 ~]# sed -nr '/oldboy|feixue/p' sed.txt       #同时取出oldboy和feixue所在的一行
101,$oldboy,CEO
105,$feixue,CIO
[root@centos7 ~]# sed  '/oldboy/d' sed.txt                #删除oldboy所在的一行,相当于取反
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO
105,$feixue,CIO
106,$lidao,UFO
[root@centos7 ~]# sed 's#lidao#qiudao#g' sed.txt     #替换‘s###g'   ,有结果显示,但是原文件没变
101,$oldboy,CEO
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO
105,$feixue,CIO
106,$qiudao,UFO
[root@centos7 ~]# sed -i 's#lidao#qiudao#g' sed.txt         #  加上-i 参数,输入后没有任何结果显示,但查看原文件,会发现变了

10. awk 去列,统计,计算。

文本处理工具,三剑客之一

选项:

取反 ’!/ /'
NR 取行'{print $0,NR}'
$ 取列 '{print ,NR}'
d 删除 '/ /d'
$NF 最后一列
$0 整行内容
print 显示内容
取反
column-t 对齐
[root@centos7 ~]# awk '{print $0,NR}' sed.txt
101,$oldboy,CEO 1
102,$zhangyao,CTO 2
103,$Alex,COO 3
104,$yy,CFO 4
105,$feixue,CIO 5
106,$qiudao,UFO 6


取行:
[root@centos7 ~]# awk 'NR==2,NR==4' sed.txt
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO
[root@centos7 ~]# awk 'NR==2,NR==4' sed.txt
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO
[root@centos7 ~]# awk 'NR>1&& NR<5' sed.txt
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO
[root@centos7 ~]# awk 'NR>=2 && NR<=4' sed.txt
102,$zhangyao,CTO
103,$Alex,COO
104,$yy,CFO


过滤
[root@centos7 ~]# awk '/oldboy/' awk.txt
101,$oldboy,CEO
[root@centos7 ~]# awk '/oldboy|qiudao/' awk.txt
101,$oldboy,CEO
106,$qiudao,UFO

取ip地址
[root@centos7 ~]# ip a s ens33 
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN group default qlen 1000
    link/ether 00:0c:29:d9:4a:0d brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::acc3:5abb:9655:e2d5/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
#先取第三行:
[root@centos7 ~]# ip a s ens33 | awk 'NR==3'
    inet 10.0.0.100/24 brd 10.0.0.255 scope global noprefixroute ens33

#把/换成空格,取第3行,第6列
[root@centos7 ~]# ip a s ens33 | awk -F '[ /]' 'NR==3{print $6}'
10.0.0.100


推荐阅读