批量查单词shell脚本

这段代码展示了一个Shell脚本,用于读取一个包含单词的文件,通过Bing词典的API获取每个单词的英文释义,并将结果输出到另一个文件中。脚本首先对单词进行URL编码,然后使用curl发送HTTP请求,解析返回的HTML以提取释义信息。

words.sh:

while read word || [[ -n ${word} ]]
do

	encoded=`echo $word | tr -d '\n' | xxd -plain | sed 's/\(..\)/%\1/g'`
	html=`curl -s "https://cn.bing.com/dict/search?q=$encoded"`
	#meaning=`echo "$html" | grep -e "必应词典为您提供.*的释义" | perl -pe 's/.*<meta name="description" content="必应词典为您提供.*的释义,(美\[[^]]+\],)?(英\[[^]]+\],)?(.*)(" \/><meta content="text\/html;).*/\3/g' | perl -pe 's/(.*)网络释义:.*/\1/g'` 
	meaning=`echo "$html" | grep -e "必应词典为您提供.*的释义" | perl -pe 's/.*<meta name="description" content="必应词典为您提供.*的释义,(美\[[^]]+\],)?(英\[[^]]+\],)?(.*?)(网络释义:.*)?" \/><meta content="text\/html;.*/\3/g'`
	
	printf "${word}:${meaning}\n"

	#if [ -z "$meaning" ]; then
	#	printf "\n\n====\n$html\n====\n\n"
	#fi

done  < $1

样例输入文件1.txt:

hypertensive
hyperactive
hypersensitive

执行:

sh words.sh 1.txt > 2.txt 

样例输出文件2.txt:

hypertensive:adj. 高血压的; n. 高血压患者; 
hyperactive:adj. 过分活跃的;多动的; 
hypersensitive:adj. 非常敏感的;很容易生气的;(对某些物质、药物、光等)过敏的; 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值