Question: ```
 ManagedElement=1,Transport=1,Router={{TAG_OAM_CMX_LEFT_VR_NAME_TAG}}
 no InterfaceIPv4=vlan1.{{TAG_OAM_NET_VLAN_TAG}}
 top
 ManagedElement=1,Transport=1,Router={{TAG_OAM_CMX_RIGHT_VR_NAME_TAG}}
 no InterfaceIPv4=vlan1.{{TAG_OAM_NET_VLAN_TAG}}
 commit -s

```

In this file, I want to add `#` in beginning if and only if the line contains `CMX`. So desired output is:

```
 #ManagedElement=1,Transport=1,Router={{TAG_OAM_CMX_LEFT_VR_NAME_TAG}}
  no InterfaceIPv4=vlan1.{{TAG_OAM_NET_VLAN_TAG}}
  top
 #ManagedElement=1,Transport=1,Router={{TAG_OAM_CMX_RIGHT_VR_NAME_TAG}}
 no InterfaceIPv4=vlan1.{{TAG_OAM_NET_VLAN_TAG}}
 commit -s

```

Answer: 
You can try the following:

```
sed -Ei 's/(^.*CMX.*$)/#\1/' your_file_name

```

It will replace the line containing `^.*CMX.*$` with itself, but by prepending with a `#`.

Here, in the replacement string `\1` is the whole matched string in parentheses. `^` indicates the beginning of line and `$` end of the line. `.*` matches any string (0 length or longer).
Using `sed` you can "replace" the zero-width `^` start-of-line anchor with `#` 

```
s/^/#/

```

only in lines of `file` that match `CMX`:

```
sed -i.bak '/CMX/s/^/#/' file

```