On this text

hardware mac 33:33:333:33:33
hardware mac 33:13:333:33:33
hardware mac 33:23:333:23:34
hardware mac 33:033:133:33:35

I want to replace all lines starting with hardware mac with

hardware mac 00:00:00:00:00:00

I have tried

sed s/^hardware mac/hardware mac 00:00:00:00:00:00/gc

and give error

also the classic vim command

%s:/^hardware mac/hardware mac 00:00:00:00:00:00/gc

But change only the string hardware mac leaving the numbers untouched. How to replace string beginning with hardware mac with my string?


You almost got it right, you just forgot to match the rest of the line (on vim):

:%s/^hardware mac.*/hardware mac 00:00:00:00:00:00/gc

You can drop the gc options at the end if you are sure of what you are doing, g is likely to be unnecessary since you won't match more than once per line.

With sed it is the same thing:

sed -i 's/^hardware mac.*/hardware mac 00:00:00:00:00:00/' filename

Note that sed's substitution command s does not accept a c (confirm) flag, like vim does.

  • +1 FWIW the g in /gc is redundant, since this regex can only match one per line anyway. – Sparhawk Jan 9 '16 at 23:06
  • I was editing my answer while you wrote this comment (= – Kira Jan 9 '16 at 23:08
  • Note that that sed syntax is GNU style. Not all versions support -i and some require an argument to it. – Eric Renouf Jan 9 '16 at 23:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.