五周第五次課(3月9日)
8.11 sort_wc_uniq命令
8.12 tee_tr_split命令
8.13 shell特殊符號下
8.10 shell特殊符_cut命令
特殊符號
*任意個任意字符
?任意一個字符
#註釋字符
在配置文件首端加入# 代表註釋,後面的參數無意義,不生效。只是起到解釋說明作用
\脫義字符
[root@centos7 ~]# a=1
[root@centos7 ~]# b=2
[root@centos7 ~]# echo $c
12
[root@centos7 ~]# c='$a$b'
[root@centos7 ~]# echo $c
$a$b
[root@centos7 ~]# c=\$a\$b
[root@centos7 ~]# echo $c
$a$b
|管道符
幾個和管道符相關的命令
cut 分割,截取作用。 -d 分隔符 -f指定段號 -c 指定第幾個字符
[root@centos7 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1
root
bin
查看/etc/passwd的頭2行,用:分割,截取第一段。
[root@centos7 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3
root:x:0
bin:x:1
查看/etc/passwd的頭2行,用:分割,截取第一到第三段。
8.11 sort_wc_uniq命令
cut -c 指定第幾個字符(使用-c的話 就不要使用-d -f了)
[root@centos7 ~]# cat /etc/passwd |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@centos7 ~]# cat /etc/passwd |head -2 |cut -c 4
t
:
sort排序
什麽時候用#sort ?
例如現在拿到一列數字或者字符串,讓它們按不同要求進行排序。
sort經常結合uniq一起使用。
test
[root@centos7 ~]# sort /etc/passwd
可以看出sort後的文件名從小到大順序排序。(sort根據ASCII碼來進行排序)
test2
[root@centos7 ~]# head /etc/passwd >> 1.txt
[root@centos7 ~]# vi 1.txt
[root@centos7 ~]# sort 1.txt
<
>
{
1.txt
2222211111
2222222aaaaa
2.txt
444448888sss
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/halt
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
sync:x:5:0:sync:/sbin:/bin/sync
*wwwweeq
此排序是根據ASCII碼來排序得來。
-n 數字排序
使用此模式後,除數字都會被無視,被認為是0,而0是比1再前,所以第一時間顯示會是非數字的內容。
test3
[root@centos7 ~]# sort -n 1.txt
<
>
{
adm:x:3:4:adm:/var/adm:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
*wwwweeq
1.txt
2.txt
2222222aaaaa
444448888sss
2222211111
-r 反序
與-n相反效果。
-t 分隔符 -kn1/-kn1,n2
用得不錯 簡單了解即可
wc -l 統計行數 -m 統計字符數(字數) -w 統計詞
test4
[root@centos7 ~]# vi 2.txt 敲入6個字符
123
abc
[root@centos7 ~]# wc -m 2.txt 為什麽統計字符會是8個字符,原因是因為有了2個$,換行會產生$,只是一般會被隱藏。
8 2.txt
[root@centos7 ~]# cat -A 2.txt cat -A可以查看隱藏字符。
123$
abc$
[root@centos7 ~]# wc -w 2.txt
2 2.txt
統計有2個詞。以空格或空白字符(, . $ < > )相隔一個整體未一個詞。
uniq 去重(重復),-c統計行數
test5
[root@centos7 ~]# vi 2.txt
123
abc 111,222
123
abc
1
1
2
使用uniq之前,一定要排序後,再uniq去重。所以搭配sort就最佳效果了。
test6
[root@centos7 ~]# sort 2.txt
1
1
123
123
2
abc
abc 111,222
[root@centos7 ~]# sort 2.txt |uniq
1
123
2
abc
abc 111,222
統計重復出現次數 -c
[root@centos7 ~]# sort 2.txt |uniq -c
2 1
2 123
1 2
1 abc
1 abc 111,222
8.12 tee_tr_split命令
tee和 >類似,重定向的同時還在屏幕顯示。
test1
[root@centos7 ~]# sort 2.txt |uniq -c > a.txt
[root@centos7 ~]# cat a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
[root@centos7 ~]# sort 2.txt |uniq -c |tee a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
由此可以見 # sort 2.txt |uniq -c |tee a.txt的效果,相當於# sort 2.txt |uniq -c > a.txt加# cat a.txt的效果。tee的作用是追加並顯示追加內容。
test2
重復測試多一次
[root@centos7 ~]# >a.txt 前空白>接文件,表示清空文件內容。
[root@centos7 ~]# cat a.txt
[root@centos7 ~]# sort 2.txt |uniq -c |tee a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
[root@centos7 ~]# cat a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
效果一樣 沒有變化。
tee -a 表示再次追加
test3
[root@centos7 ~]# sort 2.txt |uniq -c |tee -a a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
[root@centos7 ~]# cat a.txt
2 1
2 123
1 2
1 abc
1 abc 111,222
2 1
2 123
1 2
1 abc
1 abc 111,222
2 1
2 123
1 2
1 abc
1 abc 111,222
tr 替換字符, tr 'a' 'b',大小寫替換tr'[a-z]''[A-Z]'
test4
[root@centos7 ~]# echo "aminglinux" |tr '[al]' '[AL]'
AmingLinux
把aminglinux的aming中的a改成A,linux的l改成L。
test5
[root@centos7 ~]# echo "aminglinux" |tr 'a' 'A'
Aminglinux
a變成A
test6
[root@centos7 ~]# echo "aminglinux" |tr '[a-z]' '[A-Z]'
AMINGLINUX
全部變成大寫。
test7
[root@centos7 ~]# echo "aminglinux" |tr '[a-z]' '1'
1111111111
把aminglinux變成1
記住tr支持多個替換,而且它們是一一對應的。
split 切割 -b大小(默認單位字節),-l行數
大文件切割成小文件。
[root@centos7 ~]# split -b 100M bigfile
針對文件,切割100M。
[root@centos7 ~]# split -l 1000 bigfire
針對行數,切割1000行。
test 8
[root@centos7 ~]# find /etc/ -type f -name "*conf" -exec cat {} >> a.txt \;
[root@centos7 ~]# du -sh a.txt
256K a.txt
[root@centos7 ~]# mv a.txt test/
[root@centos7 ~]# cd test/
[root@centos7 test]# ls
a.txt
[root@centos7 test]# split -b 1000 a.txt
不加容量單位,默認是字節
指定大小分割
[root@centos7 test]# split -b 100k a.txt
[root@centos7 test]# ls
a.txt xaa xab xac
[root@centos7 test]# du -sh *
240K a.txt
100K xaa
100K xab
40K xac
刪除x*開頭的文件
[root@centos7 test]# rm -rf x*
[root@centos7 test]# ls
a.txt
指定分割後的名稱
[root@centos7 test]# split -b 100k a.txt FG
[root@centos7 test]# ls
a.txt FGaa FGab FGac
[root@centos7 test]# du -sh *
240K a.txt
100K FGaa
100K FGab
40K FGac
分割行數
分割1000行
[root@centos7 test]# split -l 1000 a.txt
[root@centos7 test]# ls
a.txt FGaa FGab FGac xaa xab xac xad xae xaf xag
[root@centos7 test]# wc -l *
6112 a.txt
2401 FGaa
2695 FGab
1016 FGac
1000 xaa
1000 xab
1000 xac
1000 xad
1000 xae
1000 xaf
112 xag
8.13 shell特殊符號(下)
$變量前綴,!$組合,正則裏面行尾
;多條命令寫到一行,用分號;分割
[root@centos7 ~]# ls 1.txt ; wc -l 2.txt
1.txt
7 2.txt
~用戶給家目錄,後面正則表達式表達匹配符
&放到命令後面,會把命令丟到後臺.
> >> 2> 2>> &>
> 正確重定向,會把之前的文件覆蓋掉。
>> 追加重定向,正確輸出
2> 錯誤重定向,錯誤輸出
2>> 錯誤追加重定向,錯誤輸出
&> 正確與錯誤輸出重定向。
[]指定字符中的一個,[0-9],[a-z][A-Z],[abc]
||(兩個管道符)和&&,用於命令之間。(判斷作用,||表示或,&&表示才。)
||用法,
用於兩命令之間,如果執行兩條命令,其中前面是錯誤命令的話,和執行第二條命令。如果前面命令是正確的話,後面的命令將不會執行。
[root@centos7 ~]# ls aaaa.txt || wc -l 2.txt
ls: 無法訪問aaaa.txt: 沒有那個文件或目錄
7 2.txt
[root@centos7 ~]# wc -l 2.txt || ls 1.txt
7 2.txt
&&用法,
兩條命令,如果前面的命令正確,後面的命令才會被執行。如果前面的命令錯誤,後面的命令將不會被執行。
[root@centos7 ~]# ls 1.txt && wc -l 2.txt
1.txt
7 2.txt
前面命令成功了,後面的命令繼續被執行。
[root@centos7 ~]# ls 1a.txt && wc -l 2.txt
ls: 無法訪問1a.txt: 沒有那個文件或目錄
前面命令失敗了,後面的命令沒有被執行。
test
創建一個目錄aminglinux/,條件是不存在才創建,如果存在就不創建。
[root@centos7 ~]# [ -d aminglinux ] || mkdir aminglinux
[root@centos7 ~]# ls
1.txt 2.txt 4.txt AA.txt anaconda-ks.cfg A.txt err test
1.xtx 3.txt a_(2).txt aminglinux anaconda-ks.cfg.1 bb.txt temp.1
解釋 [ -d aminglinux ] -d是否一個目錄,目錄是否存在。
如果文件存在,才去創建,此時會出現報錯。
[root@centos7 ~]# [ -d aminglinux ] && mkdir aminglinux
mkdir: 無法創建目錄"aminglinux": 文件已存在
五周第五次課(3月9日)