1

Initially I had a mistake in my below, so I'm editing and reopening this.

I'm having some problems injecting lines into files. My goals are:

  1. Look for the line (in this case VIMLINE). If the line is there, do nothing (or remove the line, I have need to do both 'do nothing' and 'remove the line' at different times, but don't know how to fit removal in - removal is for situations where I want to ensure that I am injecting at the end of the script).
  2. If the line is not there (which is what || should do, I thought) then append the line to the end of the script.

The following works perfectly for injecting a line that is not already in a file into that file.

VIMLINE='color industry'
grep -qxF "$VIMLINE" ~/.vimrc || echo $VIMLINE | sudo tee --append ~/.vimrc

My question then is, how could I modify the above to: look for a line (or part of a line) and remove that line and then reinject that line (so as to guarantee that the line is added to the end of the file)?

YorSubs
  • 3,194
  • 7
  • 37
  • 60

1 Answers1

2

Answer to the new question:

sed -i '/'$VIMLINE'/{h;d}; $G' ~/.vimrc

Answer to the old question: You are grepping on $VIMLINE literally, use grep -qxF "$VIMLINE" ... to grep on "color industry" instead.

  • 1
    To add, double quotes will expand a variable, single quotes won't. – Raman Sailopal Jan 27 '21 at 10:01
  • Yep, I got it as soon as he mentioned. Just one of those situations, I looked at it a dozen times and couldn't see the wood for the trees ... I've edited the question and resubmitted it. – YorSubs Jan 27 '21 at 10:47