| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
On Sun, 20 Mar 2016 11:52:33 -0400, "'Brian J. Murrell'
[email protected] [sed-users]" <[email protected]> wrote:
> Let's say I have a file of lines and some of the lines are of the form:
>
>
> Name: <a first name> <possibly attributions like M.D.>
>
>
> Such as:
>
>
> Name: Bart Simpson, MD
>
>
> and I want to redact the name part only with a 1:1 substitution of
> letters to asterisks so that the result is:
>
>
> Name: **** *******, MD
>
>
> So effectively I want to do a 's/[A-Za-z]/*/g' only on the portion of the
> line that comes after the ": " and before any ,.
>
>
> Is there any way to do this with sed?
You can save the whole original line in the hold space (after marking the
name part for easier processing later on), isolate the name in the pattern
space, do the replacement and then rebuild the line using the version
stored in the hold space. Sample code:
# mark the name part before saving the line
s/[:,]/\
&/g
# copy to hold space
h
# remove non-name parts
s/.*\n: //
s/\n,.*//
# do the replacement
s/[^ ]/*/g
# switch to hold space
x
# append former pattern space (now hold space)
G
# cleanup
s/\(.*\)\n:.*\n,\(.*\)\n\(.*\)/\1: \3,\2/
--
D.