LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-17-2012, 07:38 PM   #1
chumpp
LQ Newbie
 
Registered: Mar 2012
Posts: 12

Rep: Reputation: Disabled
Need help with a looping structure.


I have this input file

1
8
22
43
89
283
120
212
1043
100
287

I need to take these numbers and make them Celsius to Fahrenheit and Fahrenheit to Celsius they also need to be rounded to the nearest whole number.

I know i need to use a while loop but other than that im lost.
 
Old 04-17-2012, 07:56 PM   #2
towheedm
Member
 
Registered: Sep 2011
Location: Trinidad & Tobago
Distribution: Debian Jessie
Posts: 592

Rep: Reputation: 119Reputation: 119
You can do it with a for loop:
Code:
for line in $(cat yourfile); do
  <Code to convert C to F>
  echo result
  <Code tp convert F to C>
  echo result
done
Using a while loop:
Code:
while read line; do
  <Code to convert C to F>
  echo result
  <Code tp convert F to C>
  echo result
done < yourfile
 
Old 04-17-2012, 08:05 PM   #3
chumpp
LQ Newbie
 
Registered: Mar 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by towheedm View Post
You can do it with a for loop:
Code:
for line in $(cat yourfile); do
  <Code to convert C to F>
  echo result
  <Code tp convert F to C>
  echo result
done
Using a while loop:
Code:
while read line; do
  <Code to convert C to F>
  echo result
  <Code tp convert F to C>
  echo result
done < yourfile
thanks ill work on it and ill come back if i get stuck. Thanks again for sparing your time to help me!
 
Old 04-18-2012, 12:47 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
You may find these useful
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
Old 04-18-2012, 02:51 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Also be aware that the shell will only do integer arithmetic and it does not round but rather truncate values.
 
Old 04-18-2012, 07:23 AM   #6
chumpp
LQ Newbie
 
Registered: Mar 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Script 1 Main
Code:
  1 echo "Centigrade Temperature"        "Fahrenheit Temperature"
  2 echo "-----------------------"      "------------------------"
  3 while read line; do
  4 tf=$(echo "scale=2;((9/5) * $line) + 32" |bc)
  5 echo -e  "\t$line               \t$tf"
  6 done < 3input
Script 2 sub
Code:
  1 #!/bin/bash
  2 echo "Fahrenheit Temperature"             "Centigrade Temperature"
  3 echo "-----------------------"      "------------------------"
  4 while read line; do
  5 tc=$(echo "scale=2;(5/9)*($line - 32)"|bc)
  6 echo -e  "\t$line               \t$tc"
  7 done < 3input
i dont know how to round it to the nearest whole number. Also the math is a bit off on my second script and i dont know why.

Code:
Centigrade Temperature Fahrenheit Temperature
----------------------- ------------------------
        1                       33.8
        8                       46.4
        22                      71.6
        43                      109.4
        89                      192.2
        283                     541.4
        120                     248.0
        212                     413.6
        1043                    1909.4
        100                     212.0
        287                     548.6


Fahrenheit Temperature Centigrade Temperature
----------------------- ------------------------
        1                       -17.05
        8                       -13.20
        22                      -5.50
        43                      6.05
        89                      31.35
        283                     138.05
        120                     48.40
        212                     99.00
        1043                    556.05
        100                     37.40
        287                     140.25

Last edited by chumpp; 04-18-2012 at 09:02 AM.
 
Old 04-18-2012, 09:09 AM   #7
chumpp
LQ Newbie
 
Registered: Mar 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
EDIT * the formula was wrong but i still need help rounding the numbers

Last edited by chumpp; 04-18-2012 at 09:16 AM.
 
Old 04-18-2012, 09:30 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,450

Rep: Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776
add 0.5 before piping to bc and cut .anything:
bc | sed 's![.].*!!'
 
Old 04-18-2012, 09:32 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
Does it have to be bash and bc? The vagaries of the scale command imply their own limitations at each point of the calculation which cause wildly fluctuating answers.
If not constrained I would probably use awk with the printf command to assist with the roundings
 
Old 04-18-2012, 09:35 AM   #10
chumpp
LQ Newbie
 
Registered: Mar 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
it needs to be bash

---------- Post added 04-18-12 at 10:35 AM ----------

Quote:
Originally Posted by pan64 View Post
add 0.5 before piping to bc and cut .anything:
bc | sed 's![.].*!!'
dont understand heh
 
Old 04-18-2012, 09:39 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,450

Rep: Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776Reputation: 2776
Quote:
Originally Posted by chumpp View Post

dont understand heh
Code:
tf=$(echo "scale=2;((9/5) * $line) + 32" |bc)                         # old
tf=$(echo "scale=2;((9/5) * $line) + 32 + 0.5" | bc | sed 's/\..*//')  # new
 
Old 04-18-2012, 09:47 AM   #12
chumpp
LQ Newbie
 
Registered: Mar 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Code:
tf=$(echo "scale=2;((9/5) * $line) + 32" |bc)                         # old
tf=$(echo "scale=2;((9/5) * $line) + 32 + 0.5" | bc | sed 's/\..*//')  # new
ok i see what your doing but this is my question.
Code:
  1 #!/bin/bash
  2 echo "Fahrenheit Temperature"             "Centigrade Temperature"
  3 echo "-----------------------"      "------------------------"
  4 while read line; do
  5 tc=$(echo "scale=0;($line - 32) * 5 / 9"|bc)
  6 echo -e  "\t$line               \t$tc"
  7 done < 3input
how come when i set this the scale to 0 in this script the math is right. When i tried to set the scale to 0 in my first script why was the math wrong? i just want to understand the logic.
 
Old 04-18-2012, 10:55 AM   #13
chumpp
LQ Newbie
 
Registered: Mar 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
? still kinda lost on the logic on why the second part works when i set the scale to 0 and the first dont
 
Old 04-18-2012, 12:00 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,516

Rep: Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893Reputation: 2893
I was wondering that too, but was able to finally work it out. If you set the scale to zero and do the fraction first, ie. 5/9, with a scale of zero this becomes 0, hence the rest of the equation
is multiplied by 0 hence wrong results. I believe if you alter both functions so that the division is last you should be ok.
 
Old 04-18-2012, 12:49 PM   #15
chumpp
LQ Newbie
 
Registered: Mar 2012
Posts: 12

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I was wondering that too, but was able to finally work it out. If you set the scale to zero and do the fraction first, ie. 5/9, with a scale of zero this becomes 0, hence the rest of the equation
is multiplied by 0 hence wrong results. I believe if you alter both functions so that the division is last you should be ok.
ah ok i get it now.
 
  


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
Could someone help me with looping in vb cobrien Programming 9 11-12-2010 08:17 AM
Convert directory structure from long file names in Linux to DOS 8.3 structure? manorina Linux - Software 5 09-12-2009 09:18 AM
Home Jail Folder Structure like Gobolinux Directory Structure luispt Linux - General 3 07-26-2008 06:46 PM
Looping a script Keentolearn Linux - Newbie 4 01-30-2007 03:54 PM
Bash case structure (looping) ravvar Programming 4 10-07-2003 07:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:28 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration