Read numbers from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read numbers from file
# 1  
Old 05-10-2013
Read numbers from file

I am trying to make a script to read marks from a file then find out how many of them are above 40 (passing marks). However my script is getting stuck at "read num". I dont understand whats the problem. Any help will be much appreciated.

Code:
#!/bin/bash
set -x
count=0; countP=0;  PASSMK=40
max=-1;  min=999999
tot=0;   avg=0
read num #read the first input number
while [ $num -ge 0 ]; do
  tot=$(($tot+$num))
  if [ $num -gt $max ]; then
    max=$num
  fi
  if [ $num -lt $min ]; then
    min=$num
  fi

  count=$(($count+1))
  if [ $num -ge $PASSMK ]; then
    countP=$(($countP+1));
  fi

  read num #read the next input number
done

if [ $count -gt 0 ]; then
  avg=$(($tot/$count))
fi
printf "entries\tpasses\tmax\tmin\tavg\n"
printf "%d\t%d\t%d\t%d\t%d\n" "$count" "$countP" "$max" "$min" "$avg"


Last edited by vish6251; 05-10-2013 at 06:59 AM..
# 2  
Old 05-10-2013
Quote:
Originally Posted by vish6251
I am trying to make a script to read marks from a file then find out how many of them are above 40 (passing marks). However my script is getting stuck at "read num". I dont understand whats the problem. Any help will be much appreciated.

Code:
#!/bin/bash
set -x
count=0; countP=0;  PASSMK=40
max=-1;  min=999999
tot=0;   avg=0

read num #read the first input number
while [ $num -ge 0 ]; do
  tot=$(($tot+$num))
  if [ $num -gt $max ]; then
    max=$num
  fi
  if [ $num -lt $min ]; then
    min=$num
  fi

  count=$(($count+1))
  if [ $num -ge $PASSMK ]; then
    countP=$(($countP+1));
  fi

  read num #read the next input number
done

if [ $count -gt 0 ]; then
  avg=$(($tot/$count))
fi
printf "entries\tpasses\tmax\tmin\tavg\n"
printf "%d\t%d\t%d\t%d\t%d\n" "$count" "$countP" "$max" "$min" "$avg"

You said you want the read commands to read from a file, but you haven't specified any file. Therefore, both read commands will read from the standard input for your bash script. In other words, the script is waiting for you to type in values to be processed.

If you want to choose a different file each time you run the script, you can redirect the input of your script when you run it, as in:
Code:
script_name < input_file

If you always want the read command in your script to read from the same file every time you run the script, change the line marked in red in your script above with:
Code:
done < input_file

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-10-2013
Quote:
Originally Posted by Don Cragun
You said you want the read commands to read from a file, but you haven't specified any file. Therefore, both read commands will read from the standard input for your bash script. In other words, the script is waiting for you to type in values to be processed.

If you want to choose a different file each time you run the script, you can redirect the input of your script when you run it, as in:
Code:
script_name < input_file

If you always want the read command in your script to read from the same file every time you run the script, change the line marked in red in your script above with:
Code:
done < input_file

I knew i had to specify a file, then i tried adding "num=$1" that didn't work. But your suggestion worked perfectly! thank you!
# 4  
Old 05-10-2013
You better do
Code:
while read num #read an input number
do
 if [ $num -ge 0 ]; then
  ... #without a further read
 fi
done

Eventually done < input_file
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 05-10-2013
The previous suggestion worked for me however, now instead of a txt file with one integer per line, I want read a csv file and apply script to get the statistics of marks. How would i extract values from column 3 of csv file and feed them to the script? can i use 'awk' ?
Sample csv :
Code:
12;28;32;32
23;54;75;6
12;34;55;45
242;42;85;63
32;4;24;38
-1;-1;-1;-1


Last edited by vish6251; 05-10-2013 at 10:40 AM..
# 6  
Old 05-10-2013
I see you have a semicolon separated file. In that case specify IFS (Internal Field Separator) as semicolon and read:
Code:
while IFS=";" read f1 f2 f3 f4
do
        printf "%d\n" $f3
done < input_file

# 7  
Old 05-10-2013
Quote:
Originally Posted by Yoda
I see you have a semicolon separated file. In that case specify IFS (Internal Field Separator) as semicolon and read:
Code:
while IFS=";" read f1 f2 f3 f4
do
        printf "%d\n" $f3
done < input_file

Please check my script. How can i supply values of $f3 to $num?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Read a file with decimal numbers in bash

Hello, I have the following script while read id fraction do sambamba -h -f bam -t 10 --subsampling-seed=50 -s $frac ${id}.bam -o ${id}.out.bam done < fraction.txt where fraction.txt has two columns (id,fraction) and 50 rows I am unable to run this as bash is not able to read the second... (2 Replies)
Discussion started by: nans
2 Replies

2. Programming

Read in numbers from console won't stop at newline.

Hello, I have snippet code from Lippman's <<C++ primer>>. The program is to convert regular decimal (0 ~ 15) numbers to basic hexdecimals. The instruction tells the program will execute by hitting newline at the end. When I tried to run the compiled program, hitting ENTER did not work as... (3 Replies)
Discussion started by: yifangt
3 Replies

3. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

4. Shell Programming and Scripting

Read from file and execute the read command

Hi, I am facing issues with the below: I have a lookup file say lookup.lkp.This lookup.lkp file contains strings delimited by comma(,). Now i want to read this command from file and execute it. So my code below is : Contents in the lookup.lkp file is : c_e,m,a,`cd $BOX | ls cef_*|tail... (7 Replies)
Discussion started by: vital_parsley
7 Replies

5. Shell Programming and Scripting

Read in numbers from a datafile

Hi, I want to be able to read numbers from many files which have the same general form as follows: C3H8 4.032258004031807E-002 Phi = 1.000000E+00 Tau = 5.749E+00 sL0 = 3.805542E+01 dL0 = 1.514926E-02 Tb = 2.328291E+03 Tu = 3.450E+02 Alpha = ... (3 Replies)
Discussion started by: lost.identity
3 Replies

6. 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

7. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

8. Shell Programming and Scripting

Help with addition of 2 numbers that are read from a file

I am trying to add free and used memory (so that i can compute percentage used)of remote nodes using shell script. I use the openssh-server,expect tool and ssh script. 1)login.txt (info of nodes): ip1|username|password ip2|username|password . . . 3)sshlogin.sh #!/bin/bash ... (1 Reply)
Discussion started by: marmik1903
1 Replies

9. Programming

Cannot read a file with read(fd, buffer, buffersize) function

# include <stdio.h> # include <fcntl.h> # include <stdlib.h> # include <sys/stat.h> int main(int argc, char *argv) { int fRead, fPadded, padVal; int btRead; int BUFFSIZE = 512; char buff; if (argc != 4) { printf ("Please provide all of the... (3 Replies)
Discussion started by: naranja18she
3 Replies

10. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies
Login or Register to Ask a Question