33 C
Jaipur
Monday, October 26, 2020

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

Must read

Spotify Now Lets You Login with Your Google Account

Spotify has long offered the ability to login with Facebook and recently gained the capability to sign in with Apple ID on Apple devices....

Call Of Duty Mobile To Get A Mode With Zombies, But It Isn’t Zombies Mode

Call of Duty Mobile recently released the much-awaited Season 11 update with a lot of amazing content. However, unfortunately, Call of Duty Mobile didn’t...

Airtel Xstream App Is Now Available on Amazon Fire TV Stick: Report

Airtel Xstream came into existence more than a year ago. It was the company’s major play towards content aggregation, offering you a variety of...

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

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

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.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article

Spotify Now Lets You Login with Your Google Account

Spotify has long offered the ability to login with Facebook and recently gained the capability to sign in with Apple ID on Apple devices....

Call Of Duty Mobile To Get A Mode With Zombies, But It Isn’t Zombies Mode

Call of Duty Mobile recently released the much-awaited Season 11 update with a lot of amazing content. However, unfortunately, Call of Duty Mobile didn’t...

Airtel Xstream App Is Now Available on Amazon Fire TV Stick: Report

Airtel Xstream came into existence more than a year ago. It was the company’s major play towards content aggregation, offering you a variety of...

GitHub Removes YouTube Download Tool in Response to DMCA Takedown Notice

Microsoft-owned GitHub has removed YouTube-dl, an incredibly popular Python library to help download YouTube videos. The move is in response to a Digital Millennium...