How to read n number of lines from a file

Hiii

I am very new to shell scripting.This is my data file
a.txt:
56
45.78
1000
11.23
76.89
45
34.56
23
3400
100
..........
Now i am must use shell scripting to read n number of lines from the file & from ts n number of lines i need to find greatest number among them & so on for rest of the file still end of the file...
For example from the above data file
I want to check for first 3(this must be user defined) lines & so on for largest number
The ouput for such a condition must be
for first set(i.e first 3 lines)
output is 1000
then 2nd set
output is 76.89
the 3rd set
output is 3400
& so on till end of my file..
.......
Help is greatly appreciated:confused::confused:
Thanks

If you have a specific problem/question you can ask for help but we can't do your work for you.

Can you show us what have you tried so far and where you are stuck?

#!/bin/bash
echo "Enter file name"
read file
cat $file
echo "Enter the number"
read n
head $n  $file | sort -n -r >> file2
echo "biggest no is " 
head -1 file2

my output is

Enter file name
myfile
260
10
10
250
100
1
1
1
1
2
1
3
4
5
8
2.7
44
4
3
4
4
5
5
7
100
10
03
25
200
160
300
6000          
Enter the number
-10

biggest no is
260

it takes only first 10 number from myfile for finding biggest number among them , and it stops , for the next 10 numbers its not taking , i want such a process till the end of the line from the file

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

Thanks in advance:):slight_smile:

#!/bin/bash
echo "Enter file name"
read file
echo "Enter the number"
read n
awk -v count=$n '{k = $1; getline; for (j=1; j<cunt; j++) {if ( $1 > k ) k = $1; getline; } print k}' $file

Thanks for ur help

Will u b plzzzzzzzzz explain me the program how it works

give me in detail explanation bcoz im very much new for shell scripting:confused::frowning:

Once again Tanx in advance

#!/bin/bash

Assuming that the file this code is in has the executable attribute, the system will use /bin/bash to execute it.

echo "Enter file name"
read file

Reads the file name into variable 'file'

echo "Enter the number"
read n

Reads the number of lines over which the awk script will find the max.

awk -v count=$n '{k = $1; getline; for (j=1; j<cunt; j++) {if ( $1 > k ) k = $1; getline; } print k}' $file

This is the awk command to do the work.

-v count=$n

Sets the awk variable count to the number of lines to use for the comparison.

'{k = $1; getline; for (j=1; j<cunt; j++) {if ( $1 > k ) k = $1; getline; } print k}'

This is the awk program. It says:

For everyline
set k to the first value of the current input line

---------- Post updated at 10:58 PM ---------- Previous update was at 10:48 PM ----------

#!/bin/bash

Assuming that the file this code is in has the executable attribute, the system will use /bin/bash to execute it.

echo "Enter file name"
read file

Reads the file name into variable 'file'

echo "Enter the number"
read n

Reads the number of lines over which the awk script will find the max.

awk -v count=$n '{k = $1; getline; for (j=1; j<count; j++) {if ( $1 > k ) k = $1; getline; } print k}' $file

This is the awk command to do the work.

-v count=$n

Sets the awk variable count to the number of lines to use for the comparison.

'{k = $1; getline; for (j=1; j<count; j++) {if ( $1 > k ) k = $1; getline; } print k}'

This is the awk program. It says:

For every line
  set k to the first value of the current input line
  Get the next input line
  For j starting at 1, until j = count do the following
    If the new value is greater than k, set k to the new higher value
    get the next line
  End For j loop
  Print the high value for the set of lines
End For every line loop
$file

This is the name of the input file.

HI once again

Tanx for ur code

but still im unable to recover from my problem

c i have a file (name-myfile) it contains the data give below

6
3
0.33
6
5
100
2
12.3
6
12
54
1000
5
16
16
67
67

now i want to compare 3numbers

so my output should be like this given below

output

enter file name
myfile

enter the number
3

count1
biggest no is 6

count2
biggest no is 100

count3
biggest no is 12.3

count4
biggest no is 1000

count5
biggest no is 16

count6
biggest no is 67


final count is 6

but the code given by you i tried it gives output like this given below

enter file name
myfile

enter the number
3
6
0.33
5
2
6
54
5

PLZZZZZZZZZZZ help me:confused::confused::confused:

Tanx in advance

#!/bin/bash
echo "Enter file name"
read file
echo "Enter the number"
read n
awk -v count=$n 'BEGIN{ print "count " count; } {k = $1; for (j=1; j<count; j++) {getline; if ( $1 > k ) k = $1; } print k}' $file

Hi

Tanx , tanx a lot.....

it works!!!!!:):):):slight_smile:

---------- Post updated at 02:13 AM ---------- Previous update was at 02:06 AM ----------

but still instead of displaying the output like tis given below

Enter file name
myfile
Enter the number
3
count 3
6
100
12.3
1000
16

i want the output like given below

Enter file name
myfile
Enter the number
3

count1
biggest no is 6

count2
biggest no is 100

count3 
biggest no is 12.3

count4
biggest no is 1000

count5
biggest no is 16

total no of count is 5


plzzzzz help???????

Tanx again!!!

Try this:

#!/bin/bash
echo "Enter file name"
read file
echo "Enter the number"
read n
awk -v count=$n 'BEGIN{ i=0; } {k = $1; for (j=1; j<count; j++) {getline; if ( $1 > k ) k = $1; } print "count" i; i++; print "biggest no is " k} END {print "total count is " i}' $file

You can add any formatting stuff you need to get the exact formatting you want...

Hi,

Try this...

#!/bin/sh

read -p "Enter your filename: " filename
read -p "Enter count: " cnt

line_cnt=`cat $filename | wc -l`
loops=`echo $line_cnt / $cnt | bc`
loop_add=`echo $line_cnt % $cnt | bc`

[ $loop_add -gt 0 ] && loops=`expr $loops + 1` || loops=$loops

for (( i=1 ; i < $loops ; i++ ))
do
        high_num=`tail -$line_cnt $filename | head -$cnt | sort -nr | head -1`
        line_cnt=`expr $line_cnt - $cnt`
        echo "count $i HIGHEST NO :- $high_num"
done

HI jp2542a!!

it works perfectlyyyyy

tanks a lot!!!!

:):):):):):):slight_smile:

---------- Post updated at 03:08 AM ---------- Previous update was at 03:05 AM ----------

HI pravin27!!

tanx for ur code

but one thing,wen i give 4 for finding biggest no its not taking the last two number

i alread posted the content of myfile

Tanx a lot

Just need to change the below line

for (( i=1 ; i <= $loops ; i++ ))

Hi jp2542a

ur code works perfectly,but the thing it takes the count as from 0 but i want is it should take count as from 1

Enter file name
file3
Enter the number
4
count0
biggest no is 6
count1
biggest no is 100
count2
biggest no is 1000
count3
biggest no is 16
total count is 4

this the output

but what i need is

Enter file name
file3
Enter the number
4
count1
biggest no is 6
count2
biggest no is 100
count3
biggest no is 1000
count4
biggest no is 16
total count is 5

Help me plzzzzzzzzzzz:confused::confused:

---------- Post updated at 02:42 AM ---------- Previous update was at 02:38 AM ----------

Hi pravin27,

Tanx for ur help it works i need to print the total number of count also

Enter your filename: file3
Enter count: 4
count 1 HIGHEST NO :- 6
count 2 HIGHEST NO :- 100
count 3 HIGHEST NO :- 1000
count 4 HIGHEST NO :- 16

this is the output of the code give by u

but i also need total no of count like given below

Enter your filename: file3
Enter count: 4
count 1 HIGHEST NO :- 6
count 2 HIGHEST NO :- 100
count 3 HIGHEST NO :- 1000
count 4 HIGHEST NO :- 16
total number of count = 4

Help is appreciated thankfully!!!

Try this:

var=10
filename=file
xargs -n $var < $filename | awk '{ max=0; for(i=1;i<=NF;i++) if ($i > max) max=$i; print max; }'

Use the same logic in loop to handle multiple files.

Hi,
Try this ,

#!/bin/sh

read -p "Enter your filename: " filename
read -p "Enter count: " cnt

line_cnt=`cat $filename | wc -l`

loops=`echo $line_cnt / $cnt | bc`
loop_add=`echo $line_cnt % $cnt | bc`
[ $loop_add -gt 0 ] && loops=`expr $loops + 1` || loops=$loops

for (( i=1 ; i <= $loops ; i++ ))
do
        high_num=`tail -$line_cnt $filename | head -$cnt | sort -nr | head -1`
        line_cnt=`expr $line_cnt - $cnt`
        echo "count $i HIGHEST NO :- $high_num"
done

echo "total number of count = $cnt and total no of loops - $loops"

Hi jp2542a

ur code works perfectly,but the thing it takes the count as from 0 but i want is it should take count as from 1

Enter file name
file3
Enter the number
4
count0
biggest no is 6
count1
biggest no is 100
count2
biggest no is 1000
count3
biggest no is 16
total count is 4

this is the output given by the program

but what i need is

Enter file name
file3
Enter the number
4

count1
biggest no is 6
count2
biggest no is 100
count3
biggest no is 1000
count4
biggest no is 16

total count is 4

Help me plzzzzzzzzzzz

And plzzzzzzzzzzzzzzzzz explain me program also

Its urgent PLzzzzzzzzzzz rpy me as soon as possible

Tanx!!!!!!

Replace the awk line with this in the shell script:

awk -v count=$n 'BEGIN{ i=0; } {k = $1; for (j=1; j<count; j++) {getline; if ( $1 > k ) k = $1; } print "count" ++i; print "biggest no is " k} END {print "total count is " i}' $file

As for what it means... This says set the awk internal variable count to the input

-v count=$n

This tells awk to initialize i to 0

BEGIN{ i=0; } 

This is the beginning of a clause that says set variable k to the value of the first field of the input line. it will be done for every line.

{k = $1; 

This bit says check each of the following count-1 lines to see if the value is greater than the current k (high) value. if it is, the it becomes the new k.

for (j=1; j<count; j++) {getline; if ( $1 > k ) k = $1; }

This prints out the biggest value (k) and ends the loop

print "biggest no is " k} 

This is executed when we run out of input and prints the counter

END {print "total count is " i}

This is the name of the input file

$file

Thanx tanx a lot, the reason im keep on asking help is bcoz of im very much new for shell scripting

Tanx!!!!!!:):):slight_smile:

hi

i have shell scripting for finding the maximum number
now i want to find both maximum and minimum number

i attach the program and output and also file content

myfile(contents)

260
10
10
250
100
1
1
1
1
2
1
3
4
5
8
2.7
44
4
3
4
4
5
5
7
100
10
03
25
200
160
300
6000

program

#!/bin/bash
echo "Enter file name"
read file
echo "Enter the number"
read n
awk -v count=$n 'BEGIN{ i=0; } {k = $1; for (j=1; j<count; j++) {getline; if ( $1 > k ) k = $1 } print "count" i; i++; print "biggest no is " k;} END {print "total count is " i}' $file > output 

output

Enter file name
myfile
Enter the number
4

vi output
count0
biggest no is 260
count1
biggest no is 100
count2
biggest no is 3
count3
biggest no is 8
count4
biggest no is 44
count5
biggest no is 7
count6
biggest no is 100
count7
biggest no is 6000
total count is 8

In the above output what i need is

output

Enter file name
myfile
Enter the number
4

vi output
count0
smallest no is 10
biggest no is 260
count1
smallest no is 1
biggest no is 100
count2
smallest no is 1
biggest no is 3
count3
smallest no is 2.7
biggest no is 8
count4
smallest no is 3
biggest no is 44
count5
smallest no is 4
biggest no is 7
count6
smallest no is 03
biggest no is 100
count7
smallest no is 160
biggest no is 6000
total count is 8

i belive my question would have been clear

plzzzzzzzzz anyone help me:confused::confused:

kindly requesting!!

Thanks!!

---------- Post updated at 11:41 AM ---------- Previous update was at 01:07 AM ----------

hi

the program i tried is

#!/bin/bash
echo "Enter file name"
read file
echo "Enter the number"
read n
awk -v count=$n 'BEGIN{ i=0; } {k = $1; m = $1; for (j=1; j<count; j++) {getline; if ( $1 > k ) k = $1; if ( $1 < m ) m = $1;} print "count" ++i; print "smallest is " m; print "biggest no is " k} END{print "total count is " i}' $file

file content is (data)

6
3
2.3
5
0.3
2.6
100
21
2000
5
3
3
8
9
10
2000
12

my output is

Enter file name
data
Enter the number
4

count1
smallest is 2.3
biggest no is 6
count2
smallest is 0.3
biggest no is 100
count3
smallest is 3
biggest no is 2000
count4
smallest is 8
biggest no is 2000
count5
smallest is 
biggest no is 12
total count is 5

while there is left only one number in last i want the output like this given below

.
.
.
.
.
.
count5
only one number is left 12
total count is 5

then while taking 5set of no's my output is like this

Enter file name
data
Enter the number
5
count1
smallest is 0.3
biggest no is 6
count2
smallest is 2.6
biggest no is 2000
count3
smallest is 3
biggest no is 10
count4
smallest is 
biggest no is 2000
total count is 4

but it should display like this as given below

.
.
.
.
.
count4
smallest is 12
biggest no is 2000
total count is 4

plzzzzzzzzzz anyone help me:confused::confused::confused:

Tanx in advance