This question already has answers here:
Why do you pass
-c? That will just show the number of matches. Similarly, there is no reason to use
-r. I suggest you read
To grep for 2 words existing on the same line, simply do:
grep "word1" FILE | grep "word2"
grep "word1" FILE will print all lines that have word1 in them from FILE, and then
grep "word2" will print the lines that have word2 in them. Hence, if you combine these using a pipe, it will show lines containing both word1 and word2.
If you just want a count of how many lines had the 2 words on the same line, do:
grep "word1" FILE | grep -c "word2"
Also, to address your question why does it get stuck : in
grep -c "word1", you did not specify a file. Therefore,
grep expects input from
stdin, which is why it seems to hang. You can press Ctrl+D to send an EOF (end-of-file) so that it quits.
Search Multiple Words / String Pattern Using grep Command on , Finally, try on older Unix shells/oses: grep -e pattern1 -e pattern2 *.pl. Grep searching two words in a line. Here are all other possibilities for grep How to grep for two or more words in a line at the same time I have a file that has multiple lines separated by an asterisk as a delimiter: FILE.txt A*123*BCD*456*TOM A*789*EFG*947*CHRIS A*840*BCD*456*TOM I would like to search multiple files for the strings 'BCD' AND 'TOM' and return the number of lines, per file, that match these two reg
One simple rewrite of the command in the question is:
grep "word1" logs | grep "word2"
grep finds lines with 'word1' from the file 'logs' and then feeds those into the second
grep which looks for lines containing 'word2'.
However, it isn't necessary to use two commands like that. You could use extended
grep -E or
grep -E 'word1.*word2|word2.*word1' logs
If you know that 'word1' will precede 'word2' on the line, you don't even need the alternatives and regular
grep would do:
grep 'word1.*word2' logs
The 'one command' variants have the advantage that there is only one process running, and so the lines containing 'word1' do not have to be passed via a pipe to the second process. How much this matters depends on how big the data file is and how many lines match 'word1'. If the file is small, performance isn't likely to be an issue and running two commands is fine. If the file is big but only a few lines contain 'word1', there isn't going to be much data passed on the pipe and using two command is fine. However, if the file is huge and 'word1' occurs frequently, then you may be passing significant data down the pipe where a single command avoids that overhead. Against that, the regex is more complex; you might need to benchmark it to find out what's best — but only if performance really matters. If you run two commands, you should aim to select the less frequently occurring word in the first
grep to minimize the amount of data processed by the second.
The initial script is:
grep -c "word1" | grep -r "word2" logs
This is an odd command sequence. The first
grep is going to count the number of occurrences of 'word1' on its standard input, and print that number on its standard output. Until you indicate EOF (e.g. by typing Control-D), it will sit there, waiting for you to type something. The second
grep does a recursive search for 'word2' in the files underneath directory
logs (or, if it is a file, in the file
logs). Or, in my case, it will fail since there's neither a file nor a directory called
logs where I'm running the pipeline. Note that the second
grep doesn't read its standard input at all, so the pipe is superfluous.
With Bash, the parent shell waits until all the processes in the pipeline have exited, so it sits around waiting for the
grep -c to finish, which it won't do until you indicate EOF. Hence, your code seems to get stuck. With
Heirloom Shell, the second
grep completes and exits, and the shell prompts again. Now you have two processes running, the first
grep and the shell, and they are both trying to read from the keyboard, and it is not determinate which one gets any given line of input (or any given EOF indication).
Note that even if you typed data as input to the first
grep, you would only get any lines that contain 'word2' shown on the output.
At one time, the answer used:
grep -E 'word1.*word2|word2.*word1' "$@"
grep 'word1.*word2' "$@"
This triggered the comments below.
Grepping for two strings that MUST exist on the same line, Trying to find a way to grep for two names on a line. Both names must appear on the same line so '|' / OR is out. So far, I'm just messing around and I've got find . Trying to find a way to grep for two names on a line. Both names must appear on the same line so '|' / OR is out. So far, I'm just messing around and I've got find . -name "*" | xargs grep "Smith" Let me explain. I'm at a top level and need to know all the names of the files that contain the string.
you could use awk. like this...
cat <yourFile> | awk '/word1/ && /word2/'
Order is not important. So if you have a file and...
a file named , file1 contains:
word1 is in this file as well as word2
word2 is in this file as well as word1
word4 is in this file as well as word1
word5 is in this file as well as word2
/tmp$ cat file1| awk '/word1/ && /word2/'
will result in,
word1 is in this file as well as word2
word2 is in this file as well as word1
yes, awk is slower.
How do I grep for lines containing either of two words, but not both , pat2 , or the same with pat1 and pat2 reversed. Grep for regex pattern, sort as unique, and count the number of lines result=$(grep -oE Only one of the words exists if the result is < 2 ((result > 0)) && if (($result < 2)); then For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant.
The main issue is that you haven't supplied the first grep with any input. You will need to reorder your command something like
grep "word1" logs | grep "word2"
If you want to count the occurences, then put a '-c' on the second grep.
grep multiple words any order (AND, not OR) single line, from many , Actually I did come across piping early in my search, but because I'm looking for both words being in the same line, that didn't work. Code: grep egrep is the same as grep -E. fgrep is the same as grep -F. rgrep is the same as grep -r. Direct invocation as either egrep or fgrep is deprecated, but is provided to allow historical applications. that rely on them to run unmodified.
You cat try with below command
cat log|grep -e word1 -e word2
text processing - Grep searching two words in a line, "Both on the same line" means "'rice' followed by random characters followed by 'lemon' or the other way around". In regex that is rice.*lemon To grep for 2 words existing on the same line, simply do: grep "word1" FILE | grep "word2" grep "word1" FILE will print all lines that have word1 in them from FILE, and then grep "word2" will print the lines that have word2 in them. Hence, if you combine these using a pipe, it will show lines containing both word1 and word2. [PDF] grep, awk and sed – three VERY useful command-line utilities Matt , Beginning at the first line in the file, grep copies a line into a buffer, compares it This is useful if you are searching through multiple files for the same string. like so: grep -l "boo" * edit the existing file in place you should use the '-i' flag:. In its simpest form, grep can be used to match literal patterns within a text file. This means that if you pass grep a word to search for, it will print out every line in the file containing that word. Let's try an example. We will use grep to search for every line that contains the word "GNU" in the GNU General Public License version 3 on an Grep OR - Grep AND - Grep NOT, Examples of GREP, EGREP, AWK, SED. GREP OR, GREP AND: match multiple patterns. GREP NOT: print the lines, that do not match a pattern The grep command is used to search text. It searches the given file for lines containing a match to the given strings or words. It is one of the most useful commands on Linux and Unix-like system. Let us see how to use grep on a Linux or Unix like system. Adblock detected 😱 My website is made possible by displaying online advertisements to
A large collection of Unix/Linux 'grep' command examples , Linux grep FAQ: Can you share some Linux/Unix grep command examples? containing the string 'StartInterval' grep -il StartInterval *.plist # same thing, all lines matching multiple patterns locate -i calendar | grep Users | egrep A lot of times I know that the string "foo" exists in a file somewhere in my
Comments Possible duplicate of How to use grep to match string1 AND string2? When you're confused, the man pages are pretty much the last place you want to go for clarification. They're more confusing than randomly guessing. @TotalFrickinRockstarFromMars: I disagree. It's true that in the beginning they might seem confusing, but once you get accustomed to the format using them is pretty straightforward. Anyway, I included it in the answer more for the "teach a man how to fish" bit, I expected the OP doesn't know them, and man pages can get pretty handy. @houbysoft Then we'll have to agree to disagree. I've been using Linux and friends for the better part of 8 years, and I'd still rather google than use man pages. @TotalFrickinRockstarFromMars: Well, I'm not denying the use of that. Anyway, could you point to some specific thing you find "confusing" in the grep man page, for example? @geneorama Sure, but maybe the people who wrote the utlities to begin with would write better man pages and it wouldn't be an issue. The man pages are written for people who already know the tool and just need a little reminder. They're not written for people trying to figure out what they're doing. What is the use of "$@" can you explain. You have not mention any file name. @PrabhatKumarSingh: Inside a shell script,
"$@" expands to all the arguments passed to the shell script (that haven't been shifted away). It could be a list of file names or it could be empty, in which case
grep will read from standard input. The original code in the question doesn't mention any file names either. It will read from standard input, therefore.
ok I understand what $@ means in shell script but I have not seen script mentioned in your answer that's why got confused. Plus 1 for a more efficient way that doing grep grep. Another positive thing of this solution is that it works if both words are the same, that means it can also detect whether a word is repeated in a line. The accepted solution doesn't handle this case. +1. useless use of
A single Awk is still likely to be faster than two separate
grep processes. (But of course the extra useless
cat process would more or less nullify that difference.)
These commands search for at least one word, not for all. and the cat | is unnecessary, you can give the file as grep last argument