Need to grep a specific string using curl Need to grep a specific string using curl curl curl

Need to grep a specific string using curl


Using any sed in any shell on every Unix box:

$ curl -Ls yahoo.com | sed -n 's/^<html.* lang="\([^"]*\).*/\1/p'en-US


If you have gnu-grep then using -P (perl regex):

curl -Ls yahoo.com | grep -oP '\slang="\K[^"]+'he-IL


With awk's match function one could try following too.

your_curl_command | awk 'match($0,/^<html.*lang="[^"]*/){  val=substr($0,RSTART,RLENGTH)  sub(/.*lang="/,"",val)  print val}'

Explanation: Adding detailed explanation for above.

your_curl_command | awk '          ##Starting awk program from here.match($0,/^<html.*lang="[^"]*/){   ##using match function to match regex starting from <html till lang=" till next 1st occurrence of "  val=substr($0,RSTART,RLENGTH)    ##Creating val which has substring of matched values.  sub(/.*lang="/,"",val)           ##Substituting everything till lang=" with NULL in val here.  print val                        ##printing val here.}'