Useful Commands/Text Processing: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 2: | Line 2: | ||
! 情境 || 指令 | ! 情境 || 指令 | ||
|- | |- | ||
| (改) BIG5 轉 UTF-8 || < | | (改) BIG5 轉 UTF-8 || <syntaxhighlight lang="bash">iconv -f CP950 -t UTF-8 big5file.txt</syntaxhighlight> | ||
|- | |- | ||
| (改) 搜尋 > 取代 > 存檔 - OSX/FreeBSD || | | (改) 搜尋 > 取代 > 存檔 - OSX/FreeBSD || | ||
< | <syntaxhighlight lang="bash"> | ||
# 取代後輸出 (測試與 pipe 用) | # 取代後輸出 (測試與 pipe 用) | ||
sed -e 's/搜尋RE/取代文字/' 檔名 | sed -e 's/搜尋RE/取代文字/' 檔名 | ||
# 取代後直接寫回檔案 (單檔自動化處理用) | # 取代後直接寫回檔案 (單檔自動化處理用) | ||
sed -i -e 's/搜尋RE/取代文字/' 檔名 | sed -i -e 's/搜尋RE/取代文字/' 檔名 | ||
</ | </syntaxhighlight> | ||
|- | |- | ||
| 讀取超大文字檔的第一百萬行<br>(效能視 sed, awk 版本略有差異) || | | 讀取超大文字檔的第一百萬行<br>(效能視 sed, awk 版本略有差異) || | ||
< | <syntaxhighlight lang="bash"> | ||
awk 'NR==1000000{print;exit}' 檔名 | awk 'NR==1000000{print;exit}' 檔名 | ||
sed -n '1000000{p;q;}' 檔名 | sed -n '1000000{p;q;}' 檔名 | ||
sed '1000000q;d' 檔名 | sed '1000000q;d' 檔名 | ||
</ | </syntaxhighlight> | ||
|- | |- | ||
| 讀取超大文字檔的其中幾行 || | | 讀取超大文字檔的其中幾行 || | ||
< | <syntaxhighlight lang="bash"> | ||
awk 'NR>=1000000{print} NR==1000050{exit}' 檔名 | awk 'NR>=1000000{print} NR==1000050{exit}' 檔名 | ||
</ | </syntaxhighlight> | ||
|- | |- | ||
| 輸出檔案並且顯示列號 || | | 輸出檔案並且顯示列號 || | ||
< | <syntaxhighlight lang="bash"> | ||
nl 3small.txt | nl 3small.txt | ||
cat -n 3small.txt | cat -n 3small.txt | ||
</ | </syntaxhighlight> | ||
|- | |- | ||
| Search Text || | | Search Text || | ||
< | <syntaxhighlight lang="bash"> | ||
grep 'Test' . | grep 'Test' . | ||
apk search tcl | grep -e '^tcl' | apk search tcl | grep -e '^tcl' | ||
pyenv install -l | grep -E '^\s+3' | pyenv install -l | grep -E '^\s+3' | ||
</ | </syntaxhighlight> | ||
|} | |} | ||
Latest revision as of 20:17, 3 April 2025
| 情境 | 指令 |
|---|---|
| (改) BIG5 轉 UTF-8 | iconv -f CP950 -t UTF-8 big5file.txt
|
| (改) 搜尋 > 取代 > 存檔 - OSX/FreeBSD |
# 取代後輸出 (測試與 pipe 用)
sed -e 's/搜尋RE/取代文字/' 檔名
# 取代後直接寫回檔案 (單檔自動化處理用)
sed -i -e 's/搜尋RE/取代文字/' 檔名
|
| 讀取超大文字檔的第一百萬行 (效能視 sed, awk 版本略有差異) |
awk 'NR==1000000{print;exit}' 檔名
sed -n '1000000{p;q;}' 檔名
sed '1000000q;d' 檔名
|
| 讀取超大文字檔的其中幾行 |
awk 'NR>=1000000{print} NR==1000050{exit}' 檔名
|
| 輸出檔案並且顯示列號 |
nl 3small.txt
cat -n 3small.txt
|
| Search Text |
grep 'Test' .
apk search tcl | grep -e '^tcl'
pyenv install -l | grep -E '^\s+3'
|