Вы находитесь на странице: 1из 15

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


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
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
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


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


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
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
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


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.
TagsCommandline Tools, Linux TricksPost navigation
How to Run Any Linux Distribution Directly from Hard Disk in Ubuntu Using
Grub Menu
Useful PuTTY Configuration Tips and Tricks
If you liked this article, then do subscribe to email alerts for Linux tutorials. If
you have any questions or doubts? do ask for help in the comments 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.

We are thankful for your never ending support.

Related Posts

How to Write Scripts Using Awk Programming Language – Part 13


How to Use Flow Control Statements in Awk – Part 12

How to Allow Awk to Use Shell Variables – Part 11


Learn How to Use Awk Built-in Variables – Part 10

Learn How to Use Awk Special Patterns ‘BEGIN and END’ – Part 9
Learn How to Use Awk Variables, Numeric Expressions and Assignment
Operators – Part 8

2 thoughts on “6 Best CLI Tools to Search


Plain-Text Data Using Regular
Expressions”

1.
Brandon
August 12, 2020 at 1:23 am
Should probably replace silver searcher with the more modern (and
faster!) RipGrep.
Reply


Ravi Saive
August 12, 2020 at 9:41 am
@Brandon,

Thanks for the suggestion, will include RipGrep to the article…


Reply

Got something to say? Join the


discussion.
Have a question or suggestion? Please leave a comment to start the
discussion. Please keep in mind that all comments are moderated and your
email address will NOT be published.

Comment

Name EmailWebsite

 Save my name, email, and website in this browser for the next time I
comment.

 Notify me of followup comments via e-mail. You can


also subscribe without commenting.
Post Comment

This site uses Akismet to reduce spam. Learn how your comment data is
processed.
Over 3,500,000+ Readers

         
A Beginners Guide To Learn Linux for Free [with Examples]
Red Hat RHCSA/RHCE 8 Certification Study Guide [eBooks]
Linux Foundation LFCS and LFCE Certification Study Guide [eBooks]
Learn Linux Commands and Tools
10 Practical Examples Using Wildcards to Match Filenames in Linux
4 Ways to Disable/Lock Certain Package Updates Using Yum Command
Install Innotop to Monitor MySQL Server Performance
How to Backup or Clone Linux Partitions Using ‘cat’ Command
11 Linux Kernel Boot-time Parameters Explained
7 Ways to Determine the File System Type in Linux (Ext2, Ext3 or Ext4)
If You Appreciate What We Do Here On TecMint, You Should Consider:

Linux Server Monitoring Tools


Guider – A System Wide Linux Performance Analyzer
How to Setup Rsyslog Client to Send Logs to Rsyslog Server in CentOS
7
Pyinotify – Monitor Filesystem Changes in Real-Time in Linux
How to Monitor MySQL/MariaDB Databases using Netdata on CentOS 7
Darkstat – A Web Based Linux Network Traffic Analyzer
How to Boost Linux Server Internet Speed with TCP BBR
Learn Linux Tricks & Tips
How to Save Command Output to a File in Linux
How to Test Website Loading Speed in Linux Terminal
How to Watch TCP and UDP Ports in Real-time
How to Clone a Partition or Hard drive in Linux
12 Useful Commands For Filtering Text for Effective File Operations in
Linux
How to Download MP3 Tracks from a YouTube Video Using YouTube-DL
Best Linux Tools
7 Best Command-Line Email Clients for Linux in 2020
Top 3 Open-Source Cross-Distribution Package Management Systems
for Linux
6 Best CCleaner Alternatives for Ubuntu
Top 27 Tools for VMware Administrators
The 5 Best Command Line Music Players for Linux
Top 6 Partition Managers (CLI + GUI) for Linux
 Donate to TecMint
 Contact Us
 Advertise on TecMint
 Linux Services
 Copyright Policy
 Privacy Policy
 Career
 Sponsored Post
Tecmint: Linux Howtos, Tutorials & Guides © 2020. All Rights Reserved.
The material in this site cannot be republished either online or offline, without
our permission.

Hosting Sponsored by : Linode Cloud Hosting


Scroll back to top

Вам также может понравиться