Using sed only on grep lines in file

I have a file which contains something like this:

192.168.1.1 user=a pass=b name=muzi standalone=true
192.168.1.2 user=a pass=b name=muzi standalone=true
192.168.1.3 user=a pass=b name=muzi standalone=true

Im trying to find best way to grep lines in the file that contains name=muzi and then run sed to change standalone=false to all these lines EXCEPT the first instance. so I would have

file.txt

192.168.1.1 user=a pass=b name=muzi standalone=true
192.168.1.2 user=a pass=b name=muzi standalone=false
192.168.1.3 user=a pass=b name=muzi standalone=false

I've found a way to do that with temp files, but this code is awful and I'm sure there's a simple way to do that


Solution 1:

This answer assumes "all these lines except the first instance" means "… except the first instance of name=muzi", not "… except the first instance of name=muzi standalone=true". So the first name=muzi is "the first instance", regardless what standalone is or even if it occurs in the line at all.

The answer also assumes you want to change exact strings standalone=true to standalone=false. E.g. standalone=auto will not be changed.

With sed, possibly not the simplest solution:

<datafile sed '
/\bname=muzi\b/ {   # only for lines matching the pattern
  x                 # exchange hold and pattern spaces
  s/^$//            # if former hold space empty
  t lbl             # then go to lbl
  g                 # else get the former pattern space back
  s/\bstandalone=true\b/standalone=false/     # actual replacement
  h                 # copy pattern space to hold space
  :lbl              # the label
  g                 # copy hold space to pattern space
}'

The hold space is initially empty. Any encounter of name=muzi makes the hold space non-empty. Empty hold space is an indicator name=muzi is beeing seen for the first time.

\b is an anchor that matches a word boundary. I used it where I thought it was appropriate. This way, name=muzilla, fullname=muzi or standalone=true1 do not match.