myfile.csv
445,aLabel
446,anotherLabel
447,aThirdLabel
In a bash script, I want to search for the presence of "anotherLabel" (in a case-insensitive way) within myfile.csv and get the value 446. Additionally, if "anotherLabel" isn't present, I want to add it to the end, incrementing the token of the previous line by 1.
I started with the if
statement:
if grep -Fiq anotherLabel myfile.csv; then
#get the value of field 1 (446 in this example) and store it in a variable
else
#increment the last value of field 1 present in the file and store it in a variable
#and append "448,anotherLabel" to myfile.csv
fi
I'm not sure that using grep to verify whether the label is even in the file is the best way to approach this or if there is an easier method using sed or awk.