DEV Community

Vidyasagar SC Machupalli
Vidyasagar SC Machupalli

Posted on • Updated on

AND in grep

While working on a shell or drafting scripts, two things I love the most is grep and awk. Let's start with some definitions,

The grep utility shall search the input files, selecting lines matching one or more patterns; the types of patterns are controlled by the options specified. Source: Opengroup pubs

and

The awk utility executes programs written in the awk programming language, which is specialised for textual data manipulation. An awk program is a sequence of patterns and corresponding actions. When an input is read that matches a pattern, the action associated with that pattern will be carried out. Source: Opengroup pubs

So, what is with AND in grep?

This week, I was exploring the IBM Cloud CLI infrastructure-service plugin to manage the VPC resources. In once of the shell scripts, I wanted to pull the available Ubuntu-18.04 image for the Virtual Server Instance(VSI). So, I ran the below command to see a list of available and deprecated images

ibmcloud is images
Enter fullscreen mode Exit fullscreen mode

Alt Text

This is just a subset of images.

All I wanted is an Ubuntu 18.04 image what is currently available with amd64 architecture. So, I wrote a shell command using grep and awk

ibmcloud is images | grep -E 'available.*ubuntu-18.*amd64' | awk '{print $2}'
Enter fullscreen mode Exit fullscreen mode

-E matches using extended regular expressions.

The output is
Alt Text

This is using Grep AND using -E ‘pattern1.*pattern2’ format. You can also use multiple greps.

For more information on using AND, OR and NOT with grep, refer to this interesting article

Top comments (0)