Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6077277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:42:11+00:00 2026-05-23T10:42:11+00:00

How can I take average of decimal values written in a file: TestFile as

  • 0

How can I take average of decimal values written in a file: TestFile as

Time to write: 0.000118000 sec

Time to write: 0.000119000 sec

Time to write: 0.000122000 sec

Wrong Soln:

Following prints just zero i.e. 0

awk '{sum+=$7}END{print sum/NR}' TestFile
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T10:42:11+00:00Added an answer on May 23, 2026 at 10:42 am

    EDIT

    since you are having trouble, and seem to return zero try using this

    grep -oP "\d+\.\d+" testFile | awk -vx=0 '{x += $1} END {print x/NR}'

    this will work if the file is double spaced or not. it only prints the match of a file that has as decimal number. and sends it to awk.

    the -P flag is a perl regular expression. you could do the same with -E, but \d+ matches one or more digits, \. matches a period. a . has special meaning in regular expressions and need to be escaped and \d+ matches one or more digits so put that together, '\d+\.\d+', you have a decimal.

    lastly, if you continue to get scientific notation you may consider printf to achieve floating point noation

    awk -vx=0 '{x += $4} END { printf ("%8.9f", x/NR) } testFile'

    you can specifiy something smaller like “%4.3f” to print only 4 numbers after a decimial, Conversely, use %e to print in scientific notation

    More using printf in awk


    old information, see above <hr>

    awk -vx=0 '{x += $4} END {print x/NR}' testFile

    which outputs:

    0.000119667

    for each line append $4, which in your test file is the number, to x. At the end divide x for number of lines.

    if your file is really double spaced run the following first:

    sed -i '/^$/d' testFile to remove blank lines. you may want to consider not editing testFile in place by removing -i and doing something like this sed '/^$/d' testFile > newFile

    or even combine the two files and pipe stdout from sed to awk

    sed '/^$/d' testFile | awk -vx=0 '{x += $4} END {print x/NR}'

    if this returns 0, you may have a problem with your testFile.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can i get validation on weight which can take decimal values and also integer.
I can take snapshot of a component. But the problem is the component is
Scriptaculous's Effect.Morph can take a CSS class and apply it over a period of
Zlib::GzipReader can take an IO, or IO-like, object. as it's input, as stated in
Is there anyway we can take input from command line in Objective-C, the way
What are some apraoches i can take to rewrite fairly large procedural php roughly
I have a web request that can take 30-90 seconds to complete in some
Is there a really easy way I can take an array of JSON objects
I'd like a reg exp which can take a block of string, and find
Is there some way I can take text (retrieved from a form), and email

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.