shell特殊符號、cut、sort、uniq、tee、tr、split

通配符

  1. 通配符:*

將與零個或多個字符匹配。這就是說「什麼均可以」。例子:shell

  • /etc/g* 與 /etc 中以 g 開頭的全部文件匹配。
  • /tmp/my*1 與 /tmp 中以 my 開頭,而且以 1 結尾的全部文件匹配。
  1. 通配符:?

? 與任何單個字符匹配。例子:bash

  • myfile? 與文件名爲 myfile 後跟單個字符的任何文件匹配。
  • /tmp/notes?txt 將與 /tmp/notes.txt 和 /tmp/notes_txt 都匹配,若是它們存在。

註釋符號‘#’

註釋code

  • 在shell文件的行首,做爲include標記:#!/bin/bash。
  • 在其餘地方使用,做爲註釋,#後面的內容不會執行。

脫義字符 \

\ 符號使其後面的字符只是做爲字符自身,不會體現該字符的意義排序

[root@localhost ~]# a=123;b=456
[root@localhost ~]# c=$a$b
[root@localhost ~]# echo $c
123456
[root@localhost ~]# c=\$a\$b
[root@localhost ~]# echo $c
$a$b

管道符及cut

管道「| 」就是將前面命令輸出做爲管道後面命令的輸入 而cut是截取字符串。cut 分割,-d 分隔符 -f 指定第幾段 -c 指定第幾個字符字符串

[root@localhost tmp]# cat 2.txt |cut -d ":" -f 1
root
bin
daemon
adm
[root@localhost tmp]# cat 2.txt |cut -d ":" -f 1,2
root:x
bin:x
daemon:x
adm:x

sort 排序

參數:file

  • -n:依照數值的大小排序
  • -r:以相反的順序來排序;
  • -t<分隔字符>:指定排序時所用的欄位分隔字符;
[root@localhost tmp]# cat 2.txt 
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
[root@localhost tmp]# sort 2.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
root:x:0:0:root:/root:/bin/bash

wc 命令

  • wc -l 統計行數
  • wc -m 統計字符數
  • wc -w 統計詞數(以空格爲分割符號)
[root@localhost tmp]# wc -l 2.txt 
4 2.txt
[root@localhost tmp]# wc -w 2.txt 
4 2.txt
[root@localhost tmp]# wc -m 2.txt 
142 2.txt

uniq命令去掉重複的(得先排序再去重)

[root@localhost tmp]# cat 3.txt 
112233
223344
334455
556677
112233
223344

[root@localhost tmp]# sort -n 3.txt | uniq

112233
223344
334455
556677

tee命令

tee和>相似是重定向輸出的意思,tee -a 相似>>重定向追加,好比把3.txt排序,去重而後輸出到1.txt中的命令爲:統計

[root@localhost tmp]# sort 3.txt | uniq |tee 1.txt 

112233
223344
334455
556677
[root@localhost tmp]# cat 1.txt 

112233
223344
334455
556677

tee與>不一樣的是,它會把重定向的內容顯示在屏幕上,而>則不會。sort

tr替換字符

tr aa bb =將字符aa換成字符bb重定向

[root@localhost tmp]# cat 1.txt 

112233
223344
334455
556677
[root@localhost tmp]# tr '11' '99' < 1.txt 

992233
223344
334455
556677

|| 字符

「||」字符用在兩條命令之間,表示前面命令執行不成功,就執行後面的命令註釋

[root@localhost tmp]# cat 5.txt || cat 1.txt 
cat: 5.txt: 沒有那個文件或目錄

112233
223344
334455
556677
[root@localhost tmp]# cat 1.txt || cat 5.txt 

112233
223344
334455
556677

&&字符

&&字符表示前面命令執行成功了,纔會執行後面命令

[root@localhost tmp]# mkdir lic && mv 1.txt lic
[root@localhost tmp]# ls
2.txt  3.txt  lic
[root@localhost tmp]# tree lic/
lic/
└── 1.txt

0 directories, 1 file
相關文章
相關標籤/搜索