LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Advice for a fairly simple(?) script (https://www.linuxquestions.org/questions/programming-9/advice-for-a-fairly-simple-script-489497/)

frankie_DJ 10-04-2006 04:48 PM

Advice for a fairly simple(?) script
 
Hi everyone,

I have bunch of files of this format


Title
number1,number2
10000
12000
L
0.7
0.7
0.0

I want to write a shell script that writes for each file:

Temperature: number1
Time: 2nd_line * 6th_line
Cooling Rate: (number1-number2)/(6th_line*4th_line)

What's the most efficient way to go about this? I am not so bad with bash untill I have to batch process bunch of files like this. I know tiny little bit of awk and sed. Should I use them? Would it be an overkill if I do? Would expr utility be helpfull here? Please help.

jonaskoelker 10-04-2006 05:08 PM

Code:

IFS='
' # or possibly $'\n' if you like that syntax better
for fp in those-files/*; do {
  read title # Title
  IFS=',' read number1 # number1
  read number2 # number2
  read line2 # 10000
  read line3 # 12000
  read line4 # L
  read line5 # 0.7
  read line6 # 0.7
  echo "Temperature: $number1"
  echo "Time: $(($line2 * line6))"
  echo "Cooling Rate: $((($number1 - $number2) / ($line6 * $line4)))"
} < $fp; done

I think...

ghostdog74 10-04-2006 07:04 PM

Quote:

Originally Posted by frankie_DJ
Hi everyone,

I have bunch of files of this format


Title
number1,number2
10000
12000
L
0.7
0.7
0.0

I want to write a shell script that writes for each file:

Temperature: number1
Time: 2nd_line * 6th_line
Cooling Rate: (number1-number2)/(6th_line*4th_line)

What's the most efficient way to go about this? I am not so bad with bash untill I have to batch process bunch of files like this. I know tiny little bit of awk and sed. Should I use them? Would it be an overkill if I do? Would expr utility be helpfull here? Please help.


Alternative in Python:

Code:

#!/usr/bin/python
data = open("test.txt").read().split()
temp1,temp2 = data[1].split(",")
thetime = int(data[2]) * float(data[-2])
cooling rate = temp1 - temp2 / ( float(data[-2]) *  int(data[4] ) )


pete1234 10-04-2006 08:00 PM

Another way in bash:
Code:

#!/bin/bash -
#

for i in $(ls whatever); do
  TEMP=(`sed -n 2p $i | awk -F"," ' /[0-9]+/ {print $1,$2}'`)
  TIME=`echo "$(sed -n 3p $i) * $(sed -n 7p $i)" | bc`
  CRATE=`echo "${TEMP[0]} - ${TEMP[1]} / $(sed -n 4p $i) * $(sed -n 6p $i)" | bc`
  echo "Temperature: ${TEMP[0]}"
  echo "Time: $TIME"
  echo "Cooling Rate: $CRATE"
done

I think that's what you wanted anyway.

frankie_DJ 10-04-2006 08:43 PM

OK thank you guys. Wow. I can't believe I lost all afternoon on this thing.

SOrry ghostdog, but I don't have time to deal with yet another language. Although from what you wrote python seems really powerfull.

Jonaskoelker, thanks for the script. It didn't work out of the box, since shell doesn't do arithmetics with real numbers, so I had to pipe to bc. Also the second use of IFS (IFS=',') doesn't really do what it's supposed to and I don't really know how to fix that.

Edit: Pete, thanks, I will see if your script works better. By the way, isn't it overkill to use awk and sed? I mean sed loads about 50K of memory and awk probably even more. Just curious.

pete1234 10-04-2006 08:51 PM

Quote:

Edit: Pete, thanks, I will see if your script works better. By the way, isn't it overkill to use awk and sed? I mean sed loads about 50K of memory and awk probably even more. Just curious.
You're welcome, and you're probably right. I'm sure you could do it with sed or awk alone. I wasn't really thinking about conserving. system resources.

##EDIT This seems to work with just sed:

Code:

#!/bin/bash -
#

for i in $(ls whatever); do
  TEMP=(`sed -n -e 's/,/ /p' $i`)
  TIME=`echo "$(sed -n 3p $i) * $(sed -n 7p $i)" | bc`
  CRATE=`echo "${TEMP[0]} - ${TEMP[1]} / $(sed -n 4p $i) * $(sed -n 6p $i)" | bc`
  echo "Temperature: ${TEMP[0]}"
  echo "Time: $TIME"
  echo "Cooling Rate: $CRATE"
done


ghostdog74 10-04-2006 09:36 PM

Quote:

Originally Posted by frankie_DJ
SOrry ghostdog, but I don't have time to deal with yet another language.

hey no need to say sorry, its just an alternative..
Quote:

Although from what you wrote python seems really powerfull.
it is.

frankie_DJ 10-04-2006 10:50 PM

Thanks guys, I spend half a day on this but it wasn't wasted - I learned tons. If someone sees this and has few minutes, I wouldn't mind a version with awk only.

Also, I was curious why switching IFS doesn't work in the first version.

makyo 10-04-2006 11:32 PM

Hi.

Here's the beginning of an awk version, untested because there was no live data posted that I saw. You may need to adjust the variables, and, of course, remove the "next" statement ... cheers, makyo
Code:

#!/bin/sh

# @(#) a1      Demonstrate slurping in a whole file.

FILE=${1-data1}

awk '
BEGIN  { FS = "[, \t\n]" ; RS = "" }
        {
        print "field " $1, $2, $3, $4, $5, $6, $7, $8, $9
        # No real data, skip computation
        next
        title = $1 ; number1 = $2 ; number2 = $3
        line2 = $4 ; line3 = $5 ; line4 = $6 ; line6 = $8
        print "Temperature:", number1
        print "Time:", line2 * line6
        print "Cooling rate:", (number1-number2) / ( line6*line4)

        }
END    {
        }' $FILE

Produces on the outline data:
Code:

% ./a1
field Title number1 number2 10000 12000 L 0.7 0.7 0.0



All times are GMT -5. The time now is 08:04 AM.