0

I seem to be to stupid to parse some HTML Files with Bash. We have some files which have lines like:

var A4_total = 2018 + 4730;
var Other1_total = 3242 + 3828;

(They tell us how many pages the Printers have printed). I need to calculate the first two Values together (2018 and 3242). My Approach is:

hilfsvar1=$(echo `grep -F var\ A4_total StatCntMedia.htm | sed 's/var\ A4\_total\ \=\ //g'` | sed -ne "s/^[^=]\++//p"  | sed 's/;//g'); hilfsvar2=$(echo `grep -F var\ Other1_total StatCntMedia.htm | sed 's/var\ Other1\_total\ \=\ //g'` | sed -ne "s/^[^=]\++//p"  | sed 's/;//g'); echo "$hilfsvar1 + $hilfsvar2" | bc

This will fail. The two variables do have the right content:

[User]# echo $hilfsvar1
4730
[User]# echo $hilfsvar2
3828

But this is where I can't get forward:

[User]# echo "$hilfsvar1 + $hilfsvar2"
 +  3828

(Sorry for my scripting, I don't have deeper knowlede of Script languages :) ) - I would be happy to resolve this in another way if someone does have a solution.

Thanks in advance, Jonas

3
  • Your command works here, maybe your file has more spaces than you show in the question?
    – Thor
    Jan 8, 2015 at 13:57
  • The correct English isn't to stupid, it's too stupid :-).
    – Ed Morton
    Jan 8, 2015 at 15:33
  • I'd like to upvote that but it seems I'm too stupid :) Thanks
    – schannall
    Jan 9, 2015 at 8:25

2 Answers 2

1

It sounds like this might be what you're looking for:

$ awk '{sum[4]+=$4; sum[6]+=$6} END{print sum[4], sum[6]}' file
5260 8558

If not, update your question to show expected output.

It seems like you're probably on completely the wrong track though and trying to do somethign in shell that should be done entirely in awk.

1
  • This is Perfect, thank you! I will try to understand what exactly happens there :)
    – schannall
    Jan 9, 2015 at 8:23
0

Try something like:

awk '/A4_total|Other1_total/ {print $4+$6}' StatCntMedia.htm 
Output:
6748
7070

search for either A4_total or Other1_total, add 4th and 6th field separated by space and display the output.

5
  • 1
    Bit shorter awk '/(A4|Other1)_total/{print $4+$6}'
    – user3442743
    Jan 8, 2015 at 11:32
  • While this works I seem to be too stupid to add those two :/. [User]# awk '/(A4|Other1)_total/{print $4+$6}' StatCntMedia.htm |grep -xv "0" |bc 6748 7070
    – schannall
    Jan 8, 2015 at 11:51
  • Hi, i'm trying to add the first fild of A4_total with the first field of Other1_total and the second with the second. (Wich would be 2018+3242 and 4730+3828. Thanks for all your help! :)
    – schannall
    Jan 8, 2015 at 12:06
  • so how many times these patterns appearing your file? And i would suggest you to close this question and ask a new question as this is a follow up question.
    – SMA
    Jan 8, 2015 at 12:16
  • @almasshaikh: This is what he originally asked for, although it could be clearer.
    – Thor
    Jan 8, 2015 at 13:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.