开发者

sed + replace only if match the first word in line

The following sed command replaces the OLD string with the NEW string. My target is to replace the OLD to NEW only if COMMAND word appears as the first word in the line. How to fix my sed syntax in order to replace OLD with NEW only if COMMAND is the first word in line? (Note: COMMAND word location could be after 开发者_如何学运维some spaces from the line beginning.)

lidia

sed "/^ *#/b; /COMMAND/ s/OLD/NEW/g"  file

  COMMAND OLD
  OLD COMMAND

after sed exe:

  COMMAND NEW
  NEW COMMAND


"/^[ \t]*COMMAND/ s/OLD/NEW/g"


try this: 's/^(\s*COMMAND) OLD(.*)$/\1 NEW\2/'


awk solution

awk '{if($1=="COMMAND") $2="NEW";print}' temp.txt

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜