I have a bash function that let's me see a word definition from Google:
defword() {
if [ -z $2 ]; then
header=30
else
header=$2
fi
lynx -dump "http://www.google.com/search?hl=en&q=define: ${1}&btnG=Google Search" | grep -v "\[1\] Google" | grep -v "define: ${1}__" | grep -v "\[2\]IMAGES" | head -${header}
}
e.g. defword dog
would display the first 30 lines of output for 'dog' and defword dog 50
would display the first 50 lines.
It occurs to me that this could be cleaner output if I can just grab the 'noun', 'verb', 'wikipedia' sections etc.
Can you suggest ways (maybe with grep or sed or awk?) that I could run say defword dog noun
that will display the whole of the noun section (from the word 'noun' down to the blank line before the next section), defword dog noun 2
to display just definition 2 from the noun section (and same for 'verb' or 'adjective' if those sections are present), and defword dog wikipedia
which displays the wikipedia definition that is present below noun and verb (can see that wikipedia section by doing defword dog 100
with the above function)?
CodePudding user response:
Matching between two lines is possible with sed (https://unix.stackexchange.com/a/264977)
To match between your lines you can pipe to
sed -n '/^ noun$/,${p;/^$/q}'
This allows your entire script to be tidied to just be
lynx -dump "http://www.google.com/search?hl=en&q=define: ${1}&btnG=Google Search" | sed -n '/^ '"${2:-noun}"'$/,${p;/^$/q}'
By also using the default parameter syntax in bash (https://coderwall.com/p/s8n9qa/default-parameter-value-in-bash)