特殊符號

代表0個或多個任意字符
[root@localhost ~]# ls
1.txt 3.txt 5.txt bb.txt filename test.txt
2.txt 4.txt anaconda-ks.cfg cc.txt test.tar
[root@localhost ~]# ls .txt
1.txt 2.txt 3.txt 4.txt 5.txt bb.txt cc.txt test.txt
[root@localhost ~]# ls test.

test.tar test.txt

? 代表一個字符

[root@localhost ~]# ls test.ta?
test.tar
[root@localhost ~]# ls ?.txt
1.txt 2.txt 3.txt 4.txt 5.txt

#註釋符號,後面內容不被執行

[root@localhost ~]# #dnfsfndfi
[root@localhost ~]# ##ksdjsldkl
[root@localhost ~]# djij
-bash: djij: 未找到命令

\ 脫義字符,
這個字符會將後面的特殊字符(如*)還原爲普通字符

[root@localhost ~]# ls -d 1.txt*
ls: 無法訪問1.txt*: 沒有那個文件或目錄

cut 命令是用來截取某一字段,其格式爲 cat -d '分割字符'[-cf] n ,n爲數字
-d : 後面跟分割符,分隔符要用單引號括起來
-c :後面接的是第幾塊字符
-f :後面跟的是第幾區塊

[root@localhost ~]# cat /etc/passwd |head -2 |cut -d ':' -f 1
root
bin
[root@localhost ~]# cat /etc/passwd |head -2 |cut -d ':' -f 1,2
root:x
bin:x
[root@localhost ~]# cat /etc/passwd |head -2 |cut -d ':' -f 1-4
root:x:0:0
bin:x:1:1

[root@localhost ~]# cat /etc/passwd head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@localhost ~]# cat /etc/passwd
head -2 cut -c 4
t

sort 命令排序 格式:sort [-t 分隔符] [-kn1,n2] [nru] n1 n2 爲數字
-t:後面跟分隔符,作用同cut
-n:後面使用純數字排序
-r :反向排序
-u :表示重複
-kn1,n2:表示n1 區間排序到n2區間,可以只寫-kn1 即對n1字段排序

[root@localhost ~]# sort /etc/passwd
adm:x:3:4:adm:/var/adm:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
halt:x:7:0:halt:/sbin:/sbin/hal

wc -l 統計行數
[root@localhost ~]# wc -l 1.txt
10 1.txt
wc -w 統計詞
[root@localhost ~]# wc -m /etc/passwd
883 /etc/passwd
wc -m 統計字符數
[root@localhost ~]# wc -m 1.txt
385 1.txt

uniq 用來刪除重複的行,配合-c使用,統計重複的行數
sort 2.txt |uniq 先排序後刪除重複
[root@localhost ~]# vim 2.txt
[root@localhost ~]# uniq 2.txt
wassadjaajjj123
111111
222222
333333
111111
121212
222222
[root@localhost ~]# uniq -c 2.txt
1 wassadjaajjj123
1 111111
3 222222
1 333333
2 111111
2 121212
1 222222
[root@localhost ~]# sort 2.txt |uniq
111111
121212
222222
333333
wassadjaajjj123

tee 後面跟文件名,類似於重定向> 還有一個作用是顯示把內容顯示在屏幕上
>跟文件是清空文件的內容
tee -a 表示追加

[root@localhost ~]# sort 2.txt |uniq -c |tee 3.txt
3 111111
2 121212
4 222222
1 333333
1 wassadjaajjj123
[root@localhost ~]# cat 3.txt
3 111111
2 121212
4 222222
1 333333
1 wassadjaajjj123
[root@localhost ~]# > 3.txt
[root@localhost ~]# cat 3.txt

tee -a 表示追加

[root@localhost ~]# sort 2.txt |uniq -c |tee 3.txt
3 111111
2 121212
4 222222
1 333333
1 wassadjaajjj123
[root@localhost ~]# sort 2.txt |uniq -c |tee -a 3.txt
3 111111
2 121212
4 222222
1 333333
1 wassadjaajjj123
[root@localhost ~]# cat 3.txt
3 111111
2 121212
4 222222
1 333333
1 wassadjaajjj123
3 111111
2 121212
4 222222
1 333333

tr 命令替換字符,常用來處理文檔中處理文檔出現的特殊字符

[root@localhost ~]# ac='acer'
[root@localhost ~]# echo $ac
acer
[root@localhost ~]# echo ac
ac
[root@localhost ~]# echo ac |tr '[a-z]' '[A-Z]'
AC
[root@localhost ~]# cat /etc/passwd |head -2 |tr '[a-z]' '[A-Z]'
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN

split 用於切割文檔,常用的選項爲-b和-l
-b 表示依據大小來分割文檔,單位爲byte

!$ 表示上一條命令的最後一個變量
[root@localhost ~]# ls 1.txt
1.txt
[root@localhost ~]# ls !$
ls 1.txt
1.txt
[] 代表字符組合中的任意一個

[root@localhost ~]# cat /etc/passwd |head -2 |tr '[a-z]' '[A-Z]'
ROOT:X:0:0:ROOT:/ROOT:/BIN/BASH
BIN:X:1:1:BIN:/BIN:/SBIN/NOLOGIN

command1;command2 使用;表示不管command1是否執行成功 command2都會執行
command1&&command2 使用&&表示command1執行成功 後command2纔會執行,否則不執行
command1||command2 使用||表示command1執行不成功 command2纔會執行

[root@localhost ~]# ls
anaconda-ks.cfg cc.txt split_dir test.txt xab xad xaf xah
bb.txt filename test.tar xaa xac xae xag xai
[root@localhost ~]# touch test1 test2
[root@localhost ~]# ls test2 ; touch test2
test2
[root@localhost ~]# ls test2 && touch test2
test2
[root@localhost ~]# cat 3.txt || touch 3.txt
cat: 3.txt: 沒有那個文件或目錄
[root@localhost ~]# ls
3.txt bb.txt filename test1 test.tar xaa xac xae xag xai
anaconda-ks.cfg cc.txt split_dir test2 test.txt xab xad xaf xah

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章