LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-04-2006, 04:48 PM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 32
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.

Last edited by frankie_DJ; 10-04-2006 at 04:57 PM.
 
Old 10-04-2006, 05:08 PM   #2
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
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...
 
Old 10-04-2006, 07:04 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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] ) )
 
Old 10-04-2006, 08:00 PM   #4
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Rep: Reputation: 30
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.
 
Old 10-04-2006, 08:43 PM   #5
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
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.

Last edited by frankie_DJ; 10-04-2006 at 08:45 PM.
 
Old 10-04-2006, 08:51 PM   #6
pete1234
Member
 
Registered: May 2005
Distribution: Slack, FreeBSD,NetBSD, OpenBSD, Open Solaris, Minix
Posts: 172

Rep: Reputation: 30
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

Last edited by pete1234; 10-04-2006 at 09:13 PM.
 
Old 10-04-2006, 09:36 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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.
 
Old 10-04-2006, 10:50 PM   #8
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
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.
 
Old 10-04-2006, 11:32 PM   #9
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Need advice to set up computer for a few simple tasks. JackSmith Linux - Newbie 1 12-13-2005 07:14 PM
Fairly simple bash problem xxx_Birdman_xxx Programming 5 06-16-2004 11:27 PM
Samba issues (fairly simple) Brother Michael Debian 2 05-13-2004 02:56 AM
Simple C Shell script is not so simple elconde Programming 2 09-16-2001 11:53 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration