This might be so simple but I need to do this:
I have a file name a.sh
, and it contains few lines as below
# firstname banuka
firstcolor green
lastname jananath
# age 25
And I want to write firstname banuka
to this file, so it would look like
echo "firstname banuka" > a.sh
BUT, before writing firstname banuka
I want to check if file already has that value (or line)
As you can see in the file content of a.sh
, the part we are going to write (firstname banuka
) can be already there but with a comment.
So if it has a comment,
1. I want to un-comment it (remove `#` in front of `firstname banuka`)
If no comment and no line which says `firstname banuka`,
2. Add the line `firstname banuka`
If no comment and line is already there,
3. skip (don't write `firstname banuka` part to file)
Can someone please help me?