I have several html files containing a javascript function like this:

<script type='text/javascript'>eval(function(...............
<div class="description">mytitle123</div>

I'd like to extract the part between the | and |data|new (in this case "mystring123"), and also the title "mytitle123". The first string is always located in an eval and ends with the |data| delimiter, but the alphanumeric strings before it change.


XML/HTML documents should be processed with a proper parser tools.

xmllint approach (with sed support to search/replace within arbitrary <script> tag body text):

$ xmllint --html --xpath '//script/text()' input.html | sed -En 's/.*\|([^|]+)\|data\|new.*/\1/p'

$ xmllint --html --xpath '//div[@class="description"]/text()' input.html
| improve this answer | |
  • It works, but apparently the html documents are badly formatted as I get lots of "HTML parser error : End tag : expected >" and "Unexpected end tag" and "Tag footer invalid". The --nowarning option doesn't work. Is there a way to suppress those messages and only get the result (last line in xmllint's output) ? – Mark Roi Jun 16 '18 at 14:58
  • redirect errors: <comand> input.html 2>/dev/null – RomanPerekhrest Jun 16 '18 at 15:01
  • edit: although the 2> redirection doesn't work for some reason, 1>myfile writes the "clean" result to a file, enough for me. thanks! – Mark Roi Jun 16 '18 at 15:02

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.