I have a very simple html file with a value inside. Value is 57 in this case.

<eta version="1.0"><value uri="/user/var/48/10391/0/0/12528" strValue="57" unit="%" decPlaces="0" scaleFactor="10" advTextOffset="0">572</value></eta>

What is an easy bash script way to extract and write in a variable? Is there a way to not even require a wget into a file as an intermediate step, so as not require to open and use a file where it is stored, but directly work with the wget?

To clarify, I could do a simple wget, save to a file and check the file for the value or is there an even more enhanced way to do the wget somewhere in RAM and not require an explicit file to be stored?

Thanks a million times, highly appreciated Norbert

  • HTML is a subset of XML. You need to read up on using an XML Reader in Linux, which is most likely why you were downvoted. – eyoung100 Nov 12 '14 at 21:40
  • 1
    @eyoung100 HTML5 is not XML – Pablo A Dec 31 '19 at 3:48

You can extract a value in your example with grep and assign it to the variable in the following way

$ x=$(wget -0 - 'http://foo/bar.html' | grep -Po '<value.*strValue="\K[[:digit:]]*')
$ echo $x


  • $(): command substitution
  • grep -P: grep with Perl regexp enable
  • grep -o: grep shows only matched part of the line
  • \K: do not show in the output anything what was matched up to this point
  • wget -O -: prints downloaded document to standard output (not to file)

However, for general approach it is better to use dedicated parser for html code.

| improve this answer | |
  • what is \K doing can u explain – Hackaholic Nov 12 '14 at 21:56
  • See updated edit. \K works only with -P option. – jimmij Nov 12 '14 at 22:01
  • + 1 for \K using perl regex – Hackaholic Nov 12 '14 at 22:03
  • +1 but since you're using -P, why not use \d+ instead of [[:digit:]]*? – terdon Nov 12 '14 at 23:29
  • Nice explanations. No temp file would be nice. – geedoubleya Nov 12 '14 at 23:59

I have no idea what wget you're talking about but I am guessing that you want to download the file. If so, yes, you can download it and parse it with no intermediate temp file:

$ value=$(wget -O - http://example.com/file.html | grep -oP 'strValue="\K[^"]+')
$ echo $value
| improve this answer | |
  • Yes, you are right \d+ is shorter, also [^"]+ is better because value inside "" probably(?) doesn't need to be numerical. – jimmij Nov 12 '14 at 23:39
  1. Apart from the wget -O - ... technique, you can also use curl -Ss ... to avoid the hassle of a temporary file.

  2. The following illustrates the use of pup (https://github.com/ericchiang/pup), which supports a CSS-based query language.

a) To extract the "text" value of the <value> tag:

pup 'value text{}'  # yields 572

b) To extract the value of the strValue attribute of the <value> tag:

pup 'value attr{strvalue}' # yields 57
| improve this answer | |
cat input | grep -o strValue=".*" | sed 's/strValue=//g' | sed 's/"//g'
| improve this answer | |
  • 2
    Useless use of cat and doesn't work anyway. If you really want to involve sed try sed 's/.*strValue="\([[:digit:]]*\).*/\1/' file. – jimmij Nov 12 '14 at 22:22
  • Yeah, i tried.. – DisplayName Nov 12 '14 at 23:55
  • I suck at everything. – DisplayName Nov 13 '14 at 0:19
  • No, you just need some practise and you have very good questions, I like especially this one: unix.stackexchange.com/q/159489/80886 for obvious reason. BTW, it was not me who downvoted. – jimmij Nov 13 '14 at 0:24
  • I't doesn't matter who down voted, i don't really care about internet points that much :). – DisplayName Nov 13 '14 at 0:53

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.