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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
create a thread from a returning function wolwy_pete High Level Programming 3 05-08-2008 03:48 AM
create function with awk kamel.seg Shell Programming and Scripting 2 12-24-2007 05:36 PM
sqrt is not find??? murataht High Level Programming 2 10-17-2004 10:04 AM
create thread C with JNI function with JAVA AUBERT HP-UX 0 08-06-2004 05:24 AM
sqrt CreamHarry High Level Programming 1 06-03-2002 07:56 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-10-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
How to create SQRT function in catenate file

HI,

I have a file which i catenate and using the fields in the file, I would like to get sqrt of it. I tried to man the function but it normally would need an echo as well as bc.

What I am intending to find out is catenate a file where let say
cat a.txt| awk ' {
t= h*($3+$2);
t= 'sqrt($t)' | bc -l
count($1)=$2/t
}
END{

printf("the sqrt of %d is %d\n", $2,$1);
}
However, it does not work. Please help.


Thanks.


Rgrds,
Jason
  #2 (permalink)  
Old 12-10-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by ahjiefreak View Post
HI,

I have a file which i catenate and using the fields in the file, I would like to get sqrt of it. I tried to man the function but it normally would need an echo as well as bc.
Why?
Why cannot you use awk's 'sqrt'?
Quote:
Originally Posted by ahjiefreak
What I am intending to find out is catenate a file where let say
cat a.txt| awk ' {
t= h*($3+$2);
t= 'sqrt($t)' | bc -l
count($1)=$2/t
}
END{

printf("the sqrt of %d is %d\n", $2,$1);
}
However, it does not work. Please help.


Thanks.


Rgrds,
Jason
  #3 (permalink)  
Old 12-10-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Hi ,

Do you mean using the same code that I described earlier on?


Please advise. Thanks.


-Jason
  #4 (permalink)  
Old 12-10-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by ahjiefreak View Post
Hi ,

Do you mean using the same code that I described earlier on?


Please advise. Thanks.


-Jason
No, this 'code' will not work for multiple reasons.
What I mean (for starters) why cannot use awk's "sqrt" built-in function?
I guess I don't understand:
  1. what "t=sqrt($t)' | bc -l " is supposed to do
  2. what is the value of 'h' in "t= h*($3+$2);"
  3. What do think the value of $2 and $1 will be in the 'END' block
  4. what's the intent of this: count($1)=$2/t
  5. why are you doing this 'cat a.txt'
  #5 (permalink)  
Old 12-10-2007
ahjiefreak ahjiefreak is offline
Registered User
  
 

Join Date: Nov 2007
Posts: 132
Hi,

The answer to this question is:-

1. what "t=sqrt($t)' | bc -l " is supposed to do
2. what is the value of 'h' in "t= h*($3+$2);"
I am basically trying to get an operation of two fields of columns $3 and $2 with multiplication of some constant value(h). These operation will end up giving me a number which is t. Then, this t value I would like to get its square root and assign to a new variable.

Perhaps I should write as "w=sqrt($t)" |bc -l in the first place.

3. What do think the value of $2 and $1 will be in the 'END' block
4. what's the intent of this: count($1)=$2/t
I would like to divide again the field of column 2 with the square root value to associate with field $1. Basically in my case, $1 is unique line number and $2 is just a numeric value for each line in the file a.txt.

5. why are you doing this 'cat a.txt'
The cat a.txt is bascially I would like to open the file which contains all the numbers and have 3 fields; $1, $2 and $3.

-Jason
  #6 (permalink)  
Old 12-10-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by ahjiefreak View Post
Hi,

The answer to this question is:-

1. what "t=sqrt($t)' | bc -l " is supposed to do
2. what is the value of 'h' in "t= h*($3+$2);"
I am basically trying to get an operation of two fields of columns $3 and $2 with multiplication of some constant value(h). These operation will end up giving me a number which is t. Then, this t value I would like to get its square root and assign to a new variable.
Where the 'constant' (h) gets assigned it's value?
What do you think the value of '$t' in 'sqrt($t)' is?
Why are you trying "pipe" 'sqrt($t)' into 'bc -l'? Wouldn't a simple 't=sqrt(t)' be enough?
Quote:
Originally Posted by ahjiefreak
Perhaps I should write as "w=sqrt($t)" |bc -l in the first place.

3. What do think the value of $2 and $1 will be in the 'END' block
4. what's the intent of this: count($1)=$2/t
I would like to divide again the field of column 2 with the square root value to associate with field $1. Basically in my case, $1 is unique line number and $2 is just a numeric value for each line in the file a.txt.
'count($1)' looks like a function 'count' taking a parameter with value of '$1'. And you're assigning '$2/t' to the function. This does not make any sense.
DO mean you want to use the associative array indexed by the value of your FIRST '$1' column? Something like that:
Code:
countArray[$1] = $2/t
Quote:
Originally Posted by ahjiefreak
5. why are you doing this 'cat a.txt'
The cat a.txt is bascially I would like to open the file which contains all the numbers and have 3 fields; $1, $2 and $3.

-Jason
The general awk paradigm for 'reading' files is:
Code:
awk '... awk BODY...' myInputFile.

Last edited by vgersh99; 12-10-2007 at 03:18 AM..
Closed Thread

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 02:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
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