Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate string elements that don't match a pattern?

Tags:

linux

grep

bash

awk

If I have

days="1 2 3 4 5 6"

func() {
    echo "lSecure1"
    echo "lSecure"
    echo "lSecure4"
    echo "lSecure6"
    echo "something else"
}

and do

func | egrep "lSecure[1-6]"

then I get

lSecure1
lSecure4
lSecure6

but what I would like is

lSecure2
lSecure3
lSecure5

which is all the days that doesn't have a lSecure string.

Question

My current idea is to use awk to split the $days and then loop over all combinations.

Is there a better way?

Note that grep -v inverts the sense of a plain grep and does not solve the problem as it does not generate the required strings.

like image 247
Sandra Schlichting Avatar asked Jul 29 '26 22:07

Sandra Schlichting


1 Answers

I usually use the -f flag of grep for similar purposes. The <( ... ) code generates a file with all possibilities, grep only selects those not present in the func.

func | grep 'lSecure[1-6]' | grep -v -f- <( for i in $days ; do echo lSecure$i ; done )

Or, you may prefer it the other way round:

for i in $days ; do echo lSecure$i ; done | grep -vf <( func | grep 'lSecure[1-6]' )
like image 118
choroba Avatar answered Aug 01 '26 11:08

choroba