0

I have the following code:

#!/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="inlocuit"
        sed -i "s/$nota/$nota_noua/6" "$lista.csv"
    fi
done

My .csv file looks something like this:

id,nume,prenume,grupa,seria,nota
1,Dan,Alexandru,1001,A,7
2,Boerescu,Andrei,1003,A,3
3,Pocoae,Mihai,1004,A,9

What I am trying to achieve is simply replace the nota variable from a specific ID with a value inputed by the keyboard. From using sed I get the following error:

".csv": invalid command code

Any advice?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Please take care to format your code for legibility; you are wasting our time by having us scan for the end of the loop when the loop body is not indented. – tripleee May 24 '22 at 11:45
  • The duplicate is based on a guess; if you are not on a Mac or *BSD, please [edit] to clarify what exactly your OS and `sed` version is etc. – tripleee May 24 '22 at 11:48
  • Modifying the file you are reading from is problematic for other reasons, though. Probably just ask for the replacement and then `sed -i "" "/^$id_nou,/s/,[^,*]/,$nota_noua/5" lista.csv` without a loop if the intent is to replace the sixth field in question only on the line with this ID. Perhaps see also https://stackoverflow.com/questions/65538947/counting-lines-or-enumerating-line-numbers-so-i-can-loop-over-them-why-is-this?r=SearchResults&s=1%7C13.2059 – tripleee May 24 '22 at 11:52

0 Answers0