--- hackpadID: 5WzzFZidO6E hackpadWorkspace: tossug tags: hackpad-import, tossug --- # Linux 讀書會 - 第 10 週 10/07/2014 總目錄 [edX Introduction to Linux](https://tossug.hackpad.com/dVX1LvoCcii) ## 課程筆記 **Section 1 cat and echo** <undefined>* **cat**</undefined> cat ( concatenate的縮寫 ) * cat file * cat file1 file2 * cat file1 file2 > newfile * cat file >> existingfile * cat > file tac ( cat反過來 ,效果是從後面開始顯示) * tac file * tac file file2 > newfile cat > filename * 代表新增一個檔案 cat >> filename * 代表在檔案後端插入( append )檔案 cat > filename << EOF * 新增檔案的另一個方法,可以自行輸入內容,要離開的時候在句首輸入EOF。 <undefined>* **echo**</undefined> echo * echo string > newfile * echo string >> existingfile * echo $variable * -e 允許使用以下的特殊字元 * \n * \t **Section 2 sed and awk** 這兩項工具通常搭配一起用,awk 本身還是一門複雜的程式語言。兩者都可以把指令寫成 script 程式執行。 <undefined>* **sed**</undefined> sed Command Syntax * sed -e command <filename> * sed -f scriptfile <filename> sed Basic Operations * sed s/pattern/replace_string/ file * sed s/pattern/replace_string/g file * sed 1,3s/pattern/replace_string/g file * sed -i s/pattern/replace_string/g file 如果需要寫入檔案,建議直接輸出,不要使用 `-i` 參數,會比較安全。 $ sed s/pattern/replace_string/g file > file2 Example: To convert 01/02/… to JAN/FEB/… sed -e ’s/01/JAN/’ -e ’s/02/FEB/’ -e ’s/03/MAR/’ -e ’s/04/APR/’ -e ’s/05/MAY/’ \  -e ’s/06/JUN/’ -e ’s/07/JUL/’ -e ’s/08/AUG/’ -e ’s/09/SEP/’ -e ’s/10/OCT/’ \ -e ’s/11/NOV/’ -e ’s/12/DEC/’ <undefined>* **awk**</undefined> awk  * awk ‘command’ var=value file * awk -f scriptfile var=value file awk Basic Operations * awk ’{ print $0 }’ /etc/passwd * awk -F: ’{ print $1 }’ /etc/passwd * awk -F: ’{ print $1 $6 }’ /etc/passwd **Section 3 File Manipulation** File Manipulation Utilities * sort * uniq * paste * join * split <undefined>* **sort**</undefined> * sort -u * 列出所有結果的集合,也就是列出不重複的結果。 * sort -r * 反向排序,等同於 sort 完執行 tac。 * sort -k pos1[,pos2] 指定排序的key <undefined>* **uniq**</undefined> * 去除重複的資料,必須事先排序: * `sort file1 file2 | uniq > file3` * 計算重複的entry數量 * `uniq -c filename` <undefined>* **paste**</undefined> ![](https://courses.edx.org/c4x/LinuxFoundationX/LFS101x/asset/LFS01_ch12_screen27.jpg) * 想要把兩張表合併起來可以用paste來做到,輸出的結果依照delimiters來區分欄位,delimiter可以是tab、空格, comma,  ’|’等等。 * paste -d  * 使用` -d`可以自訂delimiter。 * paste -s  * <u>Q: 這邊看不太懂,不知道合併的規則是?</u> * <u>A: 可以把它想成是一組 key 和每一筆資料的組合,"Robert Norton" 這樣一組 column 就是一組 series。</u> | Robert Norton | Ted Yelsky | Chris Smith | Anna Brown | Brian Martin | | E001  834-677-1367 | E002  831-936-5892 | E003  854-849-4294 | E004  884-973-9674 | E005  899-055-4203 | | ... | ... | ... | ... | ... | * paste file1 file2 * paste -d, file1 file2 * paste -d ’:’ names phone <undefined>* **join**</undefined> ![](https://courses.edx.org/c4x/LinuxFoundationX/LFS101x/asset/LFS01_ch12_screen30.jpg) * $ cat phonebook * 555-123-4567 Bob * 555-231-3325 Carol * 555-340-5678 Ted * 555-289-6193 Alice * $ cat directory * 555-123-4567 Anytown * 555-231-3325 Mytown * 555-340-5678 Yourtown * 555-289-6193 Youngstown * The result of **join**ing  * $ join phonebook directory * 555-123-4567 Bob Anytown * 555-231-3325 Carol Mytown * 555-340-5678 Ted Yourtown * 555-289-6193 Alice Youngstown <undefined>* **split**</undefined> ![](https://courses.edx.org/c4x/LinuxFoundationX/LFS101x/asset/LFS01_ch012_screen31.jpg) * split infile <Prefix> * 範例:把一個字典檔切成99000行 * wc可以用來查看檔案的行數或字數 * $ wc -l american-english * 99171 american-english * $ split american-english dictionary * $ ls -l dictionary* * -rw-rw-r 1 me me 8552 Mar 23 20:19 dictionaryab * -rw-rw-r 1 me me 8653 Mar 23 20:19 dictionaryaa * . . . <undefined>* **regular expression** | a.. | matches | | b.|j. | matches both | | ..$ | og | | l.* | matches | | l.*y | matches | | the.* | matches the whole sentence |</undefined> **Section 4 grep** 用來搜尋文字的工具,會依照設定的pattern來搜尋,pattern中可以使用regular experssion <undefined>* **grep**</undefined> * grep [pattern] <filename> * grep -v [pattern] <filename> * grep [0-9] <filename> * grep -C 3 [pattern] <filename> * grep -A 3 [pattern] <filename> * grep -B 3 [pattern] <filename> **Section 5 Miscellaneous Text Utilities** <undefined>* **tr**</undefined> 用來刪除一段文字,或者替換一段文字。通常都是用來做些轉換,例如改變括號,或是所有內容合併成一行。 | Command | Usage | | $ tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ | Convert lower case to upper case | | $ tr '{}' '()' < inputfile > outputfile | Translate braces into parenthesis | | $ echo "This is for testing" | tr [:space:] '\t' | Translate white-space to tabs | | $ echo "This   is   for    testing" | tr -s [:space:] | Squeeze repetition of characters using -s | | $ echo "the geek stuff" | tr -d 't' | Delete specified characters using -d option | | $ echo "my username is 432234" | tr -cd [:digit:] | Complement the sets using -c option | | $ tr -cd [:print:] < file.txt | Remove all non-printable character from a file | | $ tr -s '\n' ' ' < file.txt | Join all the lines in a file into a single line | <undefined>* **tee**</undefined> 其實這指令通常用在 stdout 會持續輸出的時候,我們希望 log 也會記一份到另一個地方,此時就會用 `tee`。 <undefined>* **wc**</undefined> word count 可以計算文件有幾行、幾個單字或幾個字元。預設三個數字都會列。 <undefined>* **cut**</undefined> 可以想成類似 SQL `select <_column1, column2, ..._> from <_table_>` 的效果。 **Section 6 Dealing with Large Files and Text-related Utilities** 使用 `strings` 需安裝 `binutils`。 z 開頭的工具專為壓縮檔設計,可以對壓縮檔進行簡單操作。 **Lab 2** `awk -F: ’{ print $7 }’ /etc/passwd | sort -u | sed ’/^$/d’` or `awk -F: ’{ print $7 }’ /etc/passwd | sort -u | grep -v ’^$’` **Lab 3** 解答的做法不好,應該單獨對 `$7` 做處理。 Regex Cross­word [](http://regexcrossword.com/)http://regexcrossword.com/ ## 本週作業 1. 參閱筆記 Section 3 的 `paste -s` 範例,試著印出第三欄結果: * Chris Smith * E003 854-849-4294 1. 參閱筆記 Section 3 的 `join` 範例,試著印出第三行結果: * 555-340-5678 Ted Yourtown 1. 看完下列內容: * Chapter 14: Printing * Chapter 15: Bash Shell Scripting * [鳥哥的 Linux 私房菜 -- 學習 Shell Scripts](http://linux.vbird.org/linux_basic/0340bashshell-scripts.php) ## 活動簽到 [Carl Su](/ep/profile/n5euV0AaWLn) [P Fisher](https://tossug.hackpad.com/ep/profile/oTOWRrYfPRk) [Sam Lu](/ep/profile/zi86w39M6U6) [Scott Yu](/ep/profile/FXMAg851dkz) [steven huang](/ep/profile/sncZfUbLaeE) [violetson](https://tossug.hackpad.com/ep/profile/oJusv72f72w)