Discussion:
replace all characters in only a portion of a line
'Brian J. Murrell' brian@interlinx.bc.ca [sed-users]
2016-03-20 15:52:33 UTC
Permalink
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?


Cheers,
b.




[Non-text portions of this message have been removed]
Tim Chase sed@thechases.com [sed-users]
2016-03-20 18:27:52 UTC
Permalink
Post by 'Brian J. Murrell' ***@interlinx.bc.ca [sed-users]
Name: <a first name> <possibly attributions like M.D.>
Name: Bart Simpson, MD
and I want to redact the name part only with a 1:1 substitution of
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?
it's ugly, but can be done:

***@localhost$ cat redact.sed
#!/bin/sed -f
# save a pristine copy of the line
h
# delete everything up to the last comma to preserve any title
s/.*,/,/
# if there was a comma we need to put this in the hold buffer for later
t a
# otherwise just redact
b b

:a
# put the attribution in the hold space
x
# remove the attribution from the pristine version
s/,[^,]*$//
# redact the name (and the "Name: ")
s/\S/*/g
# bring back the title
G
# join the two lines
s/\n//
# Don't redact again
b c

:b
# we don't have an attribution
# redact the name (and the "Name: ")
s/\S/*/g

:c
# fix up the "Name: " if we redacted it
s/^....../Name: /

***@localhost$ printf "Name: Bart Simpson, MD\nName: Homer Simpson\n" | ./redact.sed
Name: **** *******, MD
Name: ***** *******
Davide Brini dave_br@gmx.com [sed-users]
2016-03-20 18:44:31 UTC
Permalink
On Sun, 20 Mar 2016 11:52:33 -0400, "'Brian J. Murrell'
Post by 'Brian J. Murrell' ***@interlinx.bc.ca [sed-users]
Name: <a first name> <possibly attributions like M.D.>
Name: Bart Simpson, MD
and I want to redact the name part only with a 1:1 substitution of
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.
sharma__r@hotmail.com [sed-users]
2016-03-21 02:03:34 UTC
Permalink
Post by 'Brian J. Murrell' ***@interlinx.bc.ca [sed-users]
Name: <a first name> <possibly attributions like M.D.>
Name: Bart Simpson, MD
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]
Jim Hill gjthill@gmail.com [sed-users]
2016-03-21 23:50:40 UTC
Permalink
#!/bin/sed -Ef
/^Name:/!b
h
s/[^:]*:([^,]*).*/\1/
s/[^ ]/*/g
G
s/(.*)\nName:[^,]*/Name:\1/

Save the whole thing, strip all but the name, redact nonblanks, append \n
and the whole thing, sub in the redacted name for the original.


[Non-text portions of this message have been removed]
Thierry Blanc Thierry.Blanc@gmx.ch [sed-users]
2016-03-22 08:11:38 UTC
Permalink
:a
/\*,/bb
s/[A-Za-z]/*/
ta
:b
s/^...../Name:/

What about a name like Bart J. Simpson?

Tested on sedroid
Post by 'Brian J. Murrell' ***@interlinx.bc.ca [sed-users]
Name: <a first name> <possibly attributions like M.D.>
Name: Bart Simpson, MD
and I want to redact the name part only with a 1:1 substitution of
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?
Cheers,
b.
[Non-text portions of this message have been removed]
------------------------------------
------------------------------------
--
------------------------------------
Yahoo Groups Links
--
Sent from my FAIRPHONE. Be fair, don't fox.

[Non-text portions of this message have been removed]
Thierry Blanc Thierry.Blanc@gmx.ch [sed-users]
2016-03-22 17:03:17 UTC
Permalink
On 22/03/16 09:11, Thierry Blanc ***@gmx.ch [sed-users] wrote:

now access to full keyboard.

/^Name:/!b
# if line starts with Name: don't branch to end
:a
/\*,/bb
# if line containts '*,' all letters up to comma have been replaced by
'*' so branch to b
s/[^, *]/*/
# substitute next letter (that is not comma, space or asterix)
ba
:b
s/^...../Name:/
# put the beginning of line back in place
Post by Thierry Blanc ***@gmx.ch [sed-users]
:a
/\*,/bb
s/[A-Za-z]/*/
ta
:b
s/^...../Name:/
What about a name like Bart J. Simpson?
Tested on sedroid
Post by 'Brian J. Murrell' ***@interlinx.bc.ca [sed-users]
Name: <a first name> <possibly attributions like M.D.>
Name: Bart Simpson, MD
and I want to redact the name part only with a 1:1 substitution of
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?
Cheers,
b.
[Non-text portions of this message have been removed]
------------------------------------
------------------------------------
--
------------------------------------
Yahoo Groups Links
------------------------------------

------------------------------------
--
------------------------------------

Yahoo Groups Links

<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/sed-users/

<*> Your email settings:
Individual Email | Traditional

<*> To change settings online go to:
http://groups.yahoo.com/group/sed-users/join
(Yahoo! ID required)

<*> To change settings via email:
sed-users-***@yahoogroups.com
sed-users-***@yahoogroups.com

<*> To unsubscribe from this group, send an email to:
sed-users-***@yahoogroups.com

<*> Your use of Yahoo Groups is subject to:
https://info.yahoo.com/legal/us/yahoo/utos/terms/
Loading...