#!/bin/bash # file: getwiki.sh # Pete Nesbitt, Jan 2016 # Description: script to display the initial summary from wikipedia. # uses curl to pull initial results ## set some vars for text high-lighting normal=`tput sgr0` bold=`tput bold` ###### clear echo "" echo "" # Get search term from user ($WORD) printf " => Enter a Term to lookup on Wikipedia: " read WORD # cleanup: rm extra spaces, push all lower, replace spaces with underscores KEYWORD=`echo "$WORD" |tr -s [:blank:] |tr -t [:upper:] [:lower:] | sed 's/\ /_/g'` #cleaned up search word but still has spaces for text high-light FOUND_WORD="`echo "$WORD" |tr -s [:blank:] |tr -t [:upper:] [:lower:]`" # wiki is using several page layout/styles/classes for summary area. # Since a "class" type search sometimes fails, will do messier cleanup... # first, KEYWORD has first letter Uppercased using a ^ at end of var, # then find 1st 3 paragraphs (1 or 2 is not enough in some cases due to summary info etc), # then clean out html & blank lines, break up with line returns, then indent CONTENT_AREA="`curl --silent https://en.wikipedia.org/wiki/${KEYWORD^} | grep -m 3 "

" | sed 's/||g' | tr -d '\n' | tr -d '\t' | sed s/"${FOUND_WORD}"/${bold}\&${normal}/gI | fold -s | sed 's/^/\ \ \ /'`" # validate word since any number of auto pages may be generated. echo "${CONTENT_AREA}" | egrep "Other reasons this message may be displayed:|Wikipedia does not have an article with this exact name" &> /dev/null RET_BAD=$? if [ ${RET_BAD} -eq 0 ];then CONTENT_AREA=" Sorry, there was No Wiki Page Found for: ${WORD}" fi ## display results: echo "" echo "The Wiki Summary is:" echo "" printf "$CONTENT_AREA" echo "" echo "" # only display valid links: if [ ${RET_BAD} -ne 0 ];then echo " Read entire page at https://en.wikipedia.org/wiki/${KEYWORD^}" echo "" fi # eof