文件上傳下載,命令之wget / curl / which / sort / uniq / cut / wc /tr /sed


命令

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



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM