shell特殊符號/cut,sort,wc,uniq,tee,tr,split命令

shell特殊符號cut命令

shell特殊符號linux

* *任意個任意字符
* ?任意一個字符
* #註釋字符
* \脫義字符
* |管道符

命令cut
cut用來截取某一個字段,其格式爲:#cut –d ‘分隔字符’[-cf]n,n是數字。正則表達式

選項:
-d 後面跟分隔符,分隔字符要有單引號括起來。
-c 後面接的是第幾個字符。
-f 後面接的是第幾個區塊。shell

[root@Ask-02 ~]# cat /etc/passwd |head -2
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
[root@Ask-02 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1
root
bin
[root@Ask-02 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1,2
root:x
bin:x
[root@Ask-02 ~]# cat /etc/passwd |head -2 |cut -d ":" -f 1-3
root:x:0
bin:x:1
[root@Ask-02 ~]# cat /etc/passwd |head -2 |cut -c 4
tbash

sort_wc_uniq命令

sort命令
sort用做排序,其格式爲sort[-t分隔符][-kn1,n2][-nru],這裏n1和n2指的是數字,其餘選項的含義以下。ide

選項:
-n 表示使用純數字排序
-r 表示反向排序
-u 表示去重複
-t 後面跟分割字符,做用跟cut的-d選項同樣
-kn1,n2 表示由n1區間排序到n2區間,能夠只寫-kn1,即對n1字段排序code

[root@Ask-02 文檔]# sort 1.txt
<排序

[
1111
11222
22333
444444411
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@Ask-02 文檔]# sort -n 1.txt //字母默認爲0
<文檔

[
adm:x:3:4:adm:/var/adm:/sbin/nologin
1111
11222
22333
444444411
[root@Ask-02 文檔]# sort -nr 1.txt //反序顯示
444444411
22333
11222
1111
adm:x:3:4:adm:/var/adm:/sbin/nologin
[字符串

<it

wc命令

wc命令用於統計文檔的行數、字符數、或詞數。該命令的經常使用選項由:
-l(統計行數)
-m(統計字符數)
-w(統計詞數)

[root@Ask-02 文檔]# cat 2.txt
hello world
111
222
111,222
[root@Ask-02 文檔]# wc -l 2.txt
4 2.txt
[root@Ask-02 文檔]# wc -m 2.txt
28 2.txt
[root@Ask-02 文檔]# wc -w 2.txt
5 2.txt

uniq命令
uniq用來刪除重複的行
選項:
-c 統計重複次數

[root@Ask-02 文檔]# cat 2.txt
hello world
111
222
111,22
222
aa
345
222
aa
456
456
[root@Ask-02 文檔]# sort 2.txt |uniq -c
1 111
1 111,22
3 222
1 345
2 456
2 aa
1 hello world

tee_tr_split命令

命令tee
tee後面跟文件名,其做用相似於重定向>,比重定向多一個功能,把文件寫入後面所跟的文件時並顯示在屏幕上,一般用於管道符 |後。
選項
-a 追加的意思

[root@Ask-02 文檔]# sort 2.txt |uniq -c |tee 4.txt
1 111
1 111,22
3 222
1 345
2 456
2 aa
1 hello world
[root@Ask-02 文檔]# sort 2.txt |uniq -c |tee -a 4.txt
[root@Ask-02 文檔]# cat 4.txt
1 111
1 111,22
3 222
1 345
2 456
2 aa
1 hello world
1 111
1 111,22
3 222
1 345
2 456
2 aa
1 hello world

命令tr
tr用於替換字符

[root@Ask-02 文檔]# echo "asklinux" |tr '[al]' '[AL]'
AskLinux
[root@Ask-02 文檔]# echo "asklinux" |tr '[a-z]' '[A-Z]'
ASKLINUX

命令split

split用於切割文檔。

選項:
-b 表示依據大小來分割文檔,單位爲byte
-l 表示依據行數來分割文檔

[root@Ask-02 演示]# du -sh 5.txt
652K 5.txt
[root@Ask-02 演示]# split -b 200k 5.txt
[root@Ask-02 演示]# ls
5.txt xaa xab xac xad
[root@Ask-02 演示]# split -l 4000 5.txt
[root@Ask-02 演示]# ls
5.txt xaa xab xac xad xae

shell特殊符號下

* $變量前綴,!$組合,正則裏面表示行尾
* ;多條命令寫到一行,用分號分割
* ~用戶家目錄,正則表達式表示匹配符
* &放到命令後面,會把命令丟到後臺
* >  >>   2>  2>>  &>
* []指定字符串中的一個,[0-9],[a-zA-Z][abc] 
* ||和&&,用於命令之間

[root@Ask-02 演示]# rm -f xa* ; ls //分號用法
5.txt

[root@Ask-02 文檔]# ls 1.txt || wc -l 2.txt //當前面的命令正確執行時,後面的命令將再也不執行1.txt[root@Ask-02 文檔]# ls 1.txt && wc -l 2.txt //當前面的命令正確執行時,後面的命令才執行1.txt11 2.txt

相關文章
相關標籤/搜索