Home > other >  I have been trying to deal with this issue for a while now, I am a newbie in shell programming, can
I have been trying to deal with this issue for a while now, I am a newbie in shell programming, can

Time:05-24

(EDITED)

I have the following code:

#!/bin/bash
exec 3< lista.csv
read -u 3 header
nota1=0
id1=0
nota2=0
id2=0
nota3=0
id3=0
g=1
while [ "$g" -eq 1 ]
do
g=0
while IFS="," read -u 3 -r id nume prenume grupa seria nota
do
if [ "$nota" -gt "$nota1" ]
then
g=1
nota3="$nota2"
id3="$id2"
nota2="$nota1"
id2="$id1"
nota1="$nota"
id1="$id"
fi
done
done
while IFS=, read -u 3 -r id nume prenume grupa seria nota
do
    if [ "$id" -eq "$id1" ]; then
echo "Nume: $nume"
echo "Prenume: $prenume"
echo "Grupa: $grupa"
echo "Seria: $seria"
echo "Nota: $nota"
echo
    fi
done
while IFS=, read -u 3 -r id nume prenume grupa seria nota
do
    if [ "$id" -eq "$id2" ]; then
echo "Nume: $nume"
echo "Prenume: $prenume"
echo "Grupa: $grupa"
echo "Seria: $seria"
echo "Nota: $nota"
echo
    fi
done
while IFS=, read -u 3 -r id nume prenume grupa seria nota
do
    if [ "$id" -eq "$id3" ]; then
echo "Nume: $nume"
echo "Prenume: $prenume"
echo "Grupa: $grupa"
echo "Seria: $seria"
echo "Nota: $nota"
echo
    fi
done

I am supposed to find the top 3 highest values of the operand 'nota' and output the whole line.I know it's not the most space and time effective way, but I don't know how else I could do it. My csv file looks somewhat like this:

id,nume,prenume,grupa,seria,nota

1,Ion,Andrada,1003,A,8

2,Simion,Raluca,1005,A,7

3,Gheorghita,Mihail,1009,B,5

4,Mihailescu,Georgina,1002,A,6

I have applied the necessary changes proposed, but the program still doesn't seem to work. when I execute it it doesn't let me input values to read. I have seen that some of you have suggested to try it in another programming language, but in this case it is not possible.

Any ideas?

CodePudding user response:

Assuming you only need to the 3 highest lines, you can try this head and sort

$ head -3 <(sort -t',' -nrk6 input_file)
1,Ion,Andrada,1003,A,8
2,Simion,Raluca,1005,A,7
4,Mihailescu,Georgina,1002,A,6
  • Related