shell scripting(linux)

cyber

my name defines me
The following program takes a file (here it is test1.txt) and creates a new file selected .txt which contains the names and marks of students who scored less than 90. This program works for any number of lines in the text file.To make it simple this program is executed from the same directory where test1.txt is and selected.txt is also created in the same directory
test1.txt contains,
jose 50
alan 60
john 90
shaman 80


echo "NAME MARKS" > selected.txt
x=1
for line in `cat test1.txt`
do
if [ $x -eq 1 ]
then
name=$line
elif [ $x -eq 2 ]
then
marks=$line
x=0
if [ $marks -lt 90 ]
then
echo "$name $marks" >> selected.txt
fi
fi
x=`expr $x + 1`
done

is there a better way to write the same program?
 

khmadhu

change is constant!!
is the above program worked correctly.. ?? how are u separating the name and marks. if u take a line in file both name and marks will come. u need to separate them..

try this..


Code:
echo "NAME MARKS" > selected.txt
while read line
do
echo "$line" >temp
name=`cut -d' ' -f1 temp`
score=`cut -d' ' -f2 temp`
if [$score -lt 90 ]
then
echo "$name $score" >>seleted.txt
fi
done < "test1.txt"
 
Last edited:
OP
cyber

cyber

my name defines me
in my program x is used to separate names from marks.The space between the name and marks is used as a delimiter.FOR loop reads the name first and in the second iteration it takes the marks.that's how splitting is achieved.
there is error saying that delimiter must be a single character(your program).After changing to a single character it still shows some error and can u explain me the lst line(how it works).
 

khmadhu

change is constant!!
opps .. sry extra space was there in delimiter. what error u r getting..?
I am able to run this program correctly without any error.

in the last line u r giving a file as input to the while loop, if u don't give any file as input it will take from standard input(from terminal u have to type manually)
 
Top Bottom