From a html page with lots of links, I'm looking for a way to extract only the links that follow a specific format.  <a href=https://always/same/link/same-name_19.html>always same text</a><a href=https://always/same/link/same-name_18.html>always same text</a><a href=https://always/same/link/same-name_17.html>always same text</a>I have found many solutions using grep, sed, and others but they all only return the inner links without the html tags and the always same text part.It should work from the command line / bash script, I'm using Debian Jessie. the solution doesn't need to be a oneliner. updatethe script i made with help of Ryan's Answer.Curl=https://always/same/link/filename=same-namepage=`curl $Curl$filename`echo $page > $filename.curlGurl=https://always/same/link/# workinglink=`grep -Eo '<a href=https://always/same/link/same-name_([0-9]+)\.html>always same text</a>' $filename.curl | sort | tail -n1` # not working; $link will be emptylink=`grep -Eo <a href=\https://always/same/link/same-name_([0-9]+)\.html\>always same text</a> $filename.curl | sort | tail -n1` # not working; $link will be emptylink=`grep -Eo <a href=\${Gurl}${filename}_([0-9]+)\.html\>always same text</a> $filename.curl | sort | tail -n1` # what i ended up using.link=`grep -Eo '<a href='${Gurl}${filename}'_([0-9]+)\.html\>always same text</a>' $filename.curl | sort | tail -n1` echo $link | grep -o 'https:.*[.]html'
getting links including outer html
shell script
Assuming most of the href attribute are always the same, and the inner-text are always the same, and you have an HTML source file, then something like this should work:grep -Eo '<a href=https://always/same/link/same-name_([0-9]+)\.html>always same text</a>' HTML-source-filegrep will search through the HTML-source-file for the given pattern https://always/same/link/same-name_([0-9]+)\.html.  This pattern will match any string made up of the following elements, in order:https://always/same/link/same-name_any number of the digits 0 through 9.htmlThe -E flag tells grep to interpret the pattern as an extended regular expression.The -o flag tell grep to only output the text that matches the pattern.To use variables, you can use double quotes around the regular expression, escape the double quotes inside the regular expression with backslashes, and enclose the variable names in curly braces, like so:$ link_path=https://always/same/link/$ link_name=same-name$ grep -Eo <a href=\${link_path}${link_name}_([0-9]+)\.html\>always same text</a> HTML-source-fileIf, for some reason, the above grep with escaped double-quotes does not work, you can also use this:$ link_path=https://always/same/link/$ link_name=same-name$ grep -Eo '<a href='${link_path}${link_name}'_([0-9]+)\.html>always same text</a>' HTML-source-file