Change value from file inside a while

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

I have a file like a database where I save name,surname,date of ref="https://forum.tuteehub.com/tag/birth">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  dateofref="https://forum.tuteehub.com/tag/birth">birth1  gift1
name2  surname2  dateofref="https://forum.tuteehub.com/tag/birth">birth2  gift2

Inside gift colum, I save "Yes" or "No" and my ref="https://forum.tuteehub.com/tag/code">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?

profilepic.png
manpreet 2 years ago

 

Reading a value from a file and then changing the variable that holds that value will not f="https://forum.tuteehub.com/tag/change">change the value in the file.

Instead, you would have to detect what values need changing, f="https://forum.tuteehub.com/tag/change">change them, and then output the complete file (including modified values) to a new file. This new file can then be moved to the original name, replacing the old file.

Also, parsing text in the shell is generally to be avoided. Instead, consider using something like awkfor that:

awk -F '\t' 'BEGIN { OFS=FS }
             $4 == "No" { print "Congrats, here is your gift" >"/dev/stderr" }
             { $4 = "Yes"; print }' file.txt >newfile.txt

This would read through the file line by line and whenever the fourth column says No, it outputs the text Congrats, here is your gift.

Then, for every line, the fourth column is unconditionally set to Yes before the whole line is printed.

The way the program is written and run above, the Congrats... message will appear in the terminal (it's written to the standard error stream) while the updated contents of the file will be written to the new file called newfile.txt.

The BEGIN block at the start of the code simply sets the output field separator, OFS, to the same value as the input field separator, FS, which is set to a tab character on the command line using -F '\t'.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community