0

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?

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
Multi
  • 93
  • Is gift actually set as yes in your file? 2. Did you create the file.txt on Windows but are running the code on a Linux platform?
  • – Chris Davies Apr 07 '19 at 16:11
  • By default, gift is set as No. So inside the loop, I want to check those ones who have No and change it to yes.

    And no, file it's made on Linux (Ubuntu).

    – Multi Apr 07 '19 at 16:15
  • 2
    @Multi17 The simple act of read-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 source file.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 to file.txt to replace the original file. – LL3 Apr 07 '19 at 16:20
  • @LL3 Okey, thanks for explain this. I will give it a go to it! – Multi Apr 07 '19 at 16:21