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
Thanks
#!/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
Code:
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
Thanks in advance
Last edited by Franklin52; 02-16-2010 at 06:53 AM..
Reason: Please use code tags!
Assuming that the file this code is in has the executable attribute, the system will use /bin/bash to execute it.
Code:
echo "Enter file name"
read file
Reads the file name into variable 'file'
Code:
echo "Enter the number"
read n
Reads the number of lines over which the awk script will find the max.
Code:
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.
Code:
-v count=$n
Sets the awk variable count to the number of lines to use for the comparison.
Code:
'{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 ----------
Code:
#!/bin/bash
Assuming that the file this code is in has the executable attribute, the system will use /bin/bash to execute it.
Code:
echo "Enter file name"
read file
Reads the file name into variable 'file'
Code:
echo "Enter the number"
read n
Reads the number of lines over which the awk script will find the max.
Code:
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.
Code:
-v count=$n
Sets the awk variable count to the number of lines to use for the comparison.
Code:
'{k = $1; getline; for (j=1; j<count; j++) {if ( $1 > k ) k = $1; getline; } print k}'
This is the awk program. It says:
Code:
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
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
Code:
enter file name
myfile
enter the number
3
6
0.33
5
2
6
54
5
PLZZZZZZZZZZZ help me
Tanx in advance
Last edited by Franklin52; 02-16-2010 at 06:56 AM..
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)