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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:06:58+00:00 2026-06-13T00:06:58+00:00

I have a few data text files whose lines are values with their error,

  • 0

I have a few data text files whose lines are values with their error, like these:

...
6  90.3785794742981 0.0952997386139722
40 1028.46336161948 4.41798447319325
...

the third column is the error relativley to the second column. I would like to write a script that prints them in a more human-readable format, that is to print the value with the right number of significant digits and to print the error on the last two digist between parenthesis, like this:

...
6  90.379(95)
40 1028.5(4.4)
...

using regular expressions to extract the numbers wouldn’t work right because of difficulties of handling the dot and because it would truncate numbers rather than approximate them, so i thought that i’d rather retrieve their magnitude with printf and handle them with bc.

The code I wrote for this is as follows

#! /bin/bash
while read a v verr
do
    ov=`printf %e $v`
    ov=${ov/*e/}
    overr=`printf "%e" $verr`
    overr=${overr/*e/}
dov=$((1-$overr))
v=`echo "scale=0;$v*10^($dov)" | bc -l`
v=`printf %.0f $v`
printf "$a %f(%.0f)\n" `echo "lenght=length($v);$v*10^($((-$dov)))" | bc -l` `echo "$verr*10^($dov)" | bc -l`
 done < myfile.txt

what i get is

6 90.379000(95)
40 1028.500000(44)

My code almost works, except for the appearence of those trailing zeroes.

How do I get rid of them? Just cutting them would not be good because their number is not fixed and cutting them all would give rise to errors wheter the last digit actually is a zero.

  • 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-06-13T00:06:59+00:00Added an answer on June 13, 2026 at 12:06 am

    The following code works.

    #! /bin/bash
    while read a v verr
    do
    ov=`printf %e $v`
        ov=${ov/*e/}
        overr=`printf %e $verr`
    overr=${overr/*e/}
    dov=$((1-$overr))
    v=`echo "scale=0;$v*10^($dov)" | bc -l`
    v=`printf %.0f $v`
    
    if [ $dov -gt 0 ]; then
        # remember bc doesn't understand numbers in scientific notation,
        # therefore we need to convert such numbers in regular notation.
        u=`echo ${u} | sed 's/[eE]/\\*10\\^/' | sed 's/+//'`
        verr=`echo ${verr} | sed 's/[eE]/\\*10\\^/' | sed 's/+//'`
    
        u=`echo "$v*10^($((-$dov)))" | bc -l`
        if [ $overr -eq 0 ]; then
        uerr=`echo "$verr*10^($dov - 1)" | bc -l`
        printf "$a\t%.${dov}f(%.1f)\n" $u $uerr>>$s
        else
        uerr=`echo "$verr*10^($dov)" | bc -l`
        printf "$a\t%.${dov}f(%.0f)\n" $u $uerr>>$s
        fi
    else
        echo $a "${v}(${verr})E$ov">>$s
    fi
    done < $f
    

    it prints the values formatted as I wanted. The first case tests whether it needs to express the result in exponential notation in order to get significant digits right, and the second if case tests wheter it should put a dot in the error.

    I have not tested the third line of code from the bottom because I do not need it now, so be cautious because it might not work.

    EDIT: I inserted two sed calls to handle scientific notation numbers.

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

Sidebar

Related Questions

I have few text and excel file, from which I need to import data
I have a table with a few rows of data. I would like to
I have a few HTML files that I'd like to include via tags in
I have a few SQL statements stored in text files. How do I pull
I have an application that imports data read from text files from a directory
I have a function that creates 7 different text files with rows of data.
I have created a few little programs that export data to a text file
I have few questions about using lock to protect my shared data structure. I
I'm manipulating few data with PHP, I have the choice between Json and XML.
I have a table part with a few demo data as below in Oracle

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.