The UNIX and Linux Forums  

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 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 12:48 AM
create function with awk kamel.seg Shell Programming and Scripting 2 12-24-2007 02:36 PM
sqrt is not find??? murataht High Level Programming 2 10-17-2004 07:04 AM
create thread C with JNI function with JAVA AUBERT HP-UX 0 08-06-2004 02:24 AM
sqrt CreamHarry High Level Programming 1 06-03-2002 04:56 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 12-09-2007
Registered User
 

Join Date: Nov 2007
Posts: 131
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
Reply With Quote
Forum Sponsor
  #2  
Old 12-09-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
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
Reply With Quote
  #3  
Old 12-09-2007
Registered User
 

Join Date: Nov 2007
Posts: 131
Hi ,

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


Please advise. Thanks.


-Jason
Reply With Quote
  #4  
Old 12-09-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
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'
Reply With Quote
  #5  
Old 12-09-2007
Registered User
 

Join Date: Nov 2007
Posts: 131
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
Reply With Quote
  #6  
Old 12-10-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
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 12:18 AM.
Reply With Quote
  #7  
Old 12-10-2007
Registered User
 

Join Date: Nov 2007
Posts: 131
Hi

Where the 'constant' (h) gets assigned it's value?
The h is actually counting the number of folders from other variables.

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?
Yeah. I already tried on sqrt(t); which is without bc -l.


You are right. I am using associative array count for the purpose to read each line.

However, when I get the result from the operation, it doesnt return me the correct output from these operation.Did I missed out any "$" in the below fragment of code? It gives me INF and NAN value

cat a.txt| awk ' {
t= h*($3+$2);
s= sqrt($t)

count($1)=$2/s
}
END{

printf("the sqrt of %d is %d\n", $2,$1);
}

Please advise.Thanks.


Rgrds,
Jason
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 07:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0