22.Shell特殊符號和cut,sort,wc,uniq,tee,tr,split命令
8.10 shell特殊符號cut命令
8.11 sort_wc_uniq命令
8.12 tee_tr_split命令
8.13 shell特殊符號下
相關測驗題目:http://ask.apelearn.com/question/5437
擴展
- source exec 區別 http://alsww.blog.51cto.com/2001924/1113112
- Linux特殊符號大全http://ask.apelearn.com/question/7720
- sort並未按ASCII排序 http://blog.csdn.net/zenghui08/article/details/7938975
一 特殊符號
-
[ ] * 任意個任意字符,通配符
-
[ ] ? 任意一個字符
-
[ ] # 註釋字符
-
[ ] \ 脫義字符
- [ ] | 管道符
oot@localhost:~# a=1
root@localhost:~# b=2
root@localhost:~# c=$a$b
root@localhost:~# echo $c
12
root@localhost:~# c=\$a\$b
root@localhost:~# echo $c
$a$b
二 幾個和管道有關的命令
- [ ] cut 分割,-d 分隔符 -f 指定段號 -c 指定第幾個字
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 -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 -c 4 t :
- [ ] sort 排序, -n 以數字排序 -r 反序 -t 分隔符 -kn1/-kn1,n2
root@localhost:~# sort -r /etc/passwd xavi:x:1000:1000:xavi,xavi‘s office,62580558,62589906:/home/xavi:/bin/bash xavidsf:x:1001:1001:xavi:/home/xavidsf:/bin/bash usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin unbound:x:993:991:Unbound DNS resolver:/etc/unbound:/sbin/nologin tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin test:x:1002:1002::/home/test:/bin/bash
- [ ] wc -l 統計行數 -m 統計字符數 -w 統計詞
root@localhost:~# vi 2.txt
root@localhost:~# cat 2.txt
2221dedd
dede
ded
1213
32e4
ded
root@localhost:~# wc -l 2.txt
6 2.txt
root@localhost:~# wc -m 2.txt
32 2.txt
root@localhost:~# wc -w 2.txt
6 2.txt
- [ ] uniq 去重, -c統計行數,文件內容是沒有改變的
root@localhost:~# sort 2.txt|uniq //必須先給文件排序,才能生效
1
11
1213
2
2221dedd
3
3232
32e4
444
ded
dede
root@localhost:~# sort 2.txt|uniq -c //統計重復次數,
1 1
1 11
1 1213
2 2
1 2221dedd
2 3
1 3232
1 32e4
1 444
2 ded
1 dede
- [ ] tee 和>類似,重定向的同時還在屏幕顯示
root@localhost:~# echo "dadsaddsdad" |tee a.txt
dadsaddsdad
root@localhost:~# sort 2.txt|uniq -c |tee a.txt
1 1
1 11
1 1213
2 2
1 2221dedd
2 3
1 3232
1 32e4
1 444
2 ded
1 dede
-
[ ] te -a 和>>類似,追加重定向
root@localhost:~# sort 2.txt|uniq -c |tee -a a.txt 1 1 1 11 1 1213 2 2 1 2221dedd 2 3 1 3232 1 32e4 1 444 2 ded 1 dede root@localhost:~# cat a.txt 1 1 1 11 1 1213 2 2 1 2221dedd 2 3 1 3232 1 32e4 1 444 2 ded 1 dede 1 1 1 11 1 1213 2 2 1 2221dedd 2 3 1 3232 1 32e4 1 444 2 ded 1 dede
- [ ] tr 替換字符,tr ‘a‘ ‘b‘,大小寫替換tr ‘[a-z]‘ ‘[A-Z]‘ //[]的意思是任選幾個括號內的字符
[root@localhost ~]# echo "xavilinux" |tr ‘[al]‘ ‘[AL]‘
xAviLinux
[root@localhost ~]# echo "xavilinux" |tr ‘a‘ ‘A‘
xAvilinux
- [ ] split 切割,-b大小(默認單位字節),-l行數
-b 表示依據大小來分割文檔,單位為byte
[root@localhost ~]# mkdir split_dir
[root@localhost ~]# cd !$
cd split_dir
[root@localhost split_dir]# cp /etc/passwd ./
[root@localhost split_dir]# split -b 500 passwd
[root@localhost split_dir]# ls
passwd xaa xab xac xad xae
查看起實際大小,du -sh查看的是塊,du -sb 按照byte查找大小
如果split不指定目標文件名,則會以xaa、xab……這樣的文件名來存取切割後的文件。指定目標文件名,示例如下:
[root@localhost split_dir]# rm -f x* //把前面分割好的文件全部刪除
[root@localhost split_dir]# split -b 100 passwd 123
[root@localhost split_dir]# ls
123aa 123ad 123ag 123aj 123am 123ap 123as 123av passwd
123ab 123ae 123ah 123ak 123an 123aq 123at 123aw
123ac 123af 123ai 123al 123ao 123ar 123au 123ax
-l 表示依據行數來分割文檔
[root@localhost split_dir]# rm -f 123*
[root@localhost split_dir]# split -l 10 passwd //10行分割
[root@localhost split_dir]# wc -l *
46 passwd
10 xaa
10 xab
10 xac
10 xad
6 xae
92 總用量
三 shell的特殊符號
1、特殊符號$
$可以用作變量前面的標識符,還可以和!結合使用。
[root@localhost /]# touch 1.txt
[root@localhost /]# ls 1.txt
1.txt
[root@localhost /]# ls !$
ls 1.txt
1.txt
!$表示上條命令的最後一個變量,本例中上條命令最後是1.txt,那麽在當前命令下輸入!$則表示1.txt
2、特殊符號;
在一行命令中運行兩個或兩個以上的命令,需要在命令之間加符號;。
[root@localhost /]# ls 1.txt ; wc -l 2.txt
1.txt
8 2.txt
[root@localhost /]# ls 1.txt;wc -l 2.txt //命令之間不空格也是有效的,
1.txt
8 2.txt
3、特殊符號~
符號~代表用戶的家目錄,root用戶的家目錄是/root,普通用戶的家目錄是/home/username。
4、特殊符號&
把一條命令放到後臺執行,則需要加上符號&,它通常用於命令運行時間較長的情況。比如,可以用在sleep後,如下所示:
[root@localhost /]# sleep 30 &
[2] 7047
[root@localhost /]# jobs
[1]+ 已停止 wc -l(工作目錄:~/split_dir)
[2]- 運行中 sleep 30 &
5、重定向符號>、>>、2>和2>>,錯誤和正確都輸入&>
和>>分別表示取代和追加的意思。當我們運行一個命令報錯時,報錯信息會輸出到當前屏幕。如果想重定向到一個文本,則要用重定向符號2>或者2>>,它們分別表示錯誤重定向和錯誤追加重定向。
6、中括號[ ]
中括號內為字符組合,代表字符組合中的任意一個,還可以表示一個範圍(1-3,a-z)。
7、特殊符號&& ||
在上面剛剛提到了分號,用於多條命令間的分隔符。另外還有兩個可以用於多條命令中間的特殊符號,那就是 “&&” 和 “||” 下面把這幾種情況全列出:
command1 ; command2
command1 && command2
command1 || command2
使用 ”;” 時,不管command1是否執行成功都會執行command2;
使用 “&&” 時,只有command1執行成功後,command2才會執行,否則command2不執行;
使用 “||” 時,command1執行成功後command2 不執行,否則去執行command2,總之command1和command2總有一條命令會執行。
[root@localhost /]# ls 1a.txt; wc -l 2.txt
ls: 無法訪問1a.txt: 沒有那個文件或目錄
[2]- 完成 sleep 30
8 2.txt
[root@localhost /]# ls 1.txt || wc -l 2.txt
1.txt
[root@localhost /]# ls 1.txt && wc -l 2.txt
1.txt
8 2.txt
[root@localhost /]# ls 1a.txt && wc -l 2.txt
ls: 無法訪問1a.txt: 沒有那個文件或目錄
22.Shell特殊符號和cut,sort,wc,uniq,tee,tr,split命令