How to create SQRT function in catenate file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create SQRT function in catenate file
# 1  
Old 12-10-2007
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  
Old 12-10-2007
Quote:
Originally Posted by ahjiefreak
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  
Old 12-10-2007
Hi ,

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


Please advise. Thanks.


-Jason
# 4  
Old 12-10-2007
Quote:
Originally Posted by ahjiefreak
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  
Old 12-10-2007
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  
Old 12-10-2007
Quote:
Originally Posted by ahjiefreak
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..
# 7  
Old 12-10-2007
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 Smilie

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

A Function To Create A 1 Second Sinewave WAVE Beep File In Python.

sinebeep.py Creating an audio WAVE file called... beep.wav ...that can be played using almost ANY audio player available. This simple DEMO snippet of code generates a 1 second sinewave WAVE file. It IS saved inside the CURRENT drawer so that you can find it... ;o) Note that the... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Shell Programming and Scripting

Create function

I would like to make a perl function , this function could send mail via SMTP and also could import a text file into mail content , that mean I have a text file , want to use this function to send mail via SMTP , the mail content is the text file , would advise how to write this script ? thanks (1 Reply)
Discussion started by: ust3
1 Replies

3. Shell Programming and Scripting

How to create .txt file by using function?

My Calling script is like below: for file in `echo $LIST_OF_FILES` --listing filenames eg, xyz_meta_20110501_00000789.tar do file_name=`basename $file` <call a function to create .txt file in below format> done Want to generate a .txt file that contains below data in ksh: ... (3 Replies)
Discussion started by: vedanta
3 Replies

4. UNIX for Dummies Questions & Answers

sqrt in bash

Hi, i have a the following script: #!/bin/bash a=3 b=9 let "c= b*a" let "d=sqrt $c " echo $d But when i execute the code, it gives me the an error saying: line 5: let: d=sqrt 27 : syntax error in expression (error token is "27 ") Can any body tell me what I'm doing wrong? (5 Replies)
Discussion started by: limadario
5 Replies

5. Shell Programming and Scripting

how can i find sqrt of a any number without using expr

hi friends can any body tell me how can i find sqrt of a any given number without using expr in bash shell while i am doing i got some errors please take a look and code is here x=$((( ( sqrt($1) ) | bc ))) echo $x $ sh quadratic-eqn-roots.sh 9 quadratic-eqn-roots.sh: line 12: ( (... (6 Replies)
Discussion started by: srinivas2828
6 Replies

6. UNIX for Dummies Questions & Answers

How to create alias for a function

Hi, In Unix (AIX/LINUX) how do we create a alias for a UNIX shell function? Please help me with that. Thanks!! (2 Replies)
Discussion started by: neeto
2 Replies

7. Programming

create a thread from a returning function

hi all, my requirement is to create a thread by calling another function. i.e i dont call pthread_create directly from main, but by calling another function (createThd - below ), from main. Example: void *thread_function(void *arg) { /* thread function */ int i; rc =... (3 Replies)
Discussion started by: wolwy_pete
3 Replies

8. Shell Programming and Scripting

create function with awk

hi all, i print four variable delimited by comma with awk : awk -F "," '{print $1;$2;$3;$4}' ' if $3="" code ... } i want to extract this information from another file using another awk using the $1 printed awk -v { code } but the problem i can't use two awk in the same code... (2 Replies)
Discussion started by: kamel.seg
2 Replies

9. Programming

sqrt is not find???

I was writing a simple program in linux, which includes sqrt function of c. I included the math.h. But when I use gcc to compile it, it gave an error message: /home/murat/tmp/ccOv9upo.o(.text+0x4b): In function `main': : undefined reference to `sqrt' collect2: ld returned 1 exit status I... (2 Replies)
Discussion started by: murataht
2 Replies

10. Programming

sqrt

Hi! when i'm trying to compile this lite example on my linux machine I'll get errors and i don't know why.. #include <stdio.h> #include <math.h> /* needed by sqrt() */ int main() { printf("%f", sqrt(10.0)); return (0); } this is the error: /tmp/cc33hNVHK.o: In function... (1 Reply)
Discussion started by: CreamHarry
1 Replies
Login or Register to Ask a Question