[ 作業檢討 ] 第一週超級挑戰題


Posted by yunanpan on 2020-06-19

自己寫的版本

# 開新檔案用來裝下載的資料,和擷取後的資料
touch temp.txt
touch final.txt
# 把下載的資料存到 $1.txt(依據查詢不同使用者會有不同檔案)
curl -s -o "$1.txt" "https://api.github.com/users/$1"
# 從 $1.txt 抓多個關鍵字後丟到 temp.txt
grep -E '"name"|"bio"|"location"|"blog"' $1.txt > temp.txt
# 因為 grep 完的內容會是 "name" : "The Octocat"
# 只擷取 : 後的值,並把順序換成最後想輸出的順序
cut -d : -f 2 temp.txt | awk 'NR==1' > final.txt
cut -d : -f 2 temp.txt | awk 'NR==4' >> final.txt
cut -d : -f 2 temp.txt | awk 'NR==3' >> final.txt
cut -d : -f 2-3 temp.txt | awk 'NR==2' >> final.txt
# 把多餘的符號給去掉
sed -e 's/"//g' -e 's/,$//' -e 's/null//g' final.txt

補充1:關於curl -s(擷取自 curl - How To Use


如果沒有加 -s 的話,會出現如下面的進度條:

補充2:關於 awk 'NR==1'(擷取自正規表示法 Regular Expression, RE

檢討到一半自己搞不清楚為何這樣寫XDD 論標註的重要性。


第一週超級挑戰題範例參考

(comment 為自己的理解)

#!/bin/sh
# 將執行的檔名後第一個接的參數儲存在 username 變數裡
username=$1;
# 將下載的資料儲存到 data 變數裡
data=$(curl --silent https://api.github.com/users/$username);
# 擷取指定的資訊後,因為會包含不需要的部分。以 sed 去頭去尾。
echo $data | grep -o '"name": ".*", "company' | sed 's/"name": "//g' | sed 's/", "company//g';
echo $data | grep -o '"bio": ".*", "twitter_username' | sed 's/"bio": "//g' | sed 's/", "twitter_username//g';
echo $data | grep -o '"location": ".*", "email' | sed 's/"location": "//g' | sed 's/", "email//g';
echo $data | grep -o '"blog": ".*", "location' | sed 's/"blog": "//g' | sed 's/", "location//g';









Related Posts

兩周學習計畫[隨時更新]

兩周學習計畫[隨時更新]

DOM - 事件傳遞機制

DOM - 事件傳遞機制

我的背景跟轉職動機

我的背景跟轉職動機


Comments