multiply data of a file by a factor

June 3, 2009 at 06:06:09
Specs: Linux i686
Hi, I need to make a shell script that multiply each line of my data file by the same factor, but I don't know how. For instance, I have a data file of this type, (only with much more lines):

-664.9768
-664.9738
-664.9706
-664.9736


and I want to multiply each line by 20, and make another file with the results. How can I make it? Thanks, I'd really appreciate some help.


See More: multiply data of a file by a factor

Report •


#1
June 3, 2009 at 06:58:39
awk '{print $0*20}' file > newfile


Report •

#2
June 3, 2009 at 06:59:46
I'll give you the pieces. First, my bash shell is 3.2 so it only does integer arithmetic so I use the bc utility to do the math: No error checking:

#!/bin/bash

line=-664.9768
x=$(bc << NAILS
      scale=4
      ($line * 20)
NAILS)
echo $x


Second, this stub reads a file, line by line, and sends it to a new file:


while read line
do
   echo "$line"
done < datafile.txt > newfile.txt

I'll leave it to you to put them together


Report •

Related Solutions


Ask Question