On 12/19/2017, at 04:10, @lbutlr <[email protected] <mailto:[email protected]>> 
wrote:
> On 18 Dec 2017, at 09:18, Christopher Stone <[email protected] 
> <mailto:[email protected]>> wrote:
>> 
>> sed -E '
>>    s!smitj1234@students\.uwc\.edu!!g
>>    s!jane\.smith@uwc\.edu!!g
>> '
> 
> Careful, each of these example is unbounded, so for example if you have 
> [email protected] <mailto:[email protected]> and [email protected] 
> <mailto:[email protected]> in your list and you match and replace 
> [email protected] <mailto:[email protected]>, you will be left with 'a' for 
> the second user.


Hey Lewis,

Good catch.  Thanks.

Easily fixed.


#!/usr/bin/env bash

sed -En '

   s!^jane\.smith@uwc\.edu$!!
   s!^username@students\.uwc\.edu$!!
   s!^username@wpr\.uwex\.edu$!!

   # Uncomment next line to delete blank lines
   # /^$/d

   p
'


This method is very inconvenient, because the regular expressions for replace 
must be assembled, and the special characters in email addresses escaped.  It's 
just too labor intensive.


Neither the grep or the Perl solutions are affected by Lewis' gotcha — they 
both work with the literal email address string.

I went that route, because valid email characters include a number of regex 
metacharacters — and because I was trying to make it really simple for the user 
to provide the invalid email list.  (In this case in a file with one address 
per line.)


Okay, let's do something similar with sed by assembling the command and then 
running it.

The following script lets the user open it (the script) — paste in a bare list 
of email addresses — and run it on the front BBEdit window.


#!/usr/bin/env bash

# Enter invalid email addresses here — one per line.
read -r -d '' emailAddressList <<'EOF'

[email protected] <mailto:[email protected]>
[email protected] <mailto:[email protected]>
[email protected] <mailto:[email protected]>

EOF

sedCmd=$(sed -E '
        s!([*+-?^{}|.])!\\\1!g
        s/(^.+)/s!^\1$!!/g
' <<< "$emailAddressList")

sedCmd="sed -En '
$sedCmd

# Uncomment these two lines to remove empty lines.
# s![[:blank:]]+!!g
# /^$/d

p

'"

eval "$sedCmd"


This script will leave holes where lines were removed, so the user can see what 
happened.

But — by uncommenting the two lines as instructed in the script all blank lines 
(holes) will be removed.

The script also fixes the gotcha Lewis identified.

--
Best Regards,
Chris

-- 
This is the BBEdit Talk public discussion group. If you have a 
feature request or would like to report a problem, please email
"[email protected]" rather than posting to the group.
Follow @bbedit on Twitter: <http://www.twitter.com/bbedit>
--- 
You received this message because you are subscribed to the Google Groups 
"BBEdit Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/bbedit.

Reply via email to