The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed to grep only numbers in string ajilesh Shell Programming and Scripting 5 11-07-2008 01:39 PM
How to sort a string with numbers ahjiefreak Shell Programming and Scripting 5 12-21-2007 10:52 AM
How to Compare Floating point / real numbers padarthy Shell Programming and Scripting 13 09-24-2007 08:03 PM
How do i get numbers from a string? eliraza6 Shell Programming and Scripting 13 07-18-2007 07:04 AM
problem with floating point numbers in awk kanagias Shell Programming and Scripting 7 06-24-2005 03:14 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-29-2009
tintin72 tintin72 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 23
Using Floating Numbers in String

Dear Unix Gurus,

I have a list of files that I want to loop over....for example:
Code:
 
sl40_z11.70.txt
sl41_z11.40.txt
sl42_z11.10.txt
sl43_z10.80.txt
using the script

Code:
 
#!/bin/sh
#
echo -n "....enter first Z-coordinate position....."; read zpos
q="scale=3; $zpos"
p=0.3
#
loopNumber=$[($lastslice - $firstslice)+1]
echo "loopNumber is $loopNumber"
echo firstslice no. is $firstslice
 
for ((i=$firstslice; i<=$loopNumber; i++)); do 
n=$[i]
for ((k=0; k<=$loopNumber; k++)); do 
r=$[k] ; f=$r*$p ; zposition=$q-$f 
 
echo "sl$[n]_z$[zposition].txt"
 
done
done
where firstslice is 40 and lastslice is 43 and 0.3 is the difference in z-cordinate btw the files.

My problem is that the script works fine with the exception that bash doesn't recognize $zposition. It apparently can't handle iteration with floating numbers? Can someone help?

Cheers
  #2 (permalink)  
Old 06-29-2009
jayan_jay jayan_jay is offline
Registered User
  
 

Join Date: Jul 2008
Location: Chennai
Posts: 40
Try this and change according to ur variables.

echo "scale=4;$b+$c" | bc
  #3 (permalink)  
Old 06-29-2009
tintin72 tintin72 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 23
Quote:
Originally Posted by jayan_jay View Post
Try this and change according to ur variables.

echo "scale=4;$b+$c" | bc
I'm not sure I understand you. Doing
Code:
echo "$zposition" |bc
returns the correct output. But doing
Code:
echo "sl$[n]_z$[zposition].txt"
does not. This is my problem.
  #4 (permalink)  
Old 06-29-2009
jayan_jay jayan_jay is offline
Registered User
  
 

Join Date: Jul 2008
Location: Chennai
Posts: 40
Try this
echo "sl${n}_z${zposition}.txt"
  #5 (permalink)  
Old 07-01-2009
tintin72 tintin72 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 23
Quote:
Originally Posted by jayan_jay View Post
Try this
echo "sl${n}_z${zposition}.txt"
doesn't work. can I just ask..did you actually try out your suggestions bufore suggesting them to the forum?
  #6 (permalink)  
Old 07-01-2009
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,861
Humm, I have looked at this shell script a few times and scratched my head.

You set the variable 'q' to a string
Code:
q="scale=3; $zpos"
yet you expect to be able to subtract a number from it
Code:
zposition=$q-$f
How is this meant to work?

Last edited by fpmurphy; 07-01-2009 at 08:12 PM.. Reason: fixed typo
  #7 (permalink)  
Old 07-06-2009
tintin72 tintin72 is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 23
Quote:
Originally Posted by fpmurphy View Post
Humm, I have looked at this shell script a few times and scratched my head.

You set the variable 'q' to a string
Code:
q="scale=3; $zpos"
yet you expect to be able to subtract a number from it
Code:
zposition=$q-$f
How is this meant to work?
Hi,

I revisited it too and scratched my head as well! There's a bug in the script...if firstslice is bigger than lastslice it won't work. So I've tried a different tack using awk.

as a reminder, this is what I want to do; I have a list of files:

Code:
 
sl40_z11.70.txt
sl41_z11.40.txt
sl42_z11.10.txt
sl43_z10.80.txt
I want to loop through these files keeping their labelling intact. For example, I want to do in a single script

Code:
cp sl40_z11.70.txt sl40_z11.70.dat
cp sl41_z11.40.txt sl41_z11.40.dat
cp sl42_z11.10.txt sl42_z11.10.dat
cp sl43_z10.80.txt sl43_z10.80.dat
I have hundreds of these files and I need to carry out other non-trivial operations on them. Problem is bash does not recognize floating numbers so I can't loop over them.

my new script is :

Code:
 
#!/bin/sh
#
echo -n "....enter first slice number....."; read firstslice
echo -n "....enter last slice number....."; read lastslice
#
echo -n "....enter first Z-coordinate position....."; read zpos
#sliceDiff=0.3
#
loopNumber=$[($lastslice - $firstslice)+1]
echo "loopNumber is $loopNumber"
echo firstslice no. is $firstslice
for ((k=$firstslice;k<=$lastslice; k++)); do 
r=$[k]
echo "$firstslice $r $zpos" | awk '{print $3-(($2-$1)*0.3)}'
#echo "$firstslice $r $zpos" | awk '{print $i}' sl$2_z[$3-(($2-$1)*0.3)].txt > xxxx.dat
done
So running this script and entering at prompts "40" , "43", "11.70"
loops over the 4 files nicely. But I don't know how to incorporate the output into the last line of the script so that awk recognizes the filename.

Any help would be much appreciated.

Cheers
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:18 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0