| Newsgroups |
gmane.editors.sed.user |
| Message-ID |
<[email protected]> |
---In [email protected], <brian@...> 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 do this using the following:
sed -e '
s/:/&\
/
:loop
s/\(\n\)\([^a-zA-Z,]\{1,\}\)/\2\1/; tloop
s/\(\n\)[a-zA-Z]/*\1/; tloop
s/\n//
' yourfile
Comments:
1. Place a marker (\n) on the ":" and then observe to your right.
You should be able to see 3 possible things:
a) non-alphabet non-comma (it's length may be > 1)
b) alphabet
c) comma
2. Based on what you see a/b/c in step-1, you perform the following:
a) exchange marker <-> non-alpha,non-comma , go back for more
b) exchange marker <-> alpha with a *, go back for more
c) strip away marker since you've arrived home
Notes:
The various "sed" commands used in this were the following:
1. Branching command "b"
2. Substitute command "s"
3. Label command ":"
4. Test command "t"
[Non-text portions of this message have been removed]