Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 4 down vote accepted

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.

share|improve this answer
+1 FWIW the g in /gc is redundant, since this regex can only match one per line anyway. – Sparhawk Jan 9 at 23:06
I was editing my answer while you wrote this comment (= – Kira Jan 9 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 at 23:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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