0

I have an html file and I should change (with Bash) the numbers inside. This is an example:

<TR><TD ALIGN=center><FONT SIZE="-1"><B>28</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>58</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>44</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>1220</B></FONT></TD>
<TR><TD ALIGN=center><FONT SIZE="-1"><B>29</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>57</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>43</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>730</B></FONT></TD>
<TR><TD ALIGN=center><FONT SIZE="-1"><B>30</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>56</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>41</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>736</B></FONT></TD>

I know that the numbers are all enclosed in the tag "<B>" and "</B>".

These numbers must all be multiplied by 3.

How can I do?

I'm trying to make a script without success.

thanks

Ignazio
  • 1
  • 4
  • 1
    What have you tried so far? What was your best attempt? – Shloim Nov 05 '19 at 10:05
  • This could be done in single `awk` script, but I am bad at `awk`, so I would do it like this: 1. With `sed` Extract the number between `[0-9]*` and put it in front of the line. 2. With `awk` multiply it by 3. Would be just `awk '{ $1*=3 }1`. 3. Put the number back in the line with proper `sed`. [Here is a sed introduction](http://www.grymoire.com/Unix/Sed.html) and the simplest way to learn regexes is [to play games like crosswords with it](https://regexcrossword.com/). – KamilCuk Nov 05 '19 at 10:08
  • I'm trying with `sed` and `awk` but I can't figure out how to do it. The data I need to modify is in the midst of thousands of html lines (which I filter with a grep keeping only those that have the `` tag) but then I can't do the replacement – Ignazio Nov 05 '19 at 10:22
  • `the midst of thousands of html lines` - then I advise [not to parse html with regex](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). Use a dedicated tool for html, `xmllint`, `xmlstarlet` etc. – KamilCuk Nov 05 '19 at 10:32

2 Answers2

1

awk or sed may not be the best tool, but this could work:

awk -F'<B>|</B>' -v OFS= '{$2="<B>"$2*3"</B>"}1' file
<TR><TD ALIGN=center><FONT SIZE="-1"><B>84</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>174</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>132</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>3660</B></FONT></TD>
<TR><TD ALIGN=center><FONT SIZE="-1"><B>87</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>171</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>129</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>2190</B></FONT></TD>
<TR><TD ALIGN=center><FONT SIZE="-1"><B>90</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>168</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>123</B></FONT></TD>
<TD ALIGN=right><FONT SIZE="-1"><B>2208</B></FONT></TD>
Jotne
  • 40,548
  • 12
  • 51
  • 55
  • Thanks, in your opinion what could be the best tool? How can I rewrite the file with the updated data? If I write it again using `"> filename"` gives me a completely staggered file. Is there a possibility to rewrite only the numerical data in the old file? Is it also possible to round off the result? – Ignazio Nov 05 '19 at 10:34
  • @Ignazio to rewite data using `awk` then do: `awk 'my commands' file > tmp && mv tmp file` Everything are possible, it just takes som more time, – Jotne Nov 05 '19 at 11:07
1

Use an HTML-aware tool to modify HTML. For example, in xsh, you can write

open :F html file.html ;
for //b
    set . (3 * .) ;
save :F html :b ;
choroba
  • 231,213
  • 25
  • 204
  • 289