Search code examples
sedformattingmac-address

How format mac address inside json array


Need help in getting below the mac address inside the json file to re-reformatted using sed cat 251.json

 cat /tmp/251.json


   [
  "08:f1:ea:6d:03:3c",
  "08:f1:ea:6d:03:3d",
  "08:f1:ea:6d:03:3e",
  "08:f1:ea:6d:03:3f",
  "b8:83:03:81:4b:20",
  "b8:83:03:81:4b:21",
  "b8:83:03:84:d5:1c",
  "b8:83:03:84:d5:1d"
]

The expected format is

 [
  "08f1.ea6d.033c",
  "08f1.ea6d.033d",
  "08f1.ea6d.033e",
  "08f1.ea6d.033f",
  "b883.0381.4b20",
  "b883.0381.4b21",
  "b883.0384.d51c",
  "b883.0384.d51d"
]

Solution

  • This should work:

    sed -E 's/:(.{2}):(.{2}):(.{2}):(.{2}):/\1.\2\3.\4/g' /tmp/251.json
    

    In this way, you get the output to stdout. If you want to modify the file, add the -i option. You can check the result here.