DEV Community

Vidyasagar SC Machupalli
Vidyasagar SC Machupalli

Posted on • Edited on

3 1

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

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay