1

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

<script type='text/javascript'>eval(function(...............
..............................
781a802d711afb9fe305d5b2e6|nlgphp5ee35huxvkc5xui4xl|gr6weglhvfovda4zodalt7j7glkt5ua|hojehp5ee35huxvkc5xui4xlugr6weglh|vfovl443odalt7klrfbtu4q|mystring123|data|new'.split('|')))
</script>
.......................
<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.

1

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'
mystring123

$ xmllint --html --xpath '//div[@class="description"]/text()' input.html
mytitle123
  • 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.