0

I have the following line:

[email protected] [The Name], [email protected] [The Name 2], [email protected] [Name 3], ...

and so on, all on one line ($ROW). (It's osascript output based on the user's multi-choice selection from a list.)

Later on in the script I want to run a loop only over the email addresses:

for ADDRESS in $ROW ; do

which by default would use a whitespace as delimiter.

So I need to delete everything between every " [" (incl. the space before) and the following "]," … so that the result is

[email protected] [email protected] [email protected] ...

until the end of the line (the variable).

The problem is that I'm doing this on macOS which has the BSD versions of sed, awk etc., and until now I could only find GNU solutions. I do have the coreutils installed, so I could do it with GNU, but this a script meant for macOS distribution to other Mac users, who might not have installed coreutils, so GNU sed, awk etc. are not an option.

JayB
  • 533
  • 4
  • 10
  • So I assume `grep -Po '\S+@\S+' file` is not working to you... – fedorqui Sep 08 '16 at 10:58
  • I'm afraid no... though I've got an idea… maybe it works with `awk '{gsub}'` – JayB Sep 08 '16 at 11:00
  • if you have [extglob](https://stackoverflow.com/questions/216995/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu) option, you can use `"${ROW//\[*([^]])\],/}"` – Sundeep Sep 08 '16 at 11:11
  • @sp-asic produces irregular output in my working zsh (with zprezto); on standard macOS /bin/bash, which this script will use, it isn't working. Maybe awk/gsub works, but if, it's not so easy due to [ and ] in $ROW. Would have to be escaped somehow, and then you'd also have to use a wildcard. – JayB Sep 08 '16 at 11:22
  • is it enabled? add `shopt -s extglob` within script to enable.. from shell you can use `shopt extglob` to check if it available.. but as you said, it may not be an option in your case – Sundeep Sep 08 '16 at 11:42
  • In fact, now I notice -P was not needed, so `grep -Eo '\S+@\S+' file` should do. – fedorqui Sep 08 '16 at 11:44

2 Answers2

0

I think this should work with the bsd grep:

for address in $(echo "$ROW" | grep -Eo '[^ ]*@[^ ]*'); do
    ...
done
redneb
  • 21,794
  • 6
  • 42
  • 54
0

Hopefully this works for you:

sed 's/\[[^[]*\],//g' <<< $ROW

The command deletes everything between [ and ], by searching the open square bracket \[, followed by anything else that is not an open square bracket [^[*], followed by the closing square bracket and comma \],.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
oliv
  • 12,690
  • 25
  • 45