I have a .txt file containing a column of values, something like:
2453882.157576
2453882.157947
2453882.159972
2453882.160354
2453882.160724
And I want to add a single value to each line in the code, let's say 5. So the output would be:
2453887.157576
2453887.157947
2453887.159972
2453887.160354
2453887.160724
I'm fairly new to bash commands and I am a bit unsure what commands to use. I thought it might be useful to try to iterate through the file and simply add through it:
while read i; do
shift=$(($i + $5))
done <filename.txt > shifted_by_5_filename.txt
but this outputs an error "invalid arithmetic operator". Are there any suggestions on how to accomplish this task?
UPDATE: With some of the suggestions below, I've looked into using awk to try to add floats (rather than interger addition). I've found another question: how to add Integer number and a float number in unix shell script
that has an answer to add two floats:
echo 1.234 2.345 | awk '{print $1 + $2}'
To try to extend this to my problem, I've tried the following:
while read i
do echo $i 5 | awk '{print sprintf("%.9f", ($1+ $2))}'
done < filename.txt > shifted_by_5_filename.txt
The sprintf command is to try to produce an output not in scientific notation. However, as with the suggestions below, this attempt does not add by 5 rather it adds by 2.771413. I'm a bit perplexed by this. Any ideas?