6 Best CLI Tools to Search Plain-Text Data Using Regular Expressions

This guide takes a tour of some of the best command-line tools that are used for searching matching strings or patterns in text files. These tools are usually used alongside regular expressions – shortened as REGEX – which are unique strings for describing a search pattern.

Without much further ado, let’s dive in.

1. Grep Command

Coming in the first place is the grep utility tool – is an acronym for Global Regular Expression Print, is a powerful command-line tool that comes in handy when searching for a specific string or a pattern in a file.

Grep ships with modern Linux distributions by default and gives you the flexibility to return various search results. With grep, you can perform a vast array of functioning such as:

  • Search for strings or matching patterns in a file.
  • Search for strings or matching patterns in Gzipped files.
  • Count the number of string matches.
  • Print the line numbers that contain the string or pattern.
  • Search recursively for the string in directories.
  • Perform a reverse search ( i.e. Display results of strings not matching the search criteria).
  • Ignore case sensitivity when searching for strings.

The syntax for using the grep command is quite simple:

$ grep pattern FILE

For example, to search for the string ‘Linux‘ in a file, say, hello.txt while ignoring case sensitivity, run the command:

$ grep -i Linux hello.txt
Search For String in a File
Search For String in a File

To get more options that you can use with grep, simply read our article that examples more advanced grep command examples.

2. sed Command

Sed – short for Stream Editor – is another useful command-line tool for manipulation text in a text file. Sed searches, filters and replaces strings in a given file in a non-interactive manner.

By default, sed command prints the output to STDOUT (Standard Out), implying that the result of the execution is printed on the terminal instead of being saved in a file.

Sed command is invoked as follows:

$ sed -OPTIONS command [ file to be edited ]

For example, to replace all instances of ‘Unix‘ with ‘Linux‘, invoke the command:

$ sed 's/Unix/Linux' hello.txt
Replace String in a File
Replace String in a File

If you want to redirect output instead of printing it on the terminal, use the redirection sign ( > ) as shown.

$ sed 's/Unix/Linux' hello.txt > output.txt
Redirect Output to File
Redirect Output to File

The output of the command is saved to the output.txt file instead of being printed on the screen.

To check out more options that can be used, once again check out the man pages.

$ man sed

3. Ack Command

Ack is a fast and portable command-line tool written in Perl. Ack is considered a friendly replacement for grep utility and outputs results in a visually appealing manner.

Ack command searches the file or directory for the lines that contain the match for the search criteria. It then highlights the matching string in the lines.
Ack has the capacity to distinguish files based on their file 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 check for the search term Linux, run:

$ ack Linux hello.txt
Check a String in a File
Check a String in a File

The search tool is quite intelligent and If no file or directory is provided by the user, it searches the current directory and subdirectories for the search pattern.

In the example below, no file or directory has been provided, but ack has automatically detected the available file and searched for the matching pattern provided.

$ ack Linux
Search String in a Directory
Search String in a Directory

To install ack on your system run the command:

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

4. Awk Command

Awk is a fully-fledged scripting language and also a text processing and data manipulation tool. It searches files or programs that contain the search pattern. When the string or pattern is found, awk takes action on the match or line and prints the results on STDOUT.

The AWK pattern is enclosed between curly braces while the entire program is enclosed in single quotes.

Let’s take the simplest example. Let’s assume you are printing the date of your system as shown:

$ date
Check Linux System Date
Check Linux System Date

Suppose you only want to print out the first value, which is the day of the week. In that case, pipe the output into awk as shown:

$ date | awk '{print $1}'

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

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

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

Awk Command Examples
Awk Command Examples

To get more options that you can use with awk, simply read our awk command series.

5. Silver Searcher

The silver searcher is a cross-platform and opensource code searching tool similar to ack but with an emphasis on speed. It makes it easy for you to search for a specific string within files in the shortest time possible:

Syntax:

$ ag OPTIONS search_pattern /path/to/file

For example, to search for the string ‘Linux‘ in a file hello.txt invoke the command:

$ ag Linux hello.txt
Find String in File
Find String in File

For additional options, visit the man pages:

$ man ag

6. Ripgrep

Lastly, we have the ripgrep command-line tool. Ripgrep is a cross-platform utility for searching regex patterns. It’s much faster than all of the earlier-mentioned search tools and recursively searches directories for matching patterns. In terms of speed and performance, no other tool stands out that Ripgrep.

By default, ripgrep will skip binary files/hidden files and directories. Also, be advised that by default it won’t search for files that are ignored by .gitignore/.ignore/.rgignore files.

Ripgrep also allows you to search for specific file types. For example, to limit your search to Javascript files run:

$ rg -Tsj

The syntax for using ripgrep is quite easy:

$ rg [OPTIONS] PATTERN [PATH...]

For example. To search for instances of the string ‘Linux’ in files located inside the current directory, run the command:

$ rg Linux
Search for String in a Files
Search for String in a Files

To install ripgrep on your system run the following commands:

$ sudo apt install ripgrep      [On Debian/Ubuntu]
$ sudo pacman -S ripgrep        [On Arch Linux]
$ sudo zypper install ripgrep   [On OpenSuse]
$ sudo dnf install ripgrep      [On CentOS/RHEL/Fedora]

For additional options, visit the man pages:

$ man rg

These are some of the most widely used command-line tools for searching, filtering, and manipulating text in Linux. If you have other tools you feel we have left out, do let us know in the comment section.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

James Kiarie
This is James, a certified Linux administrator and a tech enthusiast who loves keeping in touch with emerging trends in the tech world. When I'm not running commands on the terminal, I'm taking listening to some cool music. taking a casual stroll or watching a nice movie.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

2 Comments

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.