|
Home > Archive > Unix Shell > December 2007 > Arithmatic operation
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Arithmatic operation
|
|
| Nikunj 2007-12-03, 1:28 pm |
| hello ,
i have a file called abc.txt , contain of this file is
# more abc.txt
1
2
34
45
65
.......
Now i want to take one value and perform arithmatic operation for that
i have created following script but its not working , i'm using ksh
#script1
FIX=1024
for i in `cat abc.txt`
do
length1=`expr $i / 2`
length2=`expr $length1 / $FIX`
length3=`expr $length2 / $FIX`
done >> op
would be appricate to help me out to resolve the problem also suggest
me the short and sweet way of this script ,if any one having idea.
i am converting values in GB
thanks
Nikunj
| |
| Ed Morton 2007-12-03, 1:28 pm |
|
On 12/3/2007 10:54 AM, Nikunj wrote:
> hello ,
>
> i have a file called abc.txt , contain of this file is
> # more abc.txt
> 1
> 2
> 34
> 45
> 65
> ......
> Now i want to take one value and perform arithmatic operation for that
> i have created following script but its not working , i'm using ksh
>
> #script1
> FIX=1024
> for i in `cat abc.txt`
> do
> length1=`expr $i / 2`
> length2=`expr $length1 / $FIX`
> length3=`expr $length2 / $FIX`
> done >> op
>
> would be appricate to help me out to resolve the problem also suggest
> me the short and sweet way of this script ,if any one having idea.
> i am converting values in GB
>
> thanks
>
> Nikunj
You don't show what you want the output to be. If it's just the final result of
the arithmetic ops, then all you need is:
awk 'BEGIN{fix=1024;div=2*fix*fix} {print $0 / div}' abc.txt > op
Regards,
Ed.
| |
| Nikunj 2007-12-03, 1:28 pm |
| On Dec 3, 11:10 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 12/3/2007 10:54 AM, Nikunj wrote:
>
>
>
>
>
>
>
>
>
>
>
> You don't show what you want the output to be. If it's just the final result of
> the arithmetic ops, then all you need is:
>
> awk 'BEGIN{fix=1024;div=2*fix*fix} {print $0 / div}' abc.txt > op
>
> Regards,
>
> Ed.- Hide quoted text -
>
> - Show quoted text -
great ....!!!
its very easy way just got it in one line ..
excellent solution ..i have done with the script
thanks ......
Nikunj
| |
| Bill Marcum 2007-12-03, 1:28 pm |
| On 2007-12-03, Nikunj <niku.khakhar@gmail.com> wrote:
>
>
> hello ,
>
> i have a file called abc.txt , contain of this file is
> # more abc.txt
> 1
> 2
> 34
> 45
> 65
> ......
> Now i want to take one value and perform arithmatic operation for that
> i have created following script but its not working , i'm using ksh
>
> #script1
> FIX=1024
> for i in `cat abc.txt`
> do
> length1=`expr $i / 2`
> length2=`expr $length1 / $FIX`
> length3=`expr $length2 / $FIX`
> done >> op
>
You are just setting variables, not outputting them. Also, instead of
expr you could use the shell's built-in arithmetic:
length=$((i/2))
echo $length
If you want non-integer results, ksh93 and zsh have floating point
arithmetic, or you can use bc or awk.
| |
| Nikunj 2007-12-03, 1:28 pm |
| On Dec 3, 11:10 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 12/3/2007 10:54 AM, Nikunj wrote:
>
>
>
>
>
>
>
>
>
>
>
> You don't show what you want the output to be. If it's just the final result of
> the arithmetic ops, then all you need is:
>
> awk 'BEGIN{fix=1024;div=2*fix*fix} {print $0 / div}' abc.txt > op
>
> Regards,
>
> Ed.- Hide quoted text -
>
> - Show quoted text -
hey ed,
i got the output in format "15.9911"
i want only 2 dec.point or want in integrer format how can i do ?
| |
|
| Nikunj wrote:
> hello ,
>
> i have a file called abc.txt , contain of this file is
> # more abc.txt
> 1
> 2
> 34
> 45
> 65
> ......
> Now i want to take one value and perform arithmatic operation for that
> i have created following script but its not working , i'm using ksh
>
> #script1
> FIX=1024
> for i in `cat abc.txt`
> do
> length1=`expr $i / 2`
> length2=`expr $length1 / $FIX`
> length3=`expr $length2 / $FIX`
> done >> op
>
> would be appricate to help me out to resolve the problem also suggest
> me the short and sweet way of this script ,if any one having idea.
> i am converting values in GB
>
> thanks
>
> Nikunj
The ohnly thing wrong is that you never echo the result. Just add
echo $length3
before the "done" line.
-Wayne
| |
| Ed Morton 2007-12-03, 1:28 pm |
|
On 12/3/2007 11:57 AM, Nikunj wrote:
> On Dec 3, 11:10 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> hey ed,
>
> i got the output in format "15.9911"
> i want only 2 dec.point or want in integrer format how can i do ?
Something like:
printf "%d\n",$0 / div
printf takes all the normal formatting options you'd expect in C (if that helps).
Ed.
| |
| Nikunj 2007-12-03, 1:28 pm |
| On Dec 3, 1:07 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 12/3/2007 11:57 AM, Nikunj wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> Something like:
>
> printf "%d\n",$0 / div
>
> printf takes all the normal formatting options you'd expect in C (if that helps).
>
> Ed.- Hide quoted text -
>
> - Show quoted text -
thanks for respond...
where i put it ?
before awk statement or after awk /
Nikunj
| |
| Ed Morton 2007-12-03, 7:25 pm |
|
On 12/3/2007 1:26 PM, Nikunj wrote:
> On Dec 3, 1:07 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
> thanks for respond...
>
> where i put it ?
>
> before awk statement or after awk /
awk 'BEGIN{fix=1024;div=2*fix*fix} {printf "%d\n",$0 / div}' abc.txt > op
| |
| Nikunj 2007-12-03, 7:25 pm |
| On Dec 3, 1:29 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 12/3/2007 1:26 PM, Nikunj wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> awk 'BEGIN{fix=1024;div=2*fix*fix} {printf "%d\n",$0 / div}' abc.txt > op- Hide quoted text -
>
> - Show quoted text -
thanks for your prompt response , i've done with it...!!!
reg,
nik
| |
| Nikunj 2007-12-04, 1:25 pm |
| On Dec 3, 1:42 pm, Nikunj <niku.khak...@gmail.com> wrote:
> On Dec 3, 1:29 pm, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> thanks for your prompt response , i've done with it...!!!
>
> reg,
> nik- Hide quoted text -
>
> - Show quoted text -
hello ,
thanks all for response
hello i got the output in following format i got dg name twice for
example dg_te1's having 2 output 11 &39 . i want to make it as one
mean i want it dg_te1 50 mean (11+39) . can i do it ...
your help would be appriciated.
dg_te1_Data 14
dg_te1_Data 0
dg_te1 15
dg_te1 11
dg_te1 39
dg_te1 39
Thanks
niks
| |
| Ed Morton 2007-12-04, 1:25 pm |
|
On 12/4/2007 11:27 AM, Nikunj wrote:
> On Dec 3, 1:42 pm, Nikunj <niku.khak...@gmail.com> wrote:
>
>
>
> hello ,
>
> thanks all for response
>
>
> hello i got the output in following format i got dg name twice for
> example dg_te1's having 2 output 11 &39 . i want to make it as one
> mean i want it dg_te1 50 mean (11+39) . can i do it ...
> your help would be appriciated.
>
>
> dg_te1_Data 14
>
> dg_te1_Data 0
>
> dg_te1 15
>
> dg_te1 11
>
> dg_te1 39
>
> dg_te1 39
>
> Thanks
> niks
The output you posted doesn't match the input you posted or the script I gave
you and it sounds like your requirements have changed. Start again. Tell us what
you want to do, post sample input, expected output, the script you ran, the
output it gives, and what's wrong with that output.
Ed.
| |
| Nikunj 2007-12-04, 7:25 pm |
| On Dec 4, 11:33 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 12/4/2007 11:27 AM, Nikunj wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
> The output you posted doesn't match the input you posted or the script I gave
> you and it sounds like your requirements have changed. Start again. Tell us what
> you want to do, post sample input, expected output, the script you ran, the
> output it gives, and what's wrong with that output.
>
> Ed.- Hide quoted text -
>
> - Show quoted text -
ed,
it gives me list me twice output of the first columnfor example
dg_te1 is twice i want it in single and want to add the both
values which generated by the dg_te1 here in case its 11+15 .. .so it
become ease of my work....thanks
| |
| Ed Morton 2007-12-04, 7:25 pm |
|
On 12/4/2007 2:24 PM, Nikunj wrote:
> On Dec 4, 11:33 am, Ed Morton <mor...@lsupcaemnt.com> wrote:
>
<snip>[vbcol=seagreen]
>
>
> ed,
>
> it gives me list me twice output of the first columnfor example
> dg_te1 is twice i want it in single and want to add the both
> values which generated by the dg_te1 here in case its 11+15 .. .so it
> become ease of my work....thanks
Here's the input file you posted:
> i have a file called abc.txt , contain of this file is
> # more abc.txt
> 1
> 2
> 34
> 45
> 65
> ......
where all this dg_tel stuff is coming from is a mystery. Seriously - do what I
suggested about starting over.
Ed.
| |
| Bill Marcum 2007-12-04, 7:25 pm |
| On 2007-12-04, Nikunj <niku.khakhar@gmail.com> wrote:
>
>
>
> it gives me list me twice output of the first columnfor example
> dg_te1 is twice i want it in single and want to add the both
> values which generated by the dg_te1 here in case its 11+15 .. .so it
> become ease of my work....thanks
awk '{sum[$1]+=$2} END{for(x in sum) print x,xum[x]}'
| |
| Ed Morton 2007-12-04, 7:25 pm |
|
On 12/4/2007 2:51 PM, Bill Marcum wrote:
> On 2007-12-04, Nikunj <niku.khakhar@gmail.com> wrote:
>
>
>
> awk '{sum[$1]+=$2} END{for(x in sum) print x,xum[x]}'
That won't do it. In the part you snipped, he said this:
> hello i got the output in following format i got dg name twice for
> example dg_te1's having 2 output 11 &39 . i want to make it as one
> mean i want it dg_te1 50 mean (11+39) . can i do it ...
> your help would be appriciated.
>
>
> dg_te1_Data 14
>
> dg_te1_Data 0
>
> dg_te1 15
>
> dg_te1 11
>
> dg_te1 39
>
> dg_te1 39
so assuming he has an input file with a format similair to that, then your
script would produce a total of 104 for the "dg_tel" records whereas the OP says
it should be 50. There's something he's not telling us....
Ed.
|
|
|
|
|