简体   繁体   中英

Bash Script- adding numbers to each line in file

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?

Bash doesn't handle floating point arithmetics. You need an external command, eg bc :

sed 's/$/+5/' file.txt | bc

The sed command just adds +5 to the end of each line, it's faster and easier than reading the input line by line by the shell.

Bash can only do integer arithmetic. The simplest way is probably to use bc :

while read i; do
   echo $(bc <<< "$i + 5")                                                  
done <filename.txt > shifted_by_5_filename.txt

Alternatively, you could use some general purpose interpreter, eg Perl:

perl -ple '$_ += 5' filename.txt > shifted_by_5_filename.txt

Arithmetic expansion in bash works only for integers, so for floats you'll need bc , awk , python , perl , ruby , or similar. For example with bc :

while read i; do
   echo "$i + 5" | bc -l
done <filename.txt > shifted_by_5_filename.txt

Or with this simple awk script:

awk '{print $0+5}' filename.txt >shifted_by_5_filename.txt

I believe that there are two pretty clear issues here. First of all it looks like you are missing a parenthesis when you are performing the addition. Secondly, you do not need a dollar on the number 5.

With a shift variable:

while read i; do
    shift=5
    sum=$(($i+$shift))
    echo $sum
done < input.txt > output.txt

Or if you want to use your constant:

while read i; do
    sum=$(($i+5))
    echo $sum
done < input.txt > output.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM