How do I search multiple strings or words using the grep command? For example I’d like to search word1, word2, word3 and so on within /path/to/file. How do I force grep to search multiple words? How can I grep for multiple patterns on Linux, OS X, FreeBSD, or Unix-like system? The grep command supports regular expression pattern. To search multiple words, use following syntax:

Fantastic !! I had a scenario where in I had to pick up lines from a log file based on a word. Unfortunately I had 1000 such words and so it would have been difficult to find 1000 lines. But this solution really helped. Thanks.

I have a requirement in which I would need to grep/find a line based on matching 3 different patterns. I know we can grep with -E multiple parameters seprated by pipe but this work Pipe (|) as OR condition. My requirement is I want to use AND condition. It should show line where it satisfy both the parameter. e.g. line in a file as — 10-Aug-2010 Hello, this is a new example for unix. I need to show this line only when my grep command matches all 3 words this, new, unix How to right such grep command ?

That won’t work if you need to check if word 1 and word2 are anywhere in the file, because the first grep returns the matching line, so the second grep would only match if both words were on the same line.

First: grep is a single line utility. And because “5)” is on two lines, grep will find them, because it walks down the lines, and matches them to your rules, period. Second: what grep finds, that ‘entire’ line will be displayed. So it is not possible to cut the “5)” from the beginning of the line, and display the rest. If you want to find “ram” and after that the next “5)”, you need to use some utility that allows you to implement some logic. Like awk. However if you can ensure that “5)…” will be after the found line – like “ram” – then you can use the ‘after context’ feature of grep, whic displayes the matching lines PLUS some line after that.

Great info site. I have a need to search a file looking for dates and a string, For example: (Dec 2 13:25:27 name local5:warn|warning vmdaemon[180412]: #415 Moved volume tape_1 #0055 (12345678) (abc123 from online to offline.). I need to search for a date and the “online to offline” string together. Also, with grep is it possible to do a date range in the search as oppose to a single date?

I have some fairly large data files I need to search for certain conditions. Easy enough because I know the string I want to search for. However, the problem arises when someone duplicates the string in the field that I am searching for which expands the data criteria past my grep. here’s example:

String = “000/C///” Field delimiter is pipe so I use: grep -i “|000/C///|” to match the condition. Now what I have found some people doing is using multiple strings in that field where only one exists. Is there a way to search for multiples of the same string without having to do a ton of greps in a row?

I have a file “nynorsk-utf-8.txt” in UTF-8 that is supposed to be in Nynorsk, one of the two official dialects of Norwegian. But it contains some “contamination” of Bokmal, the other official dialect. To try to identify the sections of Bokmal, I want to find all lines in nynorsk-utf-8.txt that contain any one of a set of 13 short words that are exclusive to Bokmal:

ikke|jeg|fra|en|et|de|mye|hun|noen|se|selv|sÃ¦rlig|uke

I’m on OS X, using egrep (GNU grep) 2.5.1 I’ve tried the following:

egrep -in “\” nynorsk-utf_8.txt > out.txt

using \ to enforce word boundaries. Yet the output contains lines that do not contain any of these 13 words (as stand-alone words). I also tried

I have two text files file one contains many words (single word per line) and file two contains many strings per line . Now I want to Grep every line from file2 which contains file1 word. Could any one help me out ?

In the above script i want to get the keyword and once i get the keyword I need to get the count of keyword.So i tried with the above script .it is showing the count first keyword only.Can anyone suggest me to resolve the problem.

I want to use grep command for multiple patterns in a file and print a number of lines after each match into a new file. Say in the command $grep ‘Well\|MultiLayer\|Prediction’ PredResult.prn >List.txt

– after ‘Well’ match I want to print 100 lines starting with pattern ’01/’, – after ‘MultiLayer’ match I do not want to print any line, – after ‘Prediction’ match I want to print 100 lines starting with pattern ’01/’.

Maybe stupid question but I can’t understand WHY this one works at all:

$ grep 'warning\|error\|critical' /var/log/messages

Shouldn’t the \ mean it will escape the | and treat it as a string value of ‘|’? It works and treats | as the regular expression OR (alternation) function, yes, but it’s driving me crazy. I am thinking I should just egrep everything if I plan to use regular expressions because that usage makes no sense to me and the egrep usage does.

Enter the command grep ‘if ’ bashrc. This will find every line in the file that contains an if followed by a blank space. How many lines did the statement find? Were any of the lines not actually if statements? (comments start with a #). You can obtain just the number of matches using grep –c. Repeat the command using –c to make sure you counted correctly!