Beertje Knuffel
Beertje Knuffel

Reputation: 15

Read out 1 value from file and make a string/variable in bash

I need a clean and fast way to read a value from a file and put this in a variable. The files contens are:

02102014,174620,1,0,1045,3358,3,0,1045,0,0,0,0,0,941,4998,2248,4,0,941    

So i would get first value as date, seccond as time, third value as number1 etc etc

Also i would like to adjust the values (example: 3358 must be 33.58 or 335)

I hope you can understand my explanation:

Whats the best way to do this?

value=Number6 Number6 = float(value[0])/10

Somthing like this ?

My script is in BASH

INPUT=DATA.tmp  #like 03102014,132051,1,3558,0,34568 one line

[ ! -f $INPUT ] && { echo "$INPUT file not found"; exit 99; }

IFS=,

while read line; do

#LINE="03102014,132051,1,3558,0,34568"
LINE_LIST=($LINE)

for list in ${LINE_LIST[@]}; do 
echo $list
done

for single in ${LINE_LIST[3]}; do
echo $single
done

done < $INPUT

Why is it not working with the file reading??

"Bash may not be the best tool for the job. For instance, it doesn't have floating point arithmetic. However:"

Can i work this out with BC ?

store[3]=$(echo "scale=3; ${storenew[3]} * 1.000" | bc)    

Upvotes: 0

Views: 64

Answers (1)

ooga
ooga

Reputation: 15501

Bash may not be the best tool for the job. For instance, it doesn't have floating point arithmetic. However:

echo '02102014,174620,1,0,1045,3358' | 

while IFS=, read -a a; do

  x=${a[0]}
  month=${x:0:2}
  day=${x:2:2}
  year=${x:4:4}
  echo $month $day $year

  x=${a[1]}
  hour=${x:0:2}
  min=${x:2:2}
  sec=${x:4:2}
  echo $hour $min $sec

  units=$((a[5] / 100))
  fracs=$((a[5] % 100))
  echo $units $fracs

done

Output

02 10 2014
17 46 20
33 58

Upvotes: 1

Related Questions