通配符(Globbing)
通配符與元字符類似,通配符主要用于文件名的匹配,而元字符則主要用在字符串的匹配上;
下面介紹幾種常用的通配符:* 表示匹配任意位數的任意字符
? 表示匹配一位任意字符 ^ 表示取反,不包含的意思 [] 表示此區間內的任意一個字符 {} 表示一種集合 / 轉義字符,使具有特殊意義的字符失去原有意義 | 表示‘或',匹配一組可選的字符元字符
元字符是用來描述字符的特殊字符。
常用的元字符及意義如下:* 重復前面的字符0次或者多次. 匹配任意字符一次/+ 匹配前面的字符1次或者多次/? 匹配前面的字符0次或者1次/{m/} 匹配其前面的字符m次/{m,n/} 匹配前面的字符至少m次,至多n次^ 匹配字符在行首$ 匹配字符在行尾^$ 匹配空白行。空格、0不算/< 匹配字符在詞首/> 匹配字符在詞尾/<string/> 精準匹配string/(xy/) xy表示一個分組/1 模式從左側起,第一個左括號以及與之匹配的右括號之間的模式所匹配的字符除了以上的常用的元字符,還有一些特殊的元字符:
[:alpha:] 所有大小寫字母[:upper:] 所有大寫字母 [:lower:] 所有小寫字母[:alnum:] 所有字母及數字[:punct:] 所有標點符號[:blank:] 空白鍵和TAB鍵[:space:] 任意空白的字元,空格、tab、CR等[:digit:] 任意數字,0-9[:print:] 任何可以被打印出來的字符
grep
grep, egrep, fgrep - print lines matching a pattern【SYNOPSIS】 grep [OPTIONS] PATTERN [FILE...] grep [OPTIONS] [-e PATTERN | -f FILE] [FILE...]【OPTIONS】 --color=auto 對匹配到的內容進行高亮顯示處理 -i,--ignore-case Ignore case distinctions in both the PATTERN and the input files. (-i is specified by POSIX.)忽略字符大小寫匹配 -v,--invert-match Invert the sense of matching, to select non-matching lines. (-v is specified by POSIX.)顯示沒有匹配到的行 -o,--only-matching Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.只顯示匹配到的部分 -q,--quiet,--silent靜默模式,不列舉任何內容 -w,--word-regexp 單詞完整匹配所在的行 -d, --directories=ACTION how to handle directories; ACTION is 'read', 'recurse', or 'skip',目錄表示方式:只讀、遞歸、跳過 -r,-r, --recursive like --directories=recurse -c,--count print only a count of matching lines per FILE匹配到的文件有多少行 -B,--before-context=NUM print NUM lines of leading context列出匹配到的前NUM行 -A,--after-context=NUM print NUM lines of trailing context列出匹配到的后NUM行 -C,--context=NUM print NUM lines of output context列出匹配到的前后幾行
cut
Print selected parts of lines from each FILE to standard output列舉每行被選中的部分到標準輸出,也就是提取行中的某個字段【SYNOPSIS】cut OPTION... [FILE]...【OPTION】 -b,--bytes=LIST select only these bytes按字節分隔 -c,--characters=LIST select only these characters按字符分隔 -d,--delimiter=DELIM use DELIM instead of TAB for field delimiter 用TAB替換指定的分隔符來分區域 -f,--field=LIST 分區域后,根據區域位數來列出數據 -n with -b: don't split multibyte characters不分隔多字節字符【FOR EXAMPLE】 [root@localhost ~]# cat /etc/passwd|cut -d: -f1 root bin daemon adm lp 提取/etc/passwd文件的第一個字段內容,也就是用戶名 [root@localhost ~]# cat /tmp/ah2.txt |cut -nb 1,2,3 平凡的 [root@localhost ~]# cat /tmp/ah2.txt |cut -nb 1不分割字節 平 [root@localhost ~]# cat /tmp/ah2.txt |cut -b 1漢字屬于多字節字符 [root@localhost ~]# cat /tmp/ah2.txt |cut -c 1 平
新聞熱點
疑難解答