this post was submitted on 18 Jan 2025
10 points (91.7% liked)

Shell Scripting

1378 readers
1 users here now

From Ash, Bash and Csh to Xonsh, Ysh and Zsh; all shell languages are welcome here!

Rules:
  1. Follow Lemmy rules!
  2. Posts must relate to shell scripting. (See bottom of sidebar for more information.)
  3. Only make helpful replies to questions. This is not the place for low effort joke answers.
  4. No discussion about piracy or hacking.
  5. If you find a solution to your problem by other means, please take your time to write down the steps you used to solve your problem in the original post. You can potentially help others having the same problem!
  6. These rules will change as the community grows.

Keep posts about shell scripting! Here are some guidelines to help:


In general, if your submission text is primarily shell code, then it is welcome here!

founded 2 years ago
MODERATORS
 

Hi there!

Usually, sed can be used in different ways, but most of the time we use it to match lines in a file against a fixed regexp. Some examples:

This replaces ocurrences of regexp for "foo":

sed 's/regexp/foo/g' < myfile

This prints all lines that have "foo", but will change the first "o" in the line for an "a":

sed -n '/foo/s/o/a/p' < myfile

and so on...

But I tried to do a different thing, with no success: can I pass to sed a file with a bunch of regular expressions and test them against a fixed string? I tried to play with pattern space, hold space, with no success. It just seems impossible to use them (which would be the closest to "variables") in search commands.

I know sed is Turing complete, but using it that way would maybe require to implement a regexp engine from scratch?

Thanks!

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 2 days ago

What result do you need? Do you just nedd to know if one of the regexps matched the string, or do you need to know which one, or is it something else?