| 操作 | 說明 |
|---|---|
| awk '{ C += length($0) + 1; W += NF } END { print NR, W, C }' | 計算行數、詞數、字數 |
| awk 1 *.xml | 取代 cat |
| awk '{ rand() < 0.05 }' file(s) | 打印 5% 行左右的隨機樣本 |
| awk -v COLUMN=1 '{ sum += $COLUMN } END { print sum / NR }' file(s) | 加總第 n 欄的平均值 |
| awk '(100 <= FNR) && (FNR <= 150) && /pattern/ { print FILENAME ":" FNR ":" $0 }' file(s) | 文本查找,僅查找 100 - 150 行 |
| awk -v OFS='\t' '{ print $1, $3, $2, $4 }' old > new | 調換欄位並以 tab 分隔輸出 |
| awk 'BEGIN { FS = "\t"; OFS= "&" } { $1 = $1; print }' file(s) | 更換分隔符 |
| sort file(s) | awk 'Last != $0 { print } { Last = $0 }' | 排序後,刪除重複行 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # awk -f factorize.awk | |
| { | |
| n = int($1) | |
| m = n = (n>=2) ? n: 2 | |
| factors = "" | |
| for (k = 2; (m > 1) && (k^2 <= n); ) | |
| { | |
| if (int(m % k) != 0) | |
| { | |
| k++ |
| 模式 | 操作 | 說明 |
|---|---|---|
| 1 | 模式為真,默認為打印 | |
| NR > 0 | { print } | 有紀錄時打印(恆真) |
| 1 | { print } | 模式為真,則打印(預設值) |
| { print } | 無模式則為真,明確的打印 | |
| $1 ~ /jones/ | 選定字段 1 包含 jones 的紀錄 | |
| (FNR == 3), (FNR == 10) | 選定每個輸入文件的第 3 至第 10 筆紀錄 |
| 常見模式 | 說明 |
|---|---|
| NF == 0 | 選定空紀錄 |
| NF > 3 | 選定有三個字段以上的紀錄 |
| NR < 5 | 選定第 1 至第 4 筆紀錄 |
| (FNR==3) && (FILENAME ~ /[.][ch]$/) | 從來源文件選定紀錄 3 |
| $1 ~ /jones/ | 選定字段 1 包含 jones 的紀錄 |
| (FNR == 3), (FNR == 10) | 選定每個輸入文件的第 3 至第 10 筆紀錄 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /bin/sh - | |
| # | |
| # 在查找路徑下尋找一個或多個原始文件或文件模式 | |
| # 查找路徑由一個指定的環境變量所定義 | |
| # | |
| # 標準輸出產生的節,通常是在查找路徑下找到的每個文件第一個實體的完整路徑 | |
| # 或是 "filename: not found" 標準錯誤輸出 | |
| # | |
| # 如果所有文件都找到,則退出 0 | |
| # 否則,為找不到的文件個數 - 非零值 |
| exit code | meaning |
|---|---|
| 0 | 命令成功退出 |
| > 0 | 在重定向或單詞展開期間失敗 |
| 1-125 | 命令不成功退出 |
| 126 | 找到命令,但文件無法執行 |
| 127 | 找不到命令 |
| > 128 | 命令因收到信號而死亡 |
| grep | sed | ed | ex/vi | more | egrep | awk | lex | |
|---|---|---|---|---|---|---|---|---|
| BRE | v | v | v | v | v | |||
| ERE | v | v | v | |||||
\<, \> |
v | v | v | v | v |
| BRE/ERE | 模式涵義 | |
|---|---|---|
\{n,m\} |
BRE | 區間表達式(interval expression),匹配在它前面的單個字元重現的次數區間,用法有 1) \{n\} 重現 n 次 2) \{n,\} 至少重現 n 次 3) \{n,m\} |
\( \) |
BRE | 將 (與 ) 的模式存在特殊的保留空間 (holding space)。最多可以儲存 9 個獨立的子模式 (subpattern) 在單個模式中 |
\n |
BRE | 承上,透過轉義序列(escape sequences) \1 ~ \9,重複使用儲存過的子模式,如 \(ab\).*\1 能匹配 ab 組合的兩次重現,中間可存在任何數量的字元 |
{n,m} |
ERE | 同 BRE 的 {n,m} |
+ |
ERE | 匹配前面正則的一個或多個實例 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| "xte 'keydown Alt_L' 'key Left' 'keyup Alt_L'" | |
| m:0x10 + b:6 | |
| "xte 'keydown Alt_L' 'key Right' 'keyup Alt_L'" | |
| m:0x10 + b:7 | |
| "gnome-terminal" | |
| m:0x10 + b:2 | |
| "xte 'keydown Super_L' 'key Right' 'keyup Super_L'" | |
| m:0x10 + b:8 | |
| "xte 'keydown Super_L' 'key Left' 'keyup Super_L'" | |
| m:0x10 + b:9 |
| A | B | C | |
|---|---|---|---|
| IP Address | 192.168.1.10 | 192.168.1.11 | 192.168.1.12 |
| Name | web | db | nfs |
| /etc/hosts | 192.168.1.10 web 192.168.1.11 db 192.168.1.12 nfs | 192.168.1.10 web 192.168.1.11 db 192.168.1.12 nfs | 192.168.1.10 web 192.168.1.11 db 192.168.1.12 nfs |