Created
December 11, 2013 10:46
-
-
Save siongui/7908402 to your computer and use it in GitHub Desktop.
linux命令行版有道词典 From:
http://www.oschina.net/code/snippet_942897_27191
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/bash | |
ARGS=1 | |
E_BADARGS=65 | |
TEM_FILE="/tmp/dict.tmp" | |
if [ $# -ne "$ARGS" ] | |
then | |
echo "Usage:`basename $0` word" | |
exit $E_BADARGS | |
fi | |
# 抓取页面,删除html代码,空行等,只留下想要的内容 | |
curl -s 'http://dict.youdao.com/search?q='$1'' | awk 'BEGIN{j=0;i=0;} {if(/phrsListTab/){i++;} if(i==1){print $0; if(/<\/ul>/){i=0;}} if(/collinsToggle/){ j++;} if(j==1) {print $0; if(/<\/ul>/){j=0;}}}' | sed 's/<[^>]*>//g' | sed 's/ //g'| sed 's/→//g' | sed 's/^\s*//g' | sed '/^$/d'> $TEM_FILE | |
# 处理输出 | |
is_head=true # 当前行是否属于“头部” | |
head="" # 头部内容 | |
body="" # 主体内容 | |
ln_item=0 # 每一条解释的行号 | |
ln_eg=0 # 例句行号 | |
while read line | |
do | |
let ln_item++ | |
let ln_eg++ | |
num_flag=`echo "$line" | awk '/[0-9]+\.$/'` | |
if [ "$num_flag" != "" ]; then ## 遇见'数字+点'开头的行 | |
is_head=false # 第一次遇见数字行 将头部标示设置为false | |
ln_item=0 | |
fi | |
eg_flag=`echo "$line" | awk '/例:$/'` # 遇见'例:'开头的行 | |
if [ "$eg_flag" != "" ]; then | |
ln_eg=0 | |
fi | |
if $is_head ; then | |
head="$head $line" | |
else | |
if [ $ln_item == 0 ] ; then | |
line="\033[32;1m\n\n$line\033[0m" # 释义编号 | |
elif [ $ln_item == 1 ] ; then | |
line="\033[32;1m[$line]\033[0m" # 词性 | |
elif [ $ln_item == 2 ] ; then | |
line="\033[1m$line\033[0m" # 释义 | |
elif [ $ln_eg == 0 ] ; then | |
line="\033[32;1m\n $line\033[0m" # 例: | |
elif [ $ln_eg == 1 ]; then | |
line="\033[33m$line\033[0m" # 例句 | |
elif [ $ln_eg == 2 ]; then | |
line="\033[33m$line\033[0m" # 例句释义 | |
fi | |
body="$body $line" | |
fi | |
done < $TEM_FILE | |
echo -e "\033[31;1m$head\033[0m $body" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment