Combining multiple greps


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Combining multiple greps
# 1  
Old 02-22-2019
Combining multiple greps

I'm trying to learn about regular expressions. Let's say I want to list all the files in /usr/bin beginning with "p", ending with "x", and containing an "a".
I know this works:
Code:
ls | grep ^p | grep x$ | grep a

but I'm thinking there must be a way to do it without typing grep three times. Some of my attempts:
Code:
ls | grep '^p' 'x$' 'a'
ls | grep ^p | x$ | [a]
ls | grep -E "[[ ^p ]]+[[ x$ ]]+[[ a ]]"

Ubuntu 18.04.2; Xfce 4.12.3; kernel 4.15.0-45-generic; bash 4.4.19(1); Dell Inspiron-518
# 2  
Old 02-22-2019
Try this...
Code:
ls | grep "^p.*a.*x$"

.* is for any character, 0 to any number of occurrances.

So the task is to create one combined regular expression that matches all criteria you want.

Last edited by stomp; 02-23-2019 at 06:55 AM..
This User Gave Thanks to stomp For This Post:
# 3  
Old 02-22-2019
Quote:
Originally Posted by Xubuntu56
I know this works:
Code:
ls | grep ^p | grep x$ | grep a

but I'm thinking there must be a way to do it without typing grep three times. Some of my attempts:[CODE]
You are right and there is - in fact there are two ways:

First, you can use several expressions in grep at once by using the -e-switch:

Code:
grep -e "one" -e "two" /some/file

will list all lines containing "one" AND all lines containing "two" from that file. It is in fact a logical OR for these two expressions.

The second possibility (and this is probably what you wanted) is to combine regular expressions. i.e. to get all lines containing "one" and "two" in that order you would write:

Code:
grep 'one.*two' /some/file

The regexp means: "one", followed by anything (".*"), followed by "two". If the order of the two words should not matter you need two regexps, which you can combine with the method above:

Code:
grep -e 'one.*two' -e 'two.*one' /some/file

Search for "'one', something, then 'two'" or for "'two', something, then "one"'.

You can also use sed (the stream editor) for such (or even more complex) purposes where grep might get a bit unwieldy: sed -n will only print the lines you explicitly print (the default is to print every line after it is processed, which includes passing it through unchanged) therefore:

Code:
sed -n '/one/p' /some/file

basically is the same as

Code:
grep 'one' /some/file

But you can nest certain rules in sed which grep cannot do. i.e.

Code:
sed -n '/one/ {
             /two/ {
                   /three/p
             }
         }' /some/file

Would be similar to the example above: print only the lines containing "one", "two" and "three" but in any order.

I hope this helps.

bakunin

Last edited by bakunin; 02-22-2019 at 10:46 AM.. Reason: typo
These 4 Users Gave Thanks to bakunin For This Post:
# 4  
Old 02-22-2019
@stomp--ls | grep "^p.*a.*x$" works great, with either single or double quotes. I'm used to thinking of the asterisk as a wildcard, which was confusing me at first.
@bakunin--this works great

Code:
sed -n '/^p/ {
             /x$/ {
                  /a/p
                  }
             }' catalogue

# 5  
Old 02-22-2019
Not really related to the question, but:
Code:
ls -d p*a*x

These 4 Users Gave Thanks to Scrutinizer For This Post:
# 6  
Old 02-23-2019
@Scrutinizer--it does relate to the question, as it performs the task. It found the target file, partx, in /usr/bin.


I don't understand why, however, as the man page for ls says the -d option is to "list directories themselves, not their contents".
# 7  
Old 02-23-2019
Normally ls lists the contents of a given directory.
Say there would be a pax/ subdirecory beside the partx file,
ls p*a*x would list partx and the contents of the pax/
While ls -d p*a*x would list the two items partx and pax.
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combining multiple files into one

Hello Everyone, I have 4 different files (one column in each) that I'm trying to combine into 1 file with four columns. Having issues trying to get the columns to format properly. I have tried the following: paste file1 file2 file3 file4 | column -s $'\t' -t > results.txt paste file1 file2... (1 Reply)
Discussion started by: malk71
1 Replies

2. Shell Programming and Scripting

Combining multiple seds into one awk

i'm trying to optimize my script. i have a lot of instances where i'm doing something like this: echo $blah | sed 's~ ~|~g' | sed 's-_space_- -g' | sed 's-_LP_-(-g' | sed 's-_RP_-)-g' obviously, this is very inefficient. i know i can combine into one sed command with something this, using... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

Combining multiple files

I have 2 files. each having 3 coloums 1st field date as 20130322 2nd field time as 05:55 3rd field numberic value File 2 has entries missing for some date time. FILE1 20130322 05:35 2219 20130322 05:40 1809 20130322 05:45 1617 20130322 05:50 ... (2 Replies)
Discussion started by: sandeepkmehra
2 Replies

4. Shell Programming and Scripting

Bash Scipting (New); Run multiple greps > multiple files

Hi everyone, I'm new to the forums, as you can probably tell... I'm also pretty new to scripting and writing any type of code. I needed to know exactly how I can grep for multiple strings, in files located in one directory, but I need each string to output to a separate file. So I'd... (19 Replies)
Discussion started by: LDHB2012
19 Replies

5. Shell Programming and Scripting

combining multiple sed statements

I need to run a cronjob that will monitor a directory for files with a certain extension, when one appears I then need to run the below scripts How do I go about combining the following sed statements into one script? and also retain the original filename.? sed 's/71502FSC1206/\n&/g' # add a... (2 Replies)
Discussion started by: firefox2k2
2 Replies

6. Shell Programming and Scripting

Combining multiple variables into new variable

Hello, I am a new joiner to the forum, and have what i hope is a simple question, however I can't seem to find the answer so maybe it is not available within bash scripting. I intend to use the below script to archive files from multiple directories at once by using a loop, and a variable (n)... (10 Replies)
Discussion started by: dring
10 Replies

7. Shell Programming and Scripting

Combining multiple files into one with the same name/different extension

I've been trying to find information in regard to creating a script that will generate HTML files. I currently have a series of files that contain code I need to surround with a <textarea> tag for easy viewing. I have about a thousand files that contain code, one file that contains the HTML code up... (10 Replies)
Discussion started by: 12o
10 Replies

8. Shell Programming and Scripting

Combining multiple commands

Hi Guys, I am looking to optimze these 5 SSH lines to a single SSH to get my machine to not hang! lol! cat hosts.lst | xargs -n1 -t -i echo 'home/util/timeout 6 0 ssh -q {} top -b > util/{}.top &' >> r_query_info cat hosts.lst | xargs -n1 -t -i echo 'home/util/timeout 6 0 ssh -q {} uname -r... (5 Replies)
Discussion started by: wick3dsunny
5 Replies

9. Shell Programming and Scripting

Sed: Combining Multiple Lines into one

Before I ask my actual question, is it going to be a problem that I want to run this process on a 15 Gig file that is ~140 million rows? What I'm trying to do: I have a file that looks like Color,Type,Count,Day Yellow,Full 5 Tuesday Green,Half 6 Wednesday Purple,Half 8 Tuesday ...... (3 Replies)
Discussion started by: goldfish
3 Replies

10. Shell Programming and Scripting

Combining multiple lines

I am fairly new to scripting. But I have been able to extract and format all of my information required into one file. My issue is that one character is on a separate line. I need to be able to add the character to the previous line. ex. abcdefghi 1 bcdefghij 3 cdefghijk 4 need to... (4 Replies)
Discussion started by: DUST
4 Replies
Login or Register to Ask a Question