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 7655765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:35:50+00:00 2026-05-31T12:35:50+00:00

I have a file with entries seperated by an empty space. For example: example.txt

  • 0

I have a file with entries seperated by an empty space. For example:

example.txt

24676 256 218503341 2173
13236272 500 1023073758 5089
2230304 96 15622969 705
0 22 0 526
13277 28 379182 141

I would like to print, in the command line, the outcome of “column 1/ column 3” or simila. I believe it can be done with awk. However, some entries are 0, hence division by 0 gives:

fatal: division by zero attempted

In a more advanced case, I would like to find the median value (or some percentile) of the division.

  • 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-31T12:35:51+00:00Added an answer on May 31, 2026 at 12:35 pm

    There are many ways to ignore the row with a zero divisor, including:

    awk '$3 != 0 { print $1/$3 }' your-data-file
    
    awk '{ if ($3 != 0) print $1/$3 }' your-data-file
    

    The question changed — to print 0 instead. The answer is not much harder:

    awk '{ if ($3 != 0) print $1/$3; else print 0 }' your-data-file
    

    Medians and other percentiles are much fiddlier to deal with. It’s easiest if the data is in sorted order. So much easier that I’d expect to use a numeric sort and then process the data from there.


    I dug out an old shell script which computes descriptive statistics – min, max, mode, median, and deciles of a single numeric column of data:

    :   "@(#)$Id: dstats.sh,v 1.2 1997/06/02 21:45:00 johnl Exp $"
    #
    #   Calculate Descriptive Statistics: min, max, median, mode, deciles
    
    sort -n $* |
    awk 'BEGIN { max = -999999999; min = 999999999; }
        {   # Accumulate basic data
            count[$1]++;
            item[++n] = $1;
            if ($1 > max) max = $1;
            if ($1 < min) min = $1;
        }
    END {   # Print Descriptive Statistics
            printf("# Count = %d\n", n);
            printf("# Min = %d\n", min);
            decile = 1;
            for (decile = 10; decile < 100; decile += 10)
            {
                idx = int((decile * n) / 100) + 1;
                printf("# %d%% decile = %d\n", decile, item[idx]);
                if (decile == 50)
                    median = item[idx];
            }
            printf("# Max = %d\n", max);
    
            printf("# Median = %d\n", median);
            for (i in count)
            {
                if (count[i] > count[mode])
                    mode = i;
            }
            printf("# Mode = %d\n", mode);
        }'
    

    The initial values of min and max are not exactly scientific. It serves to illustrate a point.

    (This 1997 version is almost identical to its 1991 predecessor – all except for the version information line is identical, in fact. So, the code is over 20 years old.)

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

Sidebar

Related Questions

I have a file (dictionary.txt) with data field entries as follows - ABC This
I have a .txt file which has about 500k entries, each separated by new
I have a file with entries such as: 26 1 33 2 . .
I have text file with entries like 123 112 3333 44 2 How to
I have a CSV file with several entries, and each entry has 2 unix
I have the following file dupeExtracter.rb to remove duplicate entries from a DB table
I have duplicate entries in my log file for NServiceBus and I'm concerned this
I have the following entries in the css file. a.intervalLinks { font-size:11px; font-weight:normal; color:#003399;
I have a txt file with some URLs like this http://url1.com http://url1.com http://url1.com Separated
I have a big file with entries as opened in python as: fh_in=open('/xzy/abc', 'r')

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.