Americas

  • United States

How to use the Grep command

Overview

In this Linux tip, we’ll look at how you can use the grep command to look for more than one value in a single command. This can be useful when you want to select lines of two forms, but ensure they will appear in the same order as they do in the file. To separate the terms, specify them in a grep command of this form: $ grep 'pattern1\|pattern2' filename Here’s an example: $ grep 'Date:\|total:' July_sales The command will grab each line that contains one of these strings and display them like this: $ grep 'Date:\|total:' July_sales Date: July 6 total: $3,492 Date: July 13 total: $4,321 Date: July 20 total: $6,001 Date: July 27 All other lines will be ignored. Note that additional terms can be added using the same separator (\|) as needed and the strings can contain more than one word. Here’s an example of that: $ grep 'empty saying\|never falter' sayings They never falter. Is that an empty saying? Closing: That’s your Linux tip for the grep command to search for multiple strings in a file. If you have questions or would like to suggest a topic, please add a comment below. And don’t forget to subscribe to the InfoWorld channel on YouTube.

Register Now