html - hyperlink and link text extraction

Viewed 36

Hi I'm trying to extract the hyperlink and link text

HTML

<tr valign="top">
    <td class="beginner">
      B03&nbsp;&nbsp;
    </td>
    <td>
        <a href="http://www.drawspace.com/lessons/b03/simple-symmetry">Simple Symmetry</a>  </td>
</tr>

<tr valign="top">
  <td class="beginner">
    B04&nbsp;&nbsp;
  </td>
  <td>
      <a href="http://www.drawspace.com/lessons/b04/faces-and-a-vase">Faces and a Vase</a> </td>
</tr>

<tr valign="top">
    <td class="beginner">
      B05&nbsp;&nbsp;
    </td>
    <td>
      <a href="http://www.drawspace.com/lessons/b05/blind-contour-drawing">Blind Contour Drawing</a> </td>
</tr>

<tr valign="top">
    <td class="beginner">
        B06&nbsp;&nbsp;
    </td>
    <td>
      <a href="http://www.drawspace.com/lessons/b06/seeing-values">Seeing Values</a> </td>
</tr>

Code

sed -n 's/.*href="\([^"]*\).*/\1/p' file
http://www.drawspace.com/lessons/b03/simple-symmetry
http://www.drawspace.com/lessons/b04/faces-and-a-vase
http://www.drawspace.com/lessons/b05/blind-contour-drawing
http://www.drawspace.com/lessons/b06/seeing-values

Desired

http://www.drawspace.com/lessons/b03/simple-symmetry Simple Symmetry
http://www.drawspace.com/lessons/b04/faces-and-a-vase Faces and a Vase
http://www.drawspace.com/lessons/b05/blind-contour-drawing Blind Contour Drawing
http://www.drawspace.com/lessons/b06/seeing-values Seeing Values
1 Answers

1st solution: With your shown samples please try following awk code. Written and tested in GNU awk. Simple explanation would be setting RS to <a href="[^"]*">[^<]* regex and in main program, checking if RT is NOT NULL and using split to split its value into array arr with delmiter of > OR ", if all conditions are met then simply printing 2nd and 4th values of array arr as per requirement to get needed output.

awk -v RS='<a href="[^"]*">[^<]*' '
RT && split(RT,arr,"[>\"]"){
  print arr[2],arr[4]
}
'  Input_file


2nd solution: Using sed with its -E option(to enable ERE, extended regular expressions) please try following code. Using -n option to stop default writing by sed for lines. Then in main program using s option to perform substitution operation. Where I am mentioning [[:space:]]+<a href="([^"]*)">([^<]*).* regex which will create 2 capturing groups from which we are substituting whole matched text and then using p option to print this matched part as per requirement.

sed -E -n 's/[[:space:]]+<a href="([^"]*)">([^<]*).*/\1 \2/p' Input_file


3rd solution: Using GNU awk's match function where mentioning regex and creating 2 capturing group to fetch the required value(s).

awk '
match($0,/^[[:space:]]+<a href="([^"]*)">([^<]*)/,arr){
  print arr[1],arr[2]
}
'  Input_file
Related