Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Add the values of the lines according to a condition

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
aynar's Unix or Linux Image
aynar aynar is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 20 April 2018, 11:59 AM EDT
Posts: 4
Thanks: 2
Thanked 0 Times in 0 Posts
thanks for your response
i dont understand the commande very well
and when i tried it t got that result

-> 9,9555336


seem like it give the total of everything
not per ip
Sponsored Links
    #9  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 23 June 2018, 3:11 PM EDT
Location: Foxborough, MA
Posts: 8,299
Thanks: 249
Thanked 833 Times in 778 Posts
Quote:
Originally Posted by aynar View Post
thanks for your response
i dont understand the commande very well
and when i tried it t got that result

-> 9,9555336


seem like it give the total of everything
not per ip
the code is based on a data sample you provided in post 1.

The output should be similar to: 100.1.1.15 -> 8,0000000
Sponsored Links
    #10  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 June 2018, 6:40 AM EDT
Location: Aachen, Germany
Posts: 12,922
Thanks: 429
Thanked 3,969 Times in 3,650 Posts
As the sample in post#1 has commas in lieu of decimal dots, you might need to convert those first. Try this small adaption of vgersh99's code:


Code:
awk -F'[ :]' '
        {sub (/,/, ".", $1)
         a[$(NF-1)] += $1
        }
END     {for(i in a) printf("%s -> %.7f\n", i, a[i])
        }
' file
100.1.1.15 -> 9.6030529
100.1.1.64 -> 0.1469163
100.1.1.75 -> 0.0784025

    #11  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 23 June 2018, 3:11 PM EDT
Location: Foxborough, MA
Posts: 8,299
Thanks: 249
Thanked 833 Times in 778 Posts
RudiC,
the OP's locale might be different, but good catch.
Sponsored Links
    #12  
Old Unix and Linux 04-20-2018   -   Original Discussion by aynar
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 23 June 2018, 6:40 AM EDT
Location: Aachen, Germany
Posts: 12,922
Thanks: 429
Thanked 3,969 Times in 3,650 Posts
I was also thinking of DOS line terminators (<CR>, <carriage return, ^M, \r, or 0x0D), which would explain the strange output s/he posted, but there were none in the sample data.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Remove duplicate values with condition jiam912 Shell Programming and Scripting 2 06-23-2014 06:22 PM
Grep certain lines with condition radius UNIX for Dummies Questions & Answers 4 05-07-2013 04:04 PM
Perl XML, find matching condition and grep lines and put the lines somewhere else tententen Shell Programming and Scripting 4 11-03-2010 08:55 PM
To read the values and to use in the where condition venkatesht Shell Programming and Scripting 10 08-12-2010 04:01 AM
Help!! Need script to read files and add values by lines... dhuertas Shell Programming and Scripting 3 09-15-2007 11:14 PM



All times are GMT -4. The time now is 04:05 PM.