How to read n number of lines from a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to read n number of lines from a file
# 29  
Old 11-04-2009
Quote:
Originally Posted by varsha
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!!!
Code:
nawk will only work on Solaries machines GAWK is the standard (GUNU awk) and better one which will work on Linux systems.

BR

# 30  
Old 11-09-2009
Help!!

Hello all,

I have a script which calculate avg for columnwise I attaced the script below

script
Code:
echo "enter file name"
read file
cat $file
awk 'BEGIN {FS=OFS=" "} { for (i=1; i<=NF; i++) sum[i] += $i; j=NF }
END { printf "%s%s", "------------------", "\nAvg"; for (i=1; i <= j; i++) printf "%s%s", OFS, sum[i]/NR; printf "\n"; }' $file

output
Code:
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
Code:
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

Code:
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 meSmilieSmilieSmilie


Thanks!!!!SmilieSmilie

Last edited by Franklin52; 02-16-2010 at 07:03 AM.. Reason: Please use code tags!
# 31  
Old 11-09-2009
code:-

Code:
gawk '{for (i=1;i<=NF;i++){a[i]+=$i;b[i]++ ; 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

SmilieSmilieSmilieSmilie
# 32  
Old 11-09-2009
Help!!

Instead of giving output like this

Code:
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)
Code:
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 meSmilieSmilieSmilieSmilie


Thanks again!!SmilieSmilieSmilie

Last edited by Franklin52; 02-16-2010 at 07:04 AM.. Reason: Please use code tags!
# 33  
Old 11-09-2009
modification in below:-

Code:
gawk '{for (i=1;i<=NF;i++){a[i]+=$i;b[i]++ ; 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 ----------

Quote:
Originally Posted by ahmad.diab
modification in below:-

Code:
gawk '{for (i=1;i<=NF;i++){a[i]+=$i;b[i]++ ; 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

Code:
note:- if the averages are not in order use /usr/xpg4/bin/awk instead of gawk

SmilieSmilieSmilieSmilieSmilieSmilieSmilie
# 34  
Old 11-10-2009
Help!!

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 meSmilieSmilieSmilieSmilie





ThanksSmilieSmilieSmilieSmilieSmilieSmilie
# 35  
Old 11-10-2009
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...

Last edited by bittoo; 11-10-2009 at 03:03 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read a file starting at certain line number?

I am new to ksh scripts. I would like to be able to read a file line by line from a certain line number. I have a specific line number saved in a variable, say $lineNumber. How can I start reading the file from the line number saved in $lineNumber? Thanks! (4 Replies)
Discussion started by: dcowboys13
4 Replies

2. Shell Programming and Scripting

How to read a number from a file?

hello guys, I'm struggled to get a number from a very long text file. NAtoms= 33 NActive= 30 NUniq= 23 SFac= 1.00D+00 NAtFMM= 60 NAOKFM=F Big=F Integral buffers will be 131072 words long. Raffenetti 2 integral format. The number 33 is what I wanted, always follows NAtoms=... (5 Replies)
Discussion started by: liuzhencc
5 Replies

3. Shell Programming and Scripting

Read line with particular number of lines

Hi all, I have a file sample.txt abc asd adf daf adw add adv wdf I want to control the number of lines to read Like if i give input as ./script_name 2 5 required output asd adf daf (2 Replies)
Discussion started by: krux_rap
2 Replies

4. UNIX for Dummies Questions & Answers

When reading a csv file, counter to read 20 lines and wait for minute then read next 20 till end

Hello All, i am a newbie and need some help when reading a csv file in a bourne shell script. I want to read 10 lines, then wait for a minute and then do a reading of another 10 lines and so on in the same way. I want to do this till the end of file. Any inputs are appreciated ... (3 Replies)
Discussion started by: victor.s
3 Replies

5. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

6. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

7. Shell Programming and Scripting

Number lines of file and assign variable to each number

I have a file with a list of config files numbered on the lefthand side 1-300. I need to have bash read each lines number and assign it to a variable so it can be chosen by the user called by the script later. Ex. 1 some data 2 something else 3 more stuff which number do you... (1 Reply)
Discussion started by: glev2005
1 Replies

8. Shell Programming and Scripting

Read the specified line number from file

Hi Guys, I am new to unix. Actually i want help in writing an single command where i can actually read specific line number in file where the line number will be passed to command as parameter. ex. 1 a 2 b 3 c 4 d And to my command i pass as 2. so i should get output as 2 b ... (15 Replies)
Discussion started by: kam786sim
15 Replies

9. Shell Programming and Scripting

Read a number from file and place it back

Hi All, I want to read one number from the file. Only one number will be there in the file. then i have to increment the number in my script and put it back in the same file. Is it possible? Can anybody help me? Thanks, Vinay (6 Replies)
Discussion started by: vinayakatj56
6 Replies

10. Shell Programming and Scripting

To read and separate number and words in file and store to two new file using shell

hi, I am a begginer in unix and i want to know how to open a file and read it and separate the numbers & words and storing it in separate files, Using shell scripting. Please help me out for this. Regards S.Kamakshi (2 Replies)
Discussion started by: kamakshi s
2 Replies
Login or Register to Ask a Question