Reputation: 149
I am grepping logs for the word "line1" and need to replace the text following that word that is between the characters : and , possible results would be:
xxx,\"line1\":\"C/O FRED FLINSTONE, MD\",xxx
xxx,\n line1: 'C/O FRED FLINSTONE, MD',xxx
xxx,\\\"line1\\\":\\\"C\\\\/O FRED FLINSTONE\\\,MD",xxx
I want to replace "C/O FRED FLINSTONE, MD" with "Redacted-Address1" so the end result would look something like:
xxx,\"line1\":Redacted-Address1,xxx
xxx,\n line1:Redacted-Address1,xxx
xxx,\\\"line1\\\":Redacted-Address1,xxx
I don't necessarily need to use SED but thought that was a good place to start. The xxx represents the reset of the line (not actual xxx) so we cant search by that and I want to leave that untouched.
A more complete example of the data would be:
,\"object\":{\"address\":[{\"city\":\"Bedrock\",\"line1\":\"C/O FRED FLINSTONE\, MD\",\"line2\":\"55101 Main St\",\"state\":\"TX\",\"use\":\"H\",\"zip\":69162}],
And the desired result would be:
,\"object\":{\"address\":[{\"city\":\"Bedrock\",\"line1\":Redacted-Address1,\"line2\":\"55101 Main St\",\"state\":\"TX\",\"use\":\"H\",\"zip\":69162}],
Upvotes: 0
Views: 1626
Reputation: 58568
This might work for you (GNU sed):
sed 's/\(line1[^:]*:\).*,/\1Redacted Name,/' file
This uses the pattern line1
and any characters following which are not a :
followed by a :
; and then greed (all characters to the end of the file then backtracking till a ,
is found). The match is then replaced by the back reference of the pattern upto the first :
and the required string followed by a ,
.
Upvotes: 0
Reputation: 114548
It sounds like you want to get everything between the colon following line1
and the coma that immediately precedes the next colon. The following regex should accomplish that by replacing everything but the capture groups:
sed 's/\(line1[^:]*:\)[^:]*\(,[^,:]*:\)/\1 Redacted-Address1\2/'
Upvotes: 1
Reputation: 8402
Using sed
sed -r '/line1/{s/([\]"line1[\]":)[\]"[^"]+",/\1Redacted-Address1,/}'
example
echo ',\"object\":{\"address\":[{\"city\":\"Bedrock\",\"line1\":\"C/O FRED FLINSTONE\, MD\",\"line2\":\"55101 Main St\",\"state\":\"TX\",\"use\":\"H\",\"zip\":69162}], '|sed -r '/line1/{s/([\]"line1[\]":)[\]"[^"]+",/\1Redacted-Address1,/}'
output will be
,\"object\":{\"address\":[{\"city\":\"Bedrock\",\"line1\":Redacted-Address1,\"line2\":\"55101 Main St\",\"state\":\"TX\",\"use\":\"H\",\"zip\":69162}],
Upvotes: 1
Reputation: 786091
You can use this sed with greedy regex :.*,
which will match from :
to last ,
:
sed 's/:.*,/:Redacted Name,/' file
xxx,\"line1\":Redacted Name,xxx
xxx,\n line1:Redacted Name,xxx
xxx,\\"line1\\":Redacted Name,xxx
As per comments below:
sed "s/:..*['\"],/:Redacted Name,/" file
xxx,\"line1\":Redacted Name,xxx
xxx,\n line1:Redacted Name,xxx
xxx,\\"line1\\":Redacted Name,xxx
Upvotes: 0