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!

  • freagle@lemmygrad.ml
    link
    fedilink
    English
    arrow-up
    1
    arrow-down
    1
    ·
    edit-2
    2 days ago

    Way over engineered.

    Both grep and sed take pattern files as input

    For sed, the -f flag

    -f script-file
    --file=script-file
    
    Add the commands contained in the file script-file to the set of commands to be run while processing the input.
    

    For grep, -f

    -f FILE, --file=FILE
    Obtain patterns from FILE, one per line.  If this option is used multiple times or is combined with the -e (--regexp) option, search for all patterns given.  The empty file contains zero patterns, and therefore matches nothing.  If FILE is - , read patterns from standard input.