🐧5 best CLI tools for finding words using regular expressions

This guide introduces some of the best command line tools that are used to find matching strings or patterns in text files.

These tools are usually used in conjunction with regular expressions (REGEX for short), which are unique strings to describe a search pattern.

Without further ado, let’s get down to business.

1. Grep command

First of all comes the utility tool grep, which is an acronym for Global Regular Expression Print, a powerful command line tool that comes in handy when searching for a specific string or pattern in a file.

Grep comes by default on modern Linux distributions and gives you the ability to return different search results.

You can perform a wide variety of functions with grep, such as:

  • Search for strings or matching patterns in the file.
  • Search for strings or matching patterns in Gzip files.
  • Count the number of string matches.
  • Print line numbers containing a string or pattern.
  • Recursive search for a string in directories.
  • Perform a reverse search (that is, display results for rows that do not match your search criteria).
  • Ignore case sensitivity when searching for strings.

The syntax for using the grep command is pretty simple:

$ grep pattern FILE

For example, to find the string “itsecforu” in a file, say hello.txt, ignoring case, run the command:

$ grep -i itsecforu hello.txt

🐧5 best CLI tools for finding words using regular expressions

2. sed command

Sed, short for Stream Editor, is another useful command line tool for manipulating text in a text file.

Sed searches, filters, and replaces lines in a given file in a non-interactive way.

By default, the sed command prints the output to STDOUT (standard output), implying that the output is written to the terminal rather than being saved to a file.

The Sed command is invoked as follows:

$ sed -OPTIONS command [ file ]

For example, to replace all instances of “Unix” with “Linux”, issue the command:

$ sed 's/Unix/Linux' hello.txt

If you want to redirect the output rather than display it on the terminal, use the redirection sign (>) as follows:

$ sed 's/Unix/Linux' hello.txt > output.txt

The output of the command will be saved in the file output.txt, rather than being printed to the screen.

Check the man pages again for other options that you can use.

$ man sed

3. Team Ack

Ack is a fast and portable command line tool written in Perl.

Ack is considered a handy replacement for grep and displays results in a visually appealing form.

The Ack command searches a file or directory for strings that match the search criteria.

Then it highlights the corresponding line.

Ack has the ability to distinguish between files based on their extensions and, to a certain extent, the content in the files.

Ack command syntax:

$ ack [options] PATTERN [FILE...]
$ ack -f [options] [DIRECTORY...]

For example, to find the word itsecforu, run:

$ ack itsecforu hello.txt

To install ask on your system, run the command:

$ sudo apt install ack-grep    [На Debian/Ubuntu]
$ sudo dnf install ack-grep    [На CentOS/RHEL]

4. Team Awk

Awk is a complete scripting language, word processing and data manipulation tool.

It looks for files or programs that contain a search pattern.

When a string or pattern is found, awk performs the action on the match or string and prints the results to STDOUT.

The AWK pattern is enclosed in curly braces, and the entire program is enclosed in single quotes.

Let’s take the simplest example.

Suppose you are displaying your system date as shown below:

$ date

🐧5 best CLI tools for finding words using regular expressions

Suppose you only want to display the first value, that is, the day of the week. In this case, pipe the output to awk as shown below:

$ date | awk '{print $1}'

To display subsequent values, separate them with a comma, as shown below:

$ date | awk '{print $1,$2}'

The above command will display the day of the week and date.

5. Silver Searcher

Silver Searcher is a cross-platform open source code search tool similar to ack but with a focus on speed.

It makes it easy to find a specific string in files in no time at all:

$ ag OPTIONS search_pattern /path/to/file

For example, to find the string “Linux” in hello.txt, run the command:

$ ag Linux hello.txt

For additional options:

$ man ag

Sidebar