1

In another thread

grep search

got the proper use right for the OR argument in a grep search in BBEDIT and so was wondering whether this can be expanded as following :

is it possible to have a given set of different entries with different answers in the Find field or is it just one by one ?

Is there a (grep) symbol or script available to run the following queries as a single one :

string1|string2|string3|string4

Replace : string 5

stringA|stringB|stringC|stringD

Replace : string E

string@|string#|string$|string%

Replace : string £

For now I run each of these queries individually, but if it’s possible to group them together in a single find&replace query, that would be awesome !

6
  • I'm afraid this can't be done in grep. Such job requires conditional replacement.
    – Toto
    Commented Apr 15, 2023 at 12:06
  • Grep doesn't do replacement.
    – Toto
    Commented Apr 15, 2023 at 12:13
  • This can easily be done with Notepad++. I can provide an answer if you want.
    – Toto
    Commented Apr 15, 2023 at 12:23
  • yea sure, I'm interested, how can it be done in Notepad++ ? Does it run on Apple Silicon macs ? Commented Apr 15, 2023 at 12:49
  • basically are you talking a "Conditional Regex Replacement" ? I see Notepad++ isn't available for Mac but maybe this could work on the FOSS successor of Atom, called PULSAR ? Commented Apr 15, 2023 at 13:12

1 Answer 1

0

Here is a way to do the job with Notepad++ or any tool that uses boost regex flavour.

  • Ctrl+H
  • Find what: (s1|s2|s3|s4)|(sA|sB|sC|sD)|(s@|s#|s\$|s%)
  • Replace with: (?1s5)(?2sE)(?3s£)
  • TICK Wrap around
  • SELECT Regular expression
  • Replace all

Explanation:

(s1|s2|s3|s4)       # group 1, 1 out 4 possibilities
  |                   # OR
(sA|sB|sC|sD)       # group 2, 1 out 4 possibilities
  |                   # OR
(s@|s#|s\$|s%)      # group 3, 1 out 4 possibilities

Replacement:

(?1         # if group 1 exists
  s5          # print s5
)           # endif
(?2sE)      # if group 2 exists, print SE
(?3s£)      # if group 3 exists, print $£

Screenshot (before):

enter image description here

Screenshot (after):

enter image description here

5
  • wow thanks. Will try that out and update. I already see i can do that kind of seach in Pulsar app, which is cross-platform. Will try out the changes syntax. Very powerful... Commented Apr 15, 2023 at 15:03
  • @istackoverflow: You're welcome, glad it helps. If it works for you, feel free to mark the answer as accepted, How to accept an answer
    – Toto
    Commented Apr 15, 2023 at 16:05
  • ok i need to type a follow-up. If the command syntax for the search works great (it finds all the occurrences inside the brackets), the replacement command syntax does not work. It actually prints out the entire code block, i.e. it replaces the first block with "(1s5)" instead of printing just 5. So what did i do wrong ? There's no space between the code syntax blocks, i just copy pasted your answer. Commented Apr 19, 2023 at 8:33
  • PS in Pulsar i don't have this 'wrap around' option. I just have Regex and case insensitive as enabled options (other options are : match case, whole word). Commented Apr 19, 2023 at 8:40
  • @istackoverflow: Your editor (Pulsar) doesn't understand “conditional replacement”. Too bad :-(
    – Toto
    Commented Apr 22, 2023 at 9:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .