-1

I have 2 files:

a.txt:

0.513603
0.513141
0.513141
0.513046
0.513263
0.512889
0.512889
0.512097
0.51112
0.511863

b.txt:

0.5132405
0.51319025
0.51311625
0.51305325
0.512903125
0.512516625
0.5121205
0.5118095
0.511543
0.511598125

I want to know % change from both file to their corresponding line. I not able to think of how to implement it using Shell script. Every 5 minute data will be generated in both files but no of records will be same in both files each time.

Example:

(0.513603 - 0.5132405)/100  
Biffen
  • 6,249
  • 6
  • 28
  • 36
getashu1
  • 77
  • 9

3 Answers3

0

Got it working with below code. but only thing is that you will end of with no data in /tmp/b file. In my case it is ok because /tmp/b will be generated again after 5 min.

while IFS='' read -r line || [[ -n "$line" ]]; do

line2=`head -1 /tmp/b`
sed -i '1d' /tmp/b

   //  do processing between line and line2 whatever you want

done < /tmp/a
Biffen
  • 6,249
  • 6
  • 28
  • 36
getashu1
  • 77
  • 9
0

IIUC, this is what you are looking for

a=($(<a.txt))
b=($(<b.txt))

for i in for i in "${!a[@]}"
do
    printf '100\n%f\n%f\n-\n*\np\n' ${a[i]} ${b[i]} | dc
done

read the 2 files into arrays and then use dc to do floating point arithmetic.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

paste the two files together, change the two values into proper calculation with sed and let bc calculate it

{
  echo "scale=9 "
  paste a.txt b.txt
} |  sed -re 's#(.*)\t(.*)#100*(\1-\2)/\1#' | bc > percentChanged.percent

paste a.txt b.txt | paste - percentChanged.percent
ULick
  • 969
  • 6
  • 12