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]

#awk command #linux commands #sed command #commandline tools #linux tricks #linux

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