9

Say I have a file that contains:

Release 2.1 OS: RHEL File: package_el6_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el6_2.0.1.1_i686.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_i686.rpm

I want to grep and match lines that only contain 'package', 'el6', and 'x86_64'

How would I go about doing that on a one liner using grep? The line must match all three and grep shouldn't care about how many characters are in between. If there is a better tool for the job, I'm happy to use that instead.

I've tried the following and got no results:

grep package*el6*x86_64*

Seeing posts and documentation, I understand that * does not mean the same thing as it would in shell. I'm looking for the equivalent of it to use in regex. Hope this makes sense.

12
  • Have you tried anything at all? What is the output of the thing you tried? Aug 31, 2016 at 19:28
  • @MadPhysicist I have, but I'm having a lot of trouble finding a way to insert a wildcard for any number of characters. Didn't find much googling and reading the man page.
    – Mike D
    Aug 31, 2016 at 19:31
  • That's fine. Post your attempts and what went wrong here. You are more likely to get constructive help that way. As it stands, your question shows zero effort on your part. Aug 31, 2016 at 19:43
  • @MadPhysicist grep package*el6*x86_64* Reading through some documentation and posts, I understand that * does not mean the same in regex. But I don't see anything that I can use to replace it with.
    – Mike D
    Aug 31, 2016 at 19:48
  • Please edit your question with this information by clicking the "edit" button immediately under the question. Aug 31, 2016 at 19:50

4 Answers 4

12

Your attempt is very close. * in shell glob terms is roughly equivalent to .* in regex terms. . means "any character" and * is means "repeated any number of times (including zero).

Your regex just needs . before each *. The trailing * is not necessary:

package.*el6.*x86_64

Here is a sample run with your input:

grep 'package.*el6.*x86_64' <<< "Release 2.1 OS: RHEL File: package_el6_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el6_2.0.1.1_i686.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_x86_64.rpm
Release 2.1 OS: RHEL File: package_el7_2.0.1.1_i686.rpm"

Prints:

Release 2.1 OS: RHEL File: package_el6_2.0.1.1_x86_64.rpm
5
  • 2
    Needs more quotes to prevent any possibility of the shell treating that argument as a glob. Aug 31, 2016 at 19:59
  • 4
    Specifically, it should be: grep 'package.*el6.*x86_64'. Otherwise, the shell can treat the operation as a failure without even running grep (if the failglob option is set), or replace the expression with a list of filenames in the current directory which match the regex when interpreted as a glob, or delete that argument outright (if the nullglob option is set). Aug 31, 2016 at 20:00
  • Thanks for your help. Exactly what I needed.
    – Mike D
    Aug 31, 2016 at 20:00
  • @CharlesDuffy. My mistake. Fixed now. Aug 31, 2016 at 20:00
  • @CharlesDuffy thank you for your contribution as well. I'll be sure to use the quotes.
    – Mike D
    Aug 31, 2016 at 20:02
4

Not the best solution (in fact ineficient), but really easy to remember: join 3 greps

grep "package" | grep "el6" | grep "x86_64"
2
  • No, if the order isn't guaranteed just use awk instead of a chain of greps with pipes: awk '/package/ && /el6/ && /x86_64/' file.txt
    – Ed Morton
    Sep 1, 2016 at 3:54
  • Oops, you are right, my answer is not exactly what were asking for Sep 1, 2016 at 8:30
0

If they're guarenteed to be in order, then a simple grep:

grep "package.*el6.*x86_64" file.txt

would do it. If the items can be in any order, you can try a pipe:

cat file.txt | grep package | grep el6 | grep x86_64

will only show lines containing all three, but in any order

2
  • 1
    grep package file.txt | grep el6 | grep x86_64 is an alternative for the last one. Aug 31, 2016 at 20:25
  • If the order isn't guaranteed just use awk instead of a chain of greps with pipes: awk '/package/ && /el6/ && /x86_64/' file.txt.
    – Ed Morton
    Sep 1, 2016 at 3:53
-3

You can use egrep

egrep 'package|el6|x86_64' name_of_file.txt
4
  • 1
    This matches either, not all three. Aug 31, 2016 at 19:42
  • Are you trying to find files that have all three strings in it? Something like: grep -l package name_of_file.txt | xargs grep -l e16 | xargs grep -l x86_64 ?
    – connollyc4
    Aug 31, 2016 at 20:02
  • 2
    @connollyc4, reread the question -- it's very explicit that all three strings need to be present. Aug 31, 2016 at 20:11
  • Try it. You can use the command in my answer so you don't have to type from scratch Aug 31, 2016 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.