
Which gives the output: grep and wildcardsĪs you can see, the command has searched for and is now showing the results for both files “demofile.txt” and “demofile02.txt”. Here are a few ways you can use the grep command for advanced string searches.įor example, did you know you can use the grep command with wildcards? Let’s consider this command: $ grep word demofile* Tips and tricks for using the GREP command to find stringsĭespite its overall simplicity, the grep command is extremely powerful. Now let’s use what we learned to search for the strings “file” and “words” in the two files: “demofile.txt” and “demofile02.txt”: $ grep -E 'file|word' demofile.txt demofile02.txtĪnd the output is: Using GREP to Find Multiple Strings in Multiple Files #4. The -E option that we entered treats the following pattern as an extended regular expression, which allows us to search for multiple strings. This is the syntax that you will have to use: $ grep -E 'pattern1|pattern2|pattern3|.' filename1 filename2 filename3. You can also use grep to find multiple strings in single or multiple files. Using GREP to find multiple strings in multiple files Here we are searching for the string “words” in the files “demofile.txt” and “demofile02.txt”: $ grep words demofile.txt demofile02.txtĪnd the output is: Using GREP to Find the Same String in Multiple FilesĪs you can see, the grep command will not only show you the matched result but also conveniently label which file it’s from. To search through multiple files for the provided string, all you need to do is append the command with all the filenames. Using GREP to find the same string in multiple files
#Linux search files for text string how to
To ignore the case, you’ll need to enter this command instead: $ grep -i this demofile.txt How to ignore case in grep commandĪs you can see now, even though the provided string is “this,” the command matches for “This” and prints that answer. As such, you should remember that the grep command is case-sensitive. It’s only when you enter “This” that you get a match. Note how you used the string “this” first, and it didn’t print anything. $ grep This demofile.txtĪnd the output is: grep is case-sensitive Let’s see if it works by searching for the string “This” in the “demofile.txt” file. If the specified string occurs in multiple lines, it will print all of them. Which gives the output: Using GREP to Find a String in One FileĪs you can see, the command fetches the entire line containing the provided string. So, let’s say if you want to search for the word “demonstration” in the file “demofile.txt,” you will need to use this command: $ grep demonstration demofile.txt You will need to replace the expression “string” with the “regular expression” you want to use and the “filename” with the name of the file in which you want to search for the string. Here is the syntax for using the grep command to find a string in a file: $ grep string filename Now that you have an idea of what the grep command is, let’s see how we can use it. And “Print” signifies that it will print the search results as soon as it finds a match based on the provided regular expression. “Regular Expression” is what we call the text search pattern that we will use with the command. It’s “Global” because it can search your entire Linux system. GREP is short for Global Regular Expression Print. Finding strings in a file via the command line (GREP method) This second file will help to provide some diversified examples. This one is similar to the last one as it contains three lines with some words.

This contains a bunch of words to create sentences that finally end with a period.Ĭontent of demofile02.txt: This is another demo file that I'll use for this demonstration. Content of demofile.txt: This is a demo file that I have created for demonstration purposes.
