varsha
October 26, 2009, 1:28am
21
hi o:o
im very much new for shell scripting
plzzzzzzzz anyone help me:confused:
i have a file content
1 2 3 4 5 6
3 5 6
7 9 3 2 4 4 6 6
.......
.......
.....
for this i need the output like
1 2 3 4 5 6 average=3.5
3 5 6 average=4.6666
7 9 3 2 4 4 6 6 average=5.125
i think i should use "awk -f"
but im stucked hw to use for this
plzzzzzzzz help me
Thanks in advance:b:
varsha
November 3, 2009, 3:30am
22
hi everybody
i have one shell scripting which calculate average value for columns
this is the script
#!/bin/bash
echo "enter file name"
read file
cat $file
awk 'BEGIN {FS=OFS=" "} { for (i=1; i<=NF; i++) sum += $i; j=NF }
END { printf "%s%s", "------------------", "\nAvg"; for (i=1; i <= j; i++) printf "%s%s", OFS, sum/NR; printf "\n"; }' $file
it gives the output like this
enter file name
vfile
1 5 3 4 6 8
5 5 7 9 0 1
4 6 4 4 6 2
6 7 8 12 3 4
------------------
Avg 4 5.75 5.5 7.25 3.75 3.75
but i want to do calculaton for average in row wise so that my output will look like this
enter file name
vfile
1 5 3 4 6 8 = 4.5
5 5 7 9 0 1 = 4.5
4 6 4 4 6 2 = 4.333333333
6 7 8 12 3 4 = 6.666666667
Any help is thankfully appericiated
Try this...
awk '{print $0,"=",($1+$2+$3+$4+$5+$6)/6}' file
varsha
November 3, 2009, 5:41am
24
tanx!!! it worked
but one thing i can have either more than 6columns or less than that also same thing for no of rows also
for eg my output should be like this
enter file name
vfile
1 5 3 = 3
5 5 7 9 0 1 = 4.5
4 6 4 4 6 = 4.8
6 7 8 12 3 4 55 = 13.571428571
once again tanx in advance!!
code:-
nawk 'NF{for (i=1;i<=NF;i++) {x+=$i} ; print $0," =",x/NF ; x=0}' input_file
BR
varsha
November 3, 2009, 10:52pm
26
hi ahmad.diab,
tanx for ur code but the code throws me error saying that
nawk: command not found
help me plzzzz
thanks again in advance!!!!
use gawk or /usr/xpg4/bin/awk instead of nawk.
BR
varsha
November 4, 2009, 2:51am
28
Hi ahmad.diab,
I replaced gawk instead nawk. Thanks , it works well. But can u plz explain me why nawk not worked , i need the whole script explanation.????
Tanx once again!!!
nawk will only work on Solaries machines GAWK is the standard (GUNU awk) and better one which will work on Linux systems.
BR
varsha
November 9, 2009, 12:35am
30
Hello all,
I have a script which calculate avg for columnwise I attaced the script below
script
echo "enter file name"
read file
cat $file
awk 'BEGIN {FS=OFS=" "} { for (i=1; i<=NF; i++) sum += $i; j=NF }
END { printf "%s%s", "------------------", "\nAvg"; for (i=1; i <= j; i++) printf "%s%s", OFS, sum/NR; printf "\n"; }' $file
output
enter file name
vfile
1 5 3 4 6 8
5 5 7 9 0 1
4 6 4 4 6 2
6 7 8 12 3 4
------------------
Avg 4 5.75 5.5 7.25 3.75 3.75
but when there is any disordered no of row for eg 1st col may have 5rows 2nd column have 4rows means it is calculating only 1st col
sample output
enter file name
vfile
1 5 3 4 6 8 9 9
5 5 7 9 0 1 1
4 6 4 4 6 2
6 7 8 12 3 4
7 9
------------------
Avg 4.6 6.4
but i want the output like more over in the above o/p instead of 6.2 its printing 6.4
enter file name
vfile
1 5 3 4 6 8 9 9
5 5 7 9 0 1 1
4 6 4 4 6 2
6 7 8 12 3 4
7 9
------------------
Avg 4.6 5.75 6.2 7.25 3.75 3.75 5 9
I belive my promblem would have reached u plzzzzzzzz kindly help me:confused:
Thanks!!!!
code:-
gawk '{for (i=1;i<=NF;i++){a+=$i;b++ ; if(nf < NF) {nf=NF} } }
END{printf "Avrg " ; for (i in a) {printf "%s %s",a[nf+1-i]/b[nf+1-i],( i < nf ) ? " " : "\n" }}
' file.txt
:D:D:D:D
varsha
November 9, 2009, 5:41am
32
Instead of giving output like this
enter file name
vfile
1 5 3 4 6 8
5 5 7 9 0 1
4 6 4 4 6 2
6 7 8 12 3 4
------------------
Avg 4 5.75 5.5 7.25 3.75 3.75
its giving some other result(output)
enter file name
vfile
1 5 3 4 6 8
5 5 7 9 0 1
4 6 4 4 6 2
6 7 8 12 3 4
Avrg 5.5 5.75 4
3.75 3.75 7.25
Plz hepl me:confused:
Thanks again!!
modification in below:-
gawk '{for (i=1;i<=NF;i++){a+=$i;b++ ; if(nf <= NF) {nf=NF} } }
END{printf "Avrg " ; for (i in a) {printf "%s %s",a[nf+1-i]/b[nf+1-i],( i <= nf ) ? " " : "\n" }}
' file.txt
---------- Post updated at 12:24 PM ---------- Previous update was at 11:20 AM ----------
note:- if the averages are not in order use /usr/xpg4/bin/awk instead of gawk
:D:D:D:D:D:D:D
varsha
November 10, 2009, 12:00am
34
bittoo
November 10, 2009, 1:54am
35
Ignore this ..Supposed to be in another thread
---------- Post updated at 12:24 PM ---------- Previous update was at 12:19 PM ----------
Hi Try this:
Added if ( j != count-1 ) to get next line and works good....
awk -v count=$n '{k = $1; getline; for (j=1; j<count; j++) {if ( $1 > k ) k = $1
; if ( j != count-1 ) getline;} print k}' $file...
Sorry i didnot see that it is solved...
if you don't have /usr/xpg4/bin/awk modify my code to the below and use gawk
gawk '{for (i=1;i<=NF;i++){a+=$i;b++ ; if(nf <= NF) {nf=NF} } }
END{printf "Avrg " ; for (i=1;i<=nf;i++) {printf "%s %s",a/b,( i <= nf ) ? " " : "\n" } print "" }
' file.txt
:D:D:D
varsha
November 25, 2009, 12:37am
37
hello everybody,
I have problem in my script , it is for finding avg in column wise it is working fine wen all the columns have equal no of rows but it is not working when there disordered no of rows in columns
for eg :
3 4 5 7 8
4 7 2 5
5 6 8 6
6 7
-------------
Avg:4.5 6
but my ans should be like this given below:
3 4 5 7 8
4 7 2 5
5 6 8 6
6 7
---------------
Avg:4.5 6 5 6 7
my script is given below:
#!/bin/bash
echo "enter file name"
read file
cat $file
awk 'BEGIN {FS=OFS=" "} { for (i=1; i<=NF; i++) sum += $i; j=NF }
END { printf "%s%s", "------------------", "\nAvg"; for (i=1; i <= j; i++) printf "%s%s", OFS, sum/NR; printf "\n"; }' $file
I belive my qn have mentione clearly. plz anybody help me:confused:
Thanks in advance:b:
varsha
December 9, 2009, 1:10am
38
gawk '{for (i=1;i<=NF;i++){a+=$i;b++ ; if(nf <= NF) {nf=NF} } }
END{printf "Avrg " ; for (i=1;i<=nf;i++) {printf "%s %s",a/b,( i <= nf ) ? " " : "\n" }
print "" }
' file.txt