Search code examples
regexbashshell

How to replace text using a regular expression in bash


Imagine you have the following partial log of a network request (in this case it's an from an iSO app). In this specific example I want to replace the values of the Authorization and X-Client-Secret headers using ONE regular expression in Bash so we can share the log without exposing any secrets:

Http: createRide (422): http(serviceResponse: POST https://staging.test.com/sometpath {
   ["Authorization": "Bearer xxxxxxxxxx", "X-REQUEST-ID": "5FE8D9C0-B3F0-4DAE-9188-A0CB1906EB5D", "X-Client-Version": "3.0.0", "X-Client-Identifier": "something", "Content-Type": "application/json", "X-Api-Version": "1", "X-Client-Secret": "xxxxxxxxx"]
 }
  {
  "data" : {
  }

The regex should work as follows:

Find the header fields that contain either the name secret or authorization, ideally case insensitive and replace its value by e.g. ***.

The result should look as follows:

Http: createRide (422): http(serviceResponse: POST https://staging.test.com/sometpath {
   ["Authorization": "***", "X-REQUEST-ID": "5FE8D9C0-B3F0-4DAE-9188-A0CB1906EB5D", "X-Client-Version": "3.0.0", "X-Client-Identifier": "something", "Content-Type": "application/json", "X-Api-Version": "1", "X-Client-Secret": "***"]
 }
  {
  "data" : {
  }

Solution

  • This should work

    masked_log=$(echo "$log" | sed -E 's/(Authorization|Secret)":\s*"[^"]*"/\1": "***"/Ig')