0

I'm writing a bash script to extract data from html tags and I found this command in another thread : grep -o '<tr>.*</tr>' HTMLFILE | sed 's/\(<tr>\|<\/tr>\)//g' > NEWFILE, it's working great.

But know I want to extract a name from a link in an <a> tag, like <a href="www.mywebsite.com?name="What_I_Want_To_Extract">. I thought hard but didn't manage to adapt the command below, any help ?

1

grep -oP 'name="\K[^"]*' filename

Output:

What_I_Want_To_Extract

See: http://www.charlestonsw.com/perl-regular-expression-k-trick/

Your version adapted:

grep -o 'name=".*">' HTMLFILE | sed 's/name="\|">//g' NEWFILE

  • so you can test above echo '<a href="fred.php" name="my file">' | grep -oP 'name="\K[^"]*' – zzapper Aug 16 '14 at 10:54
  • Thanks it does the trick, what should I add to this command if I want to specify what should come after "What_I_Want_To_Extract", as a class attribute so I restrict my extraction to a <a> with the attribute like class="something" just after the href ? – Zil0 Aug 16 '14 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.