banner



How To Find A Pattern In A File In Unix

Introduction

This guide details the near useful grep commands for Linux / Unix systems.

Later going through all the commands and examples, you volition learn how to use grep to search files for a text from the terminal.

header image for grep commands in linux

Prerequisites

  • Linux or UNIX-like organization
  • Access to a terminal/command line
  • A user with permissions to access the desired files and directories

Note: A line does not represent a line of text as viewed on the terminal screen. A line in a text file is a sequence of characters until a line intermission is introduced. The output of grep commands may contain whole paragraphs unless the search options are refined.

What is the grep Control?

Grep is an acronym that stands for 1000lobal Regular Expression Print.

Grep is a Linux / Unix command-line tool used to search for a cord of characters in a specified file. The text search blueprint is called a regular expression. When it finds a friction match, it prints the line with the upshot. The grep control is handy when searching through large log files.

Using the grep Command

The grep command consists of 3 parts in its most basic form. The first office starts with grep , followed by the pattern that you are searching for. Later on the cord comes the file proper name that the grep searches through.

The simplest grep command syntax looks like this:

grep simple syntax example

The command can contain many options, pattern variations, and file names. Combine equally many options as necessary to become the results you demand. Beneath are the most mutual grep commands with examples.

Notation: Grep is instance-sensitive. Make sure to use the correct case when running grep commands.

To Search a File

To impress any line from a file that contains a specific pattern of characters, in our case phoenix in the file sample2 , run the command:

          grep phoenix sample2        

Grep will display every line where there is a lucifer for the word phoenix . When executing this command, you practice not get verbal matches. Instead, the terminal prints the lines with words containing the string of characters you entered. Here is an example:

 example of grep command words containing the string of characters you entered

Tip: If your search design includes characters other than alphanumeric, utilize quotation marks. This includes bare spaces or whatever symbol.

To Search Multiple Files

To search multiple files with the grep command, insert the filenames you want to search, separated with a space character.

In our case, the grep command to match the word phoenix in three files sample , sample2 , and sample3 looks similar this example:

          grep phoenix sample sample2 sample3        

The terminal prints the name of every file that contains the matching lines, and the actual lines that include the required string of characters.

example of searching multiple files with grep command

You can append equally many filenames equally needed. The terminal prints a new line with the filename for every match it finds in the listed files.

Tip: Refer to our article Xargs Commands to learn how to use xargs with grep to search for a string in the list of files.

Search All Files in Directory

To search all files in the current directory, use an asterisk instead of a filename at the terminate of a grep command.

In this example, we use goose egg as a search criterion:

          grep nix *        

The output shows the proper noun of the file with nada and returns the entire line.

To Find Whole Words Only

Grep allows y'all to find and print the results for whole words only. To search for the word phoenix in all files in the current directory, append -w to the grep control.

          grep -w phoenix *        

This choice only prints the lines with whole-give-and-take matches and the names of the files it found them in:

example of grep print lines with whole word matches

When -w is omitted, grep displays the search design even if it is a substring of another word.

If you would like to search for multiple strings and word patterns, bank check out our commodity on how to grep for multiple strings, patterns or words.

To Ignore Case in Grep Searches

Every bit grep commands are instance sensitive, one of the most useful operators for grep searches is -i. Instead of printing lowercase results merely, the terminal displays both uppercase and lowercase results. The output includes lines with mixed instance entries.

An case of this command:

          grep -i phoenix *        

If we use the -i operator to search files in the electric current directory for phoenix , the output looks like this:

example of how to use the i operator to search files with grep

To Search Subdirectories

To include all subdirectories in a search, add the -r operator to the grep command.

          grep -r phoenix *        
example of grep including all subdirectories in a search

This command prints the matches for all files in the electric current directory, subdirectories, and the exact path with the filename. In the instance below, we also added the -w operator to bear witness whole words, simply the output form is the aforementioned.

Inverse grep Search

You can utilise grep to print all lines that do not match a specific pattern of characters. To invert the search, append -v to a grep command.

To exclude all lines that incorporate phoenix , enter:

          grep -v phoenix sample        

The concluding prints all lines that practice not incorporate the word used as a search criterion. Use -i to ignore case to exclude completely the word used for this search:

example of excluding lines in grep

To Show Lines That Exactly Match a Search String

The grep control prints entire lines when it finds a match in a file. To impress just those lines that completely match the search string, add the -x option.

          grep -10 "phoenix number3" *        

The output shows only the lines with the verbal friction match. If in that location are whatsoever other words or characters in the same line, the grep does not include it in the search results. Do not forget to use quotation marks whenever at that place is a space or a symbol in a search pattern.

Here is a comparison of the results without and with the -x operator in our grep command:

example of comparison with the x operator in grep command

To Listing Names of Matching Files

Sometimes, y'all only demand to come across the names of the files that contain a word or string of characters and exclude the actual lines. To print only the filenames that match your search, use the -l operator:

          grep -l phoenix *        
example how to find files that contain a word or string of characters excluding the actual lines

The output shows the exact filenames that contain phoenix in the current directory only does non impress the lines with the corresponding word:

Every bit a reminder, use the recursive search operator -r to include all subdirectories in your search.

To Count the Number of Matches

Grep can display the filenames and the count of lines where it finds a lucifer for your word.

Apply the -c operator to count the number of matches:

          grep -c phoenix *        
example of grep counting the total number of matches of a search

To Display the Number of Lines Before or Afterwards a Search String

Sometimes you lot demand more content in search results to determine what is well-nigh relevant.

Use the following operators to add together the desired lines earlier, later on a match, or both:

  • Use -A and a number of lines to display later a match: grep -A 3 phoenix sample - this command prints three lines later the match.
  • Utilise -B and a number of lines to brandish before a match: grep -B 2 phoenix sample - this command prints ii lines before the match.
  • Employ -C and a number of lines to display earlier and after the match: grep -C 2 phoenix sample - this command prints two lines earlier and later the match.

To Display Line Numbers with grep Matches

When grep prints results with many matches, it comes handy to come across the line numbers. Suspend the -n operator to whatever grep command to show the line numbers.

Nosotros will search for Phoenix in the current directory, show two lines earlier and subsequently the matches along with their line numbers.

          grep -north -C 2 Phoenix sample        
example of grep displaying two lines before and after the matches

Limit grep Output to a Fixed Number of Lines

Individual files, such equally log files, can comprise many matches for grep search patterns. Limit the number of lines in the grep output by adding the -k selection and a number to the control.

          grep -m2 Phoenix sample        

In this example, the final prints the first ii matches information technology finds in the sample file.

If yous do not specify a file and search all files in a directory, the output prints the starting time two results from every file forth with the filename that contains the matches.

Conclusion

Now you know how to use the grep command in Linux/Unix.

The grep control is highly flexible with many useful operators and options. By combining grep commands, you tin can go powerful results and detect the text hiding in thousands of files.

Was this commodity helpful?

Yep No

Source: https://phoenixnap.com/kb/grep-command-linux-unix-examples

Posted by: brownnectur.blogspot.com

0 Response to "How To Find A Pattern In A File In Unix"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel