So I have been enjoying trying to find minimal answer results to questions of late using a variety of sed, awk and bash (and happy for other alternatives).
I have a small piece of code and was wondering if anyone has a good one liner to accomplish the same, the ones i currently have don't seem much better than my current code:
Current Code:
Code:
if [[ $(grep -q "foo" $file) -eq 0 ]]
then
sed -i 's/foo/bar/' $file
else
echo "bar" >> $file
fi
As you can see the general scenario is:
1. If "foo" in file change it for "bar"
2. Else add "bar" to the end of the file
To give an idea of what I have tried:
Code:
awk 'BEGIN{f=1}/foo/{gsub(/foo/,"bar");f=0}END{if(f)print "bar"}1' file
Any suggestions greatly appreciated