I have a file like a database where I save name,surname,date of birth, gift, and I want to know if this person have got a gift from us or not. Those dates are splited by tabs in the file, one person per line, like:
name1 surname1 dateofbirth1 gift1
name2 surname2 dateofbirth2 gift2
Inside gift colum, I save "Yes" or "No" and my code looks like:
while IFS=$'\t' read -r name surname dob gift; do
if [[ "$gift" == "No" ]]; then
echo "Congrats, here is your gift
gift=Yes
fi
done < "file.txt"
But the gift colum, don't seems to change to yes, only seems to be there as an aux var. How can I change the value of that line, in that colum, to yes?
gift
actually set asyes
in your file? 2. Did you create thefile.txt
on Windows but are running the code on a Linux platform?And no, file it's made on Linux (Ubuntu).
– Multi Apr 07 '19 at 16:15read
-ing a line does not bind that line to the variables you specified for receiving the line’s fields. You need to rewrite that line with the updated value of"${gift}"
. As to how do that, replacing it ”in-place” (i.e. on the same file and line as your sourcefile.txt
) cannot be done simply. What you may do in a simple way is just output the updated line, along with all the other lines, and redirect that output to another file, which you’ll then move tofile.txt
to replace the original file. – LL3 Apr 07 '19 at 16:20