w3resource

Bash Searching for Patterns Using Grep

1.

Search for a specific word in a text file:

Code:

grep "Bash" document.txt

Output:

Bash is a command processor that typically runs in a text window where the user types commands that cause actions.
Bash can also read and execute commands from a file, called a shell script.
Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.

2.

Search for a word in multiple files:

Code:

grep "word" file1.txt file2.txt file3.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep "and" file1.txt file2.txt file3.txt
file2.txt:sh is the GNU Project's shell-the Bourne Again SHell. This is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and the C shell (csh).
file2.txt:It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use.
file3.txt:When a computer boots up, a kernel (whether it's Linux, BSD, Mach, or NT) recognizes all the physical hardware and enables each component to talk with one another and be orchestrated by some basic software.
file3.txt:A computer's most basic set of instructions simply keeps it powered on and in a safe state: activating fans periodically to prevent overheating, using subsystems to monitor disk space or "listen" for newly attached devices, and so on. 

Note: No matches found in file1.txt

Explanation:

grep "word" file1.txt file2.txt file3.txt

  • grep: This is a command-line utility for searching plain-text data sets for lines that match a regular expression.
  • "word": This is the string pattern to search for. In this case, it's "word".
  • file1.txt file2.txt file3.txt: These are the filenames where "grep" will search for the specified pattern.

3.

Search for a word ignoring case:

Code:

grep -i "word" filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep -i "Types" document.txt
Bash is a command processor that typically runs in a text window where the user types commands that cause actions.

Explanation:

grep -i "word" filename.txt

  • grep: This is a command-line utility for searching plain-text data sets for lines that match a regular expression.
  • -i: This option makes the search case-insensitive. It means that "grep" will match "word", "Word", "WORD", etc., regardless of case.
  • "word": This is the string pattern to search for. In this case, it's "word".
  • filename.txt: This is the name of the file to search within.

4.

Search for a word recursively in all files in a directory:

Code:

grep -r "word" directory/

Output:

ad@DESKTOP-3KE0KU4:~$ grep -r "Bash" new_dir/
new_dir/file1.txt:This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.
new_dir/file1.txt:This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.
new_dir/file2.txt:In addition, most sh scripts can be run by Bash without modification.

Explanation:

grep -r "word" directory/

  • grep: This is a command-line utility for searching plain-text data sets for lines that match a regular expression.
  • -r: This option tells "grep" to search recursively through directories and their subdirectories.
  • "word": This is the string pattern to search for. In this case, it's "word".
  • directory/: This is the directory where the search will be performed. The trailing slash indicates that "grep" will search within this directory and its subdirectories.

5.

Search for a word and display line numbers:

Code:

grep -n "word" filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep -n "Bash" file1.txt
1:This text is a brief description of the features that are present in the Bash shell (version 5.2, 19 September 2022). The Bash home page is http://www.gnu.org/software/bash/.
3:This is Edition 5.2, last updated 19 September 2022, of The GNU Bash Reference Manual, for Bash, Version 5.2.

Explanation:

grep -n "word" filename.txt

  • grep: This is a command-line utility for searching plain-text data sets for lines that match a regular expression.
  • -n: This option is used to display line numbers along with the matching lines.
  • "word": This is the string pattern to search for. In this case, it's "word".
  • filename.txt: This is the name of the file in which the search will be performed.

6.

Search for lines that do not contain a word:

Code:

grep -v "word" filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep -v "Bash" document.txt
Like most Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration.
The keywords, syntax, dynamically scoped variables and other basic features of the language are all copied from sh.

Explanation:

grep -v "word" filename.txt

  • grep: This is a command-line utility for searching plain-text data sets for lines that match a regular expression.
  • -v: This option is used to invert the match, i.e., it selects all lines that do not contain the specified pattern.
  • "word": This is the string pattern that we want to exclude.
  • filename.txt: This is the name of the file in which the search will be performed.

7.

Search for lines starting with a specific pattern:

Code:

grep "^pattern" filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep "^Bash" document.txt
Bash is a command processor that typically runs in a text window where the user types commands that cause actions.
Bash can also read and execute commands from a file, called a shell script.

Explanation:

grep "^pattern" filename.txt

  • grep: This is a command-line utility for searching plain-text data sets for lines that match a regular expression.
  • ^pattern: The ^ symbol is a regular expression anchor that matches the start of a line. So, ^pattern will match any line that starts with the specified pattern.
  • filename.txt: This is the name of the file in which the search will be performed.

8.

Search for lines ending with a specific pattern:

Code:

grep "pattern$" filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep "extensions.$" document.txt
Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.

Explanation:

grep "pattern$" filename.txt

  • grep: This command searches text using patterns.
  • "pattern$": This part of the command specifies the pattern to search for. The $ symbol is a regular expression anchor that matches the end of a line. So, "pattern$" searches for lines ending with the specified pattern.
  • filename.txt: This is the name of the file in which the search will be performed.

9.

Search for lines containing any of multiple patterns:

Code:

grep -e "pattern1" -e "pattern2" filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep -e "command" -e "shell" document.txt
Bash is a command processor that typically runs in a text window where the user types commands that cause actions.
Bash can also read and execute commands from a file, called a shell script.
Like most Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration.
Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.

Explanation:

grep -e "pattern1" -e "pattern2" filename.txt

  • grep: This command searches text using patterns.
  • -e "pattern1" -e "pattern2": This part of the command specifies multiple patterns to search for. The -e option allows specifying multiple patterns.
  • "pattern1" and "pattern2": These are the patterns to search for. In this case, the command searches for lines containing either "pattern1" or "pattern2".
  • filename.txt: This is the name of the file in which the search will be performed.

10.

Search for lines matching a pattern and count occurrences:

Code:

grep -c "pattern" filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep -c "command" document.txt
3

Explanation:

grep -c "pattern" filename.txt

The above command uses "grep" to search for occurrences of "pattern" in the file 'filename.txt'. The -c option tells "grep" to count the number of lines containing the pattern, rather than printing the lines themselves. So, the command returns the total count of lines in 'filename.txt' where "pattern" appears.

11.

Search for lines containing a word with a specified number of characters:

Code:

grep '\b\w\{5\}\b' filename.txt

Output:

rg@DESKTOP-3KE0KU4:~$ grep '\b\w\{5\}\b' document.txt
Bash is a command processor that typically runs in a text window where the user types commands that cause actions.
Bash can also read and execute commands from a file, called a shell script.
The keywords, syntax, dynamically scoped variables and other basic features of the language are all copied from sh.
Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.

Explanation:

grep '\b\w\{5\}\b' filename.txt

  • grep: This command searches text using patterns.
  • '\b\w\{5\}\b': This is the pattern to search for.
    • \b: This matches a word boundary, ensuring word separation from other characters.
    • \w: This matches any word character (letters, digits, or underscore).
    • \{5\}: This specifies that the previous character (\w) should appear exactly 5 times, making the word 5 characters long.
    • \b: Matching a word boundary, ensuring that the word ends after exactly 5 characters.
  • filename.txt: This is the name of the file in which the search will be performed.

12.

Search for lines containing a word starting with a specific letter:

Code:

grep '\b[Aa]\w*' filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep '\b[Pp]\w*' document.txt
Bash is a command processor that typically runs in a text window where the user types commands that cause actions.
Like most Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration.
Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.

Explanation:

grep '\b[Aa]\w*' filename.txt

  • grep: This command searches text using patterns.
  • '\b[Aa]\w*': This is the pattern to search for.
    • \b: This matches a word boundary, ensuring that the word starts at the beginning of a word.
    • [Aa]: This matches either 'A' or 'a'.
    • \w*: This matches zero or more word characters (letters, digits, or underscore).
  • filename.txt: This is the name of the file in which the search will be performed.

13.

Search for lines containing a word with a specific number of occurrences of a character:

Code:

grep '\b\w*ll\w*\b' filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep '\b\w*ll\w*\b' document.txt
Bash is a command processor that typically runs in a text window where the user types commands that cause actions.
Bash can also read and execute commands from a file, called a shell script.
Like most Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration.
The keywords, syntax, dynamically scoped variables and other basic features of the language are all copied from sh.
Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.

Explanation:

grep: Command for searching text using patterns.

  • '\b\w*ll\w*\b': The pattern to search for.
    • \b: Matches a word boundary, ensuring that the word starts at the beginning of a word.
    • \w*: Matches zero or more word characters (letters, digits, or underscore) before the substring "ll".
    • ll: Matches the substring "ll".
    • \w*: Matches zero or more word characters after the substring "ll".
    • \b: Matches a word boundary, ensuring that the word ends at the end of a word.
  • filename.txt: The file in which the search will be performed.

14.

Search for lines containing a word with a specific range of occurrences of a character:

Code:

grep '\b\w\{3,5\}\b' filename.txt

Output:

rg@DESKTOP-3KE0KU4:~$ grep '\b\w\{10,15\}\b' document.txt
Like most Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration.
The keywords, syntax, dynamically scoped variables and other basic features of the language are all copied from sh.
Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.

Explanation:

grep '\b\w\{3,5\}\b' filename.txt

  • grep: Command for searching text using patterns.
  • '\b\w\{3,5\}\b': The pattern to search for.
    • \b: Matches a word boundary, ensuring that the word starts at the beginning of a word.
    • \w: Matches a word character (letter, digit, or underscore).
    • \{3,5\}: Quantifier that matches the previous character (in this case, \w) between 3 and 5 times.
    • \b: Matches a word boundary, ensuring that the word ends at the end of a word.
  • filename.txt: The file in which the search will be performed.

15.

Search for lines containing a word with a specific pattern using regular expressions:

Code:

grep -E '\b[A-Za-z]{3}[0-9]{2}\b' filename.txt

Output:

ad@DESKTOP-3KE0KU4:~$ grep -E '\b[A-Za-z]{3}[0-9]{2}\b' temp.txt
ABC12 DSFJOEW SDFSD ABC1NJU pqr66 Ko12.

Explanation:

grep -E '\b[A-Za-z]{3}[0-9]{2}\b' filename.txt

  • \b: Represents a word boundary, ensuring that the pattern starts at the beginning of a word.
  • [A-Za-z]: Matches any uppercase or lowercase letter.
  • {3}: Specifies that the previous character class should occur exactly 3 times.
  • [0-9]: Matches any digit.
  • {2}: Specifies that the previous character class should occur exactly 2 times.
  • \b: Represents a word boundary, ensuring that the pattern ends at the end of a word.

Bash Editor:


More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/bash-script-exercises/searching-for-patterns-using-grep.php