Home > other >  how do I replace a information Fromm csv file?
how do I replace a information Fromm csv file?

Time:05-24

I have the following program

#!/bin/bash
exec 3< lista.csv
read -u 3 header
declare -i id_nou
echo "ID: "
read id_nou
while IFS=, && read -u 3 -r id nume prenume seria grupa nota
do
if [ "$id_nou" -eq "$id" ]
then
echo "Nota noua: "
read  nota_noua
nota=$nota_noua
print > lista.csv
fi
done

My csv file looks something 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

What I'm trying to do is replace the nota value of the correspondent's id with a given by the keyboard value, but this doesn't seem to work. The error message is

line 14: print: command not found

Any advice?

CodePudding user response:

Here's one in awk:

awk 'BEGIN {
    FS=OFS=","                            # comma field separators
    printf "id: "                         # ask for id
    if((getline id < "/dev/stdin")<=0)    # store to a variable
        exit 1
    printf "nota: "                       # ...
    if((getline nota < "/dev/stdin")<=0)
        exit 1
}
$1==id {                                  # if firsst field matches
    $NF=nota                              # replace last field value
}
1' file                                   # output

Output:

id: 1
nota: THIS IS NEW VALUE
id,nume,prenume,grupa,seria,nota
1,Ion,Andrada,1003,A,THIS IS NEW VALUE
2,Simion,Raluca,1005,A,7
3,Gheorghita,Mihail,1009,B,5
4,Mihailescu,Georgina,1002,A,6

Here is some info on saving the changes.

  • Related