How to read n number of lines from a file

hi :o:o:o

im very much new for shell scripting

plzzzzzzzz anyone help me:confused::confused::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::b::b:

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

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

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

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

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::confused::confused:

Thanks!!!!:b::b:

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

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::confused::confused::confused:

Thanks again!!:b::b::b:

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

hello.....

  In result the averages are not in order so instead of gawk i used /usr/xpg4/bin/awk but it throws me error showing  No such file or directory

Help me:confused::confused::confused::confused:

Thanks:):):):b::b::b:

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

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::confused::confused::confused:

Thanks in advance:b::b::b:

Till today there is no help foe me:(:(:frowning:

Anyone help me plz:confused::confused::confused:

Thanks:b::b::b:

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

Heyyyyyyy it worked:D:D

Thanks a lot friend:b::b: