Tuesday, May 7, 2024
15
rated 0 times [  15] [ 0]  / answers: 1 / hits: 1654  / 2 Years ago, wed, october 12, 2022, 2:36:18

How can we convert dalton values into kilodalton by using shell commands? I have a list of values in daltons. There are 1000 daltons in a kilodalton, so it is necessary to divide each value by a thousand.



Sample data:



12345.09
236575.665
18865.41
45678.87


Desired output:



12.34509
236.575665
18.86541
45.67887

More From » command-line

 Answers
7

With awk


The task here is simply to divide each value by a thousand. There are several good ways to do this--on all the values in your file--with just a single command. Suppose your numbers are in a file called dalton.txt and, as in the example you showed, are one value per line. Here's one way, using AWK:


awk '{ print $0 / 1000.0 }' dalton.txt

That operates on each line. $0 in AWK indicates the whole line. As written that outputs to your terminal but you can redirect the output to a file. This sends the output to kilodalton.txt (overwriting it if it already exists):


awk '{ print $0 / 1000.0 }' dalton.txt > kilodalton.txt

However, you may want to customize the precision. Given the example input you showed us, the output to your terminal or the file from using those commands is:


12.3451
236.576
18.8654
45.6789

The your output example, each output value retains the same precision (in terms of number of digits of information shown) as the corresponding input value. If you want that:


awk '{ printf "%.*g
", length($0), $0 / 1000.0 }' dalton.txt

This still truncates trailing zeros after the decimal point, since that's how the printf function's g format specifier behaves. (Your examples don't make clear if you want that.) The way it works is simply to specify a precision that is always at least as high as the number of digits in the input, but making it the total length of the input line, length($0).


That command may of course also be redirected to a file, and its output is exactly the output shown in your question:


12.34509
236.575665
18.86541
45.67887

Often you may want to specify the same precision for each entry, either as the total number of figures shown or as the number of figures show after the decimal point. For example, to show a total of 6 figures for each value (the first line will be 12.3451), use:


awk '{ printf "%.6g
", $0 / 1000.0 }' dalton.txt

Or to show 3 figures after the decimal place for each value (the first line will be 12.345) use:


awk '{ printf "%.3f
", $0 / 1000.0 }' dalton.txt

Although it is possible to write a loop in a shell script that reads the data, that's more complicated and it is rarely the best approach when a simple command will do it for you. Even if this is part of a larger shell script already, shells are glue languages that are meant to be used to call whatever tools are best suited to a task. So I suggest using this awk method, terdon's excellent perl method, or the like.


[#10221] Wednesday, October 12, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
nerta

Total Points: 414
Total Questions: 103
Total Answers: 97

Location: England
Member since Wed, Apr 19, 2023
1 Year ago
;