1

Below is extract from the file to be changed: May use awk or sed...any utility is good for pattern matching

mgad 1
m-id         50
rf-chan-id   1
base-station-id 00:a0:bc:0c:1b:c1
....
....
mgad 2
....
base-station-id 00:a0:bc:0c:1b:c2
....
....
mgad 3
....
base-station-id 00:a0:bc:0c:1b:c3
....
....
mgad 4
base-station-id 00:a0:bc:0c:1b:c4

I have to edit mac address in the 5th position from 00:a0:bc:0c:XX:c3 to 00:a0:bc:0c:18:c3 for multiple lines in the file. PN: 5th position can be variable,needs to be changed to "18"

I tried this:

sed -En ' s/^\( base-station-id.* [0-9A-Fa-f]{2}\:[0-9A-Fa-f]\:[0-9A-Fa-f]\:[0-9A-Fa-f]\:\)[0-9A-Fa-f]+\(\:.*\)$/\118\2/p; '

Please help with correct regex pattern matching and replace with desired change in substring in mac address

1 Answer 1

1

Capture the leading portion; allow any two characters in the 5th position of the MAC, then capture the trailing portion, and replace it with the 1st captured group, the text 18, and the 2nd captured group:

sed 's/\(base-station-id ..:..:..:..:\)..\(:..\)/\118\2/' < input > output
5
  • I get an error: code sed -En 's/( base-station-id ..:..:..:..:)..(:..)/\118\2/p' file.txt sed: -e expression #1, char 53: invalid reference \2 on `s' command's RHS
    – Rocky M
    Commented Oct 31, 2017 at 0:37
  • Escape the parens
    – Jeff Schaller
    Commented Oct 31, 2017 at 0:38
  • sed -En 's/( base-station-id ..:..:..:..:)..(:..)/\118\2/p' file.txt sed: -e expression #1, char 53: invalid reference \2 on `s' command's RHS
    – Rocky M
    Commented Oct 31, 2017 at 0:40
  • No need for -E here, either.
    – Jeff Schaller
    Commented Oct 31, 2017 at 0:41
  • Works without -E
    – Rocky M
    Commented Oct 31, 2017 at 0:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .