grep!

guys if I have a load of text files which contain many lines with this one in particular in there somwhere:

cheese [XYZ] jam, bread [XYZ]

where [XYZ] is a random string, how would I do a grep for this?

I tried:

grep "cheese * jam, bread *" file.txt but this doesn't work, whats the search pattern i need to put into grep?

Try:

grep 'cheese.*jam.*bread'

doesn't seem to work,

the line I'm trying to return is:

cheese, bread [XYZ_JIM], spoon [CreamCake],

the XYZ part is a totally random string

so it's gzgrep 'cheese, bread ??? JIM], spoon [CreamCake]' file???

(sorry it's actually gzgrep not grep)

This is because grep does not use shell-style wildcards, it uses regular expressions. The . means "match any character", and the * means "match one or more of the previous character". So .* means "match one or more of anything".

Perhaps you could use gzegrep?

i.e. like:

$ gzegrep "cheese|bread|jam" file.txt

Although to me, it's not really clear which bit you are trying to "grep" for, and really what [XYZ] has to do with any of it.

[edit] crosspost.

You asked for "cheese...jam...bread", not "cheese....bread....spoon" so of course it will not match since it cannot find 'jam' nor 'bread' in the order it expects. Alter the pattern "cheese.*jam.*bread" to "cheese.*bread.*spoon"

If comma is essentially there after your random string then,

grep 'cheese, bread [^,]*, spoon [^,],'

or, if that random string it alphanumeric only,

grep 'cheese, bread [a-zA-Z0-9]*, spoon [a-zA-Z0-9]*,'