0

I have 2 HTML files, where part of the content looks like this:

In FILE1:

<td width="48%" align="right" valign="top">
<b>mom. Wirkleistung P+ tot.: </b><br>
<b>mom. Wirkleistung P+ L1: </b><br>
<b>mom. Wirkleistung P+ L2: </b><br>
<b>mom. Wirkleistung P+ L3: </b><br>
</td><td width="4%" align="middle">
&nbsp;
</td><td width="48%" valign="top">
<b>114,00 W </b><br>
<b>  2,00 W </b><br>
<b>109,00 W </b><br>
<b>  2,00 W </b><br>
</td></tr></table>
<p></td>

and in File2:

<b>mom. Wirkleistung P- tot.: </b><br>
<b>mom. Wirkleistung P- L1: </b><br>
<b>mom. Wirkleistung P- L2: </b><br>
<b>mom. Wirkleistung P- L3: </b><br>
</td><td width="4%" align="middle">
&nbsp;
</td><td width="48%" valign="top">
<b>  45,00 W </b><br>
<b>  0,00 W </b><br>
<b>  0,00 W </b><br>
<b>  0,00 W </b><br>
</td></tr></table>

Where I want to use for either files the first Watt value (114.00 and 45.00, which of course does change every 5 seconds) and put the SUM together.

I'm using a RASPBERRY PI (do Debian Linux), is there a way to extract these values from the two files and add it together, so that it works even if it contains a 5.00 or 66.70 or 1444.24 value.

ATTACHED below the FULL FILE....

<html><head>
<title>FacilityWeb</title>
<meta http-equiv="cache-control" content="no-cache">
<style type="text/css">
#idHF  {font-family:Arial; font-size:30px; color:#FFFFFF }
a      {font-family:Arial; font-size:20px; color:#FFFFFF }
table  {font-family:Arial; font-size:20px; color:#FFFFFF }
input  {font-family:Arial; font-size:20px; font-weight:bold; color:#000000 }
select {font-family:Arial; font-size:20px; font-weight:bold; color:#000000 }
</style>
</head>
<body bgcolor="#000000" link=#ffffff vlink=#ffffff alink=#ffffff>

<table align="center" border="0" width="960" cellspacing="0" cellpadding="8">
<tr><td id="idHF" align="right" valign="middle" bgcolor="#0074B2">
<b><i>Lingg &amp; Janke&nbsp;</i></b></td></tr></table>

<p><table align="center" border="0" width="960" bgcolor="#2f2f2f"><tr>

<!-- BCU part begin -->

<td align="center">
<a href="valpap">[ LEISTUNG P+ ]</a> <a href="valpan">[ LEISTUNG P- ]</a>
<a href="valprp">[ LEISTUNG Q+ ]</a> <a href="valprn">[ LEISTUNG Q- ]</a><br>
<a href="valv">[ SPANNUNG ]</a> <a href="valc">[ STROM ]</a>
<a href="valx">[ COS PHI ]</a><br>
<a href="valpapt">[ GRENZWERTE P+ tot. ]</a><br><a href="valpap1">[ GRENZWERTE P+ L1 ]</a>
<a href="valpap2">[ GRENZWERTE P+ L2 ]</a> <a href="valpap3">[ GRENZWERTE P+ L3 ]</a><br>
<a href="/1.1.2/">[ HOME ]</a>
<p><b>Wirkleistungen P+ (Bezug)</b><p>
<table width="100%"><tr>
<td width="48%" align="right" valign="top">
<b>mom. Wirkleistung P+ tot.: </b><br>
<b>mom. Wirkleistung P+ L1: </b><br>
<b>mom. Wirkleistung P+ L2: </b><br>
<b>mom. Wirkleistung P+ L3: </b><br>
</td><td width="4%" align="middle">
&nbsp;
</td><td width="48%" valign="top">
<b> 70,00 W </b><br>
<b>  2,00 W </b><br>
<b> 64,00 W </b><br>
<b>  2,00 W </b><br>
</td></tr></table>
<p></td>

<!-- BCU part end -->

</tr></table><p>

<table align="center" border="0" width="960" cellspacing="0" cellpadding="8">
<tr><td align="center" valign="middle" bgcolor="#0074B2">
<a id="idHF" href="/en/main.htm"><b>HOME</b></a></td></tr></table>

</body></html>
1
  • What are you using the values with? You could, of course, use awk for that, but perhaps XPath or XQuery would be better suited (take a look at Xidel).
    – Jan
    Sep 1, 2014 at 8:46

1 Answer 1

1

Assuming the input file are a.txt and b.txt respectively,

sed -e 's/<[^>]*>//g' -e s/,/./ a.txt|awk '/W/ { s+=$1 } END { print s }'
227

sed -e 's/<[^>]*>//g' -e s/,/./ b.txt|awk '/W/ { s+=$1 } END { print s }'
45

This work even with decimal data (e.g 0,12 W give 45.12).

If you always want decimal data

sed -e 's/<[^>]*>//g' -e s/,/./ a.txt|awk  '/W/ { s+=$1 } END { printf "%.2f\n", s }'
227.00

Edit: To get only th first value ($2 is W, you print $1 )

sed -e 's/<[^>]*>//g' -e s/,/./ a.txt|awk '/W/ { print $1 ; exit ;  }'

Edit 2: with full log, trying to get line with W

sed -e 's/<[^>]*>//g' -e s/,/./ a.txt|awk '$2 == "W" { print $1 ; exit ;  }'
70.00

Edit 3: adding two values.

Simplest way is to use bash abilities to add number.

Let says your files are in files1.htmland files2.html

VAR1=$(sed -e 's/<[^>]*>//g' -e s/,/./ files1.html |awk '$2 == "W" { print $1 ; exit ;  }' )
VAR2=$(sed -e 's/<[^>]*>//g' -e s/,/./ files2.html |awk '$2 == "W" { print $1 ; exit ;  }' )
SUM=$(($VAR1 + $VAR2))
5
  • 1
    While this is technically correct, it would be better to use a parser as the OP seems to be programming anyway.
    – Jan
    Sep 1, 2014 at 9:40
  • Thanks for your code.....the second one with 45 works fine....but where does the 227 come from? also for me it shows 227 - however the value in File1 or a.txt is 114,00Watt.
    – njordan
    Sep 1, 2014 at 19:39
  • Upps, sorry my mistake - I only need the first value from each file....so the 114W and the 45W.....how would that look like?
    – njordan
    Sep 1, 2014 at 19:58
  • Now it only reports BLANK back, I guess I need to provide the rest of the file, otherwise my fear is that your "cut" triggers too soon.
    – njordan
    Sep 2, 2014 at 8:47
  • thanks a lot, now it works great....only one minor question - and I know it is stupid but I spent now 1h and still I do not succeed. If I want to store each of the two values in var1 and var2 and build in the end the sum of var1 from file1 and var2 from file2....how would I do that....tried instead of print $1 to say var1 = $1 but this did not work. sorry again for this stupid question but I could not manage myself.
    – njordan
    Sep 2, 2014 at 20:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .