1. grep 1.1 grep的與或非舉例 1.2 其餘經常使用例子 1.3 更多選項 2 正則表達式 2.1 概念 2.2 大部分正則表達式的形式都有以下的結構: 2.3 精確的語法可能因不一樣的工具或程序而異。 3. 實例 3.1 查找html文件裏全部高度參數 3.2 刪除表格內高度參數 3.3 寫入文件
grep – 在文件中搜索模式,打印模式匹配成功的行,能夠搜索一個或多個文件。
這裏的模式匹配與文本編輯器裏的字段搜索,通配符模糊查找不一樣之處是使用正則表達式,是一套完整的模式表示規則。html
and
$ grep pci hwinfo.txt |grep devices
or -E
$ grep 'pci\|devices' hwinfo.txt
$ grep -E 'pci|devices' hwinfo.txt
非 -v
$ grep -v pci hwinfo.txt正則表達式
單詞精確查找 -w
$ grep -w pci hwinfo.txt
僅查找獨立的pci單詞。即不包括ppci,pci1等先後有其餘字母或數字,包含pci的詞。
忽略大小寫 -i
$ grep -i pci hwinfo.txt
統計行數 -c
$ grep -i pci hwinfo.txt | grep -vc pci-0000
輸出包含行號 -n
$ grep -i pci hwinfo.txt | grep -vn pci-0000
打印包含字段的文件名 -l
$ grep -l pci hwinfo.txt s.json
hwinfo.txt
打印不包含字段的文件名 -L
$ grep -L pci hwinfo.txt s.json
s.jsonexpress
匹配選擇: | Pattern selection and interpretation: | 模式選擇和解釋: | |
-E, | --extended-regexp | PATTERNS are extended regular expressions | 使用擴展正則表達式匹配。 |
-F, | --fixed-strings | PATTERNS are strings | 使用固定字符串匹配。 |
-G, | --basic-regexp | PATTERNS are basic regular expressions | PATTERNS是基本的正則表達式 (默認) |
-P, | --perl-regexp | PATTERNS are Perl regular expressions | PATTERNS是Perl正則表達式 |
匹配控制: | |||
-e, | --regexp=PATTERNS | use PATTERNS for matching | 使用PATTERNS進行匹配 |
-f, | --file=FILE | take PATTERNS from FILE | 從文件中獲取PATTERNS |
-i, | --ignore-case | ignore case distinctions | 忽略案例區別. 忽略大小寫 |
-w, | --word-regexp | match only whole words | 只匹配整個單詞. 若是還指定了-x,則此選項無效。 |
-x, | --line-regexp | match only whole lines | 只匹配整條線 |
-v, | --invert-match | select non-matching lines | 選擇不匹配的行 |
通常輸出控制: | Output control: | ||
-c, | --count | print only a count of selected lines per FILE | 僅打印每一個FILE所選行的計數. 使用-v,計算不匹配的行。 |
--color[=WHEN], | use markers to highlight the matching strings; | 使用標記突出顯示匹配的字符串; | |
--colour[=WHEN] | WHEN is 'always', 'never', or 'auto' | 何時是「永遠」,「從不」或「自動」 | |
-L, | --files-without-match | print only names of FILEs with no selected lines | 僅打印沒有選定行的FILE名稱 |
-l, | --files-with-matches | print only names of FILEs with selected lines | 僅打印具備選定行的FILE名稱 |
-m, | --max-count=NUM | stop after NUM selected lines | NUM選擇行後中止 |
-o, | --only-matching | show only nonempty parts of lines that match | 僅顯示匹配的非線性部分 |
-q, | --quiet, --silent | suppress all normal output | 抑制全部正常輸出 |
-s, | --no-messages | suppress error messages | 抑制錯誤消息 |
輸出行前綴控制: | |||
-b, | --byte-offset | print the byte offset with output lines | 輸出打印字節偏移量 |
-H, | --with-filename | print file name with output lines | 打印每一個匹配的文件名。當有多個要搜索的文件時,這是默認設置。 |
-h, | --no-filename | suppress the file name prefix on output | 禁止輸出上的文件名前綴。當只有一個文件(或僅標準輸入)時,這是默認設置。 |
--label=LABEL | use LABEL as the standard input file name prefix | 使用LABEL做爲標準輸入文件名前綴 | |
-n, | --line-number | print line number with output lines | 輸出打印行號 |
-T, | --initial-tab | make tabs line up (if needed) | 製做標籤隊列(若是須要). 初始選項卡 |
-Z, | --null | print 0 byte after FILE name | 在FILE名稱後打印0字節。 輸出零字節(ASCII NUL字符). |
上下文行控制: | Context control: | ||
-A, | --after-context=NUM | print NUM lines of trailing context | 打印NUM行尾隨上下文 |
-B, | --before-context=NUM | print NUM lines of leading context | 打印NUM行前導上下文 |
-C, | --context=NUM | print NUM lines of output context | 打印NUM行輸出上下文 |
-NUM | same as --context=NUM | 與--context = NUM相同 | |
文件和目錄選擇: | |||
-a, | --text | equivalent to --binary-files=text | 至關於--binary-files = text; 像處理文本同樣處理二進制文件; |
--binary-files=TYPE | assume that binary files are TYPE; | 假設二進制文件是TYPE; TYPE is 'binary', 'text', or 'without-match' | |
-D, | --devices=ACTION | how to handle devices, FIFOs and sockets; ACTION is 'read' or 'skip' | 如何處理設備,FIFO和插座; ACTION is 'read' or 'skip' |
-d, | --directories=ACTION | how to handle directories; ACTION is 'read', 'recurse', or 'skip' | 如何處理目錄; ACTION is 'read', 'recurse'遞歸, or 'skip' |
--exclude=GLOB | skip files and directories matching GLOB | 跳過與GLOB匹配的文件和目錄 | |
--exclude-from=FILE | skip files matching any file pattern from FILE | 跳過與FILE中的任何文件模式匹配的文件 | |
--exclude-dir=GLOB | skip directories that match GLOB | 跳過與GLOB匹配的目錄 | |
--include=GLOB | search only files that match GLOB (a file pattern) | 僅搜索與GLOB匹配的文件(文件模式) | |
-I | equivalent to --binary-files=without-match | 至關於--binary-files =不匹配; 處理二進制文件,就像它不包含匹配數據同樣 | |
-r, | --recursive | like --directories=recurse | 只有符號連接在命令行上時,才能遞歸地讀取每一個目錄下的全部文件。若是沒有給出文件操做數,grep將搜索工做目錄。這至關於-d recurse選項 |
-R, | --dereference-recursive | likewise, but follow all symlinks | 遞歸地讀取每一個目錄下的全部文件。跟隨全部符號連接,與-r不一樣。 |
其餘選擇: | Miscellaneous: | ||
-V, | --version | display version information and exit | 顯示版本信息並退出 |
--help | display this help text and exit | 顯示此幫助文本並退出 | |
--line-buffered | flush output on every line | 在輸出上使用行緩衝。這可能會致使性能降低。 | |
-U, | --binary | do not strip CR characters at EOL (MSDOS/Windows) | 不要在EOL(MSDOS/Windows)中刪除CR字符。將文件視爲二進制文件。 |
-z, | --null-data | a data line ends in 0 byte, not newline | 將輸入和輸出數據視爲行序列,每一個行以零字節(ASCII NUL字符)而不是換行符 |
https://zh.wikipedia.org/wiki/正則表達式
https://en.wikipedia.org/wiki/Regular_expression編程
正則表達式(英語:Regular Expression,在代碼中常簡寫爲regex、regexp或RE)(有時稱爲rational expression理性表達),又稱正則表示式、正則表示法、規則表達式、常規表示法,是計算機科學的一個概念。正則表達式使用單個字符串來描述、匹配一系列匹配某個句法規則的字符串。
自20世紀80年代以來,用於編寫正則表達式的不一樣語法已經存在,一個是POSIX標準,另外一個是普遍使用的Perl語法。
許多程序設計語言都支持利用正則表達式進行字符串操做。例如,在Perl中就內建了一個功能強大的正則表達式引擎。正則表達式這個概念最初是由Unix中的工具軟件(例如sed,awk和grep)普及開的。正則表達式用於搜索引擎,搜索和替換文字處理器和文本編輯器的對話框,文本處理實用程序(如sed和AWK)以及詞法分析。許多編程語言都提供內置或經過庫的正則表達式功能。
正則表達式(一般稱爲模式)是用於指定特定目的所需的一組字符串的表達式。指定有限字符串集的簡單方法是列出其元素或成員。可是,一般有更簡潔的方法來指定所需的字符串集。例如,包含三個字符串「Handel」,「Händel」和「Haendel」的集合能夠由模式 H(ä|ae?)ndel 指定。
json
Boolean "or" 布爾「或」socket
Quantification 數量限定
某個字符後的數量限定符用來限定前面這個字符容許出現的個數。最多見的數量限定符包括+、?和*(不加數量限定則表明出現一次且僅出現一次):編程語言
匹配編輯器
上述這些構造子均可以自由組合,所以H(ae?|ä)ndel和H(a|ae|ä)ndel是相同的。
工具
正則表達式有多種不一樣的風格。
更多語法及具體規則在可參閱:https://en.wikipedia.org/wiki/Regular_expression
https://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines
正則表達式引擎的比較
正則表達風味比較 - 最流行的正則表達風味的詳細比較
http://www.regular-expressions.info/refflavors.html
Regexp語法摘要
http://www.greenend.org.uk/rjk/2002/06/regexp.html
在線正則表達式測試 - 支持Java,JavaScript,.Net,PHP,Python和Ruby
http://www.regexplanet.com/
實現正則表達式 - RE2的做者Russ Cox的系列文章
https://swtch.com/~rsc/regexp/
正則表達式引擎
http://www.softec.lu/site/RegularExpressions/RegularExpressionEngines
性能
$ grep height=\"..\" 1.html
<td align="left" height="20">RS</td>
<td align="left" height="23">ORS</td>
$ sed 's/height=\"..\">/>/g' 1.html |grep align
<td align="left" >OFS</td>
<td align="left" >RS</td>
$ sed -i 's/height=\"..\">/>/g' 1.html