6 Finest CLI Instruments to Search Plain-Textual content Information Utilizing Common Expressions


Get real time updates directly on you device, subscribe now.

This information takes a tour of a number of the greatest command-line instruments which might be used for looking out matching strings or patterns in textual content recordsdata. These instruments are often used alongside common expressions – shortened as REGEX – that are distinctive strings for describing a search sample.

With out a lot additional ado, let’s dive in.

1. Grep Command

Coming within the first place is the grep utility device – is an acronym for World Common Expression Print, is a strong command-line device that turns out to be useful when looking for a particular string or a sample in a file.

Grep ships with trendy Linux distributions by default and provides you the flexibleness to return varied search outcomes. With grep, you may carry out an enormous array of functioning reminiscent of:

Seek for strings or matching patterns in a file.
Seek for strings or matching patterns in Gzipped recordsdata.
Depend the variety of string matches.
Print the road numbers that comprise the string or sample.
Search recursively for the string in directories.
Carry out a reverse search ( i.e. Show outcomes of strings not matching the search standards).
Ignore case sensitivity when looking for strings.

The syntax for utilizing the grep command is kind of easy:

$ grep sample FILE

For instance, to seek for the string ‘Linux‘ in a file, say, hey.txt whereas ignoring case sensitivity, run the command:

$ grep -i Linux hey.txt

Search For String in a FileSearch For String in a File

To get extra choices that you need to use with grep, merely learn our article that examples extra superior grep command examples.

2. sed Command

Sed – quick for Stream Editor – is one other helpful command-line device for manipulation textual content in a textual content file. Sed searches, filters and replaces strings in a given file in a non-interactive method.

By default, sed command prints the output to STDOUT (Customary Out), implying that the results of the execution is printed on the terminal as a substitute of being saved in a file.

Sed command is invoked as follows:

$ sed -OPTIONS command [ file to be edited ]

For instance, to interchange all situations of ‘Unix‘ with ‘Linux‘, invoke the command:

$ sed ‘s/Unix/Linux’ hey.txt

Replace String in a FileSubstitute String in a File

If you wish to redirect output as a substitute of printing it on the terminal, use the redirection signal ( > ) as proven.

$ sed ‘s/Unix/Linux’ hey.txt > output.txt

Redirect Output to FileRedirect Output to File

The output of the command is saved to the output.txt file as a substitute of being printed on the display screen.

To take a look at extra choices that can be utilized, as soon as once more try the person pages.

$ man sed

3. Ack Command

Ack is a quick and moveable command-line device written in Perl. Ack is taken into account a pleasant alternative for grep utility and outputs ends in a visually interesting method.

Ack command searches the file or listing for the traces that comprise the match for the search standards. It then highlights the matching string within the traces.
Ack has the capability to differentiate recordsdata based mostly on their file extensions, and to a sure extent, the content material within the recordsdata.

Ack command syntax:

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

For instance, to verify for the search time period Linux, run:

$ ack Linux hey.txt

Check a String in a FileTest a String in a File

The search device is kind of clever and If no file or listing is offered by the person, it searches the present listing and subdirectories for the search sample.

Within the instance beneath, no file or listing has been offered, however ack has mechanically detected the out there file and looked for the matching sample offered.

$ ack Linux

Search String in a DirectorySearch String in a Listing

To put in ack in your system run the command:

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

4. Awk Command

Awk is a fully-fledged scripting language and likewise a textual content processing and information manipulation device. It searches recordsdata or packages that comprise the search sample. When the string or sample is discovered, awk takes motion on the match or line and prints the outcomes on STDOUT.

The AWK sample is enclosed between curly braces whereas your entire program is enclosed in single quotes.

Let’s take the best instance. Let’s assume you’re printing the date of your system as proven:

$ date

Check Linux System DateTest Linux System Date

Suppose you solely need to print out the primary worth, which is the day of the week. In that case, pipe the output into awk as proven:

$ date | awk ‘{print $1}’

To show subsequent values, separate them utilizing a comma as proven:

$ date | awk ‘{print $1,$2}’

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

Awk Command ExamplesAwk Command Examples

To get extra choices that you need to use with awk, merely learn our awk command collection.

5. Silver Searcher

The silver searcher is a cross-platform and opensource code looking out device just like ack however with an emphasis on velocity. It makes it straightforward so that you can seek for a particular string inside recordsdata within the shortest time attainable:


$ ag OPTIONS search_pattern /path/to/file

For instance, to seek for the string ‘Linux‘ in a file hey.txt invoke the command:

$ ag Linux hey.txt

Find String in FileDiscover String in File

For added choices, go to the person pages:

$ man ag

6. Ripgrep

Lastly, we now have the ripgrep command-line device. Ripgrep is a cross-platform utility for looking out regex patterns. It’s a lot quicker than the entire earlier-mentioned search instruments and recursively searches directories for matching patterns. When it comes to velocity and efficiency, no different device stands out that Ripgrep.

By default, ripgrep will skip binary recordsdata/hidden recordsdata and directories. Additionally, be suggested that by default it received’t seek for recordsdata which might be ignored by .gitignore/.ignore/.rgignore recordsdata.

Ripgrep additionally means that you can seek for particular file varieties. For instance, to restrict your search to Javascript recordsdata run:

$ rg -Tsj

The syntax for utilizing ripgrep is kind of straightforward:


For instance. To seek for situations of the string ‘Linux’ in recordsdata positioned inside the present listing, run the command:

$ rg Linux

Search for String in a FilesSeek for String in a Information

To put in ripgrep in your system run the next instructions:

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

For added choices, go to the person pages:

$ man rg

These are a number of the most generally used command-line instruments for looking out, filtering, and manipulating textual content in Linux. When you have different instruments you are feeling we now have omitted, do tell us within the remark part.

If You Recognize What We Do Right here On TecMint, You Ought to Take into account:

TecMint is the quickest rising and most trusted group web site for any sort of Linux Articles, Guides and Books on the net. Tens of millions of individuals go to TecMint! to look or browse the 1000’s of printed articles out there FREELY to all.

In the event you like what you’re studying, please take into account shopping for us a espresso ( or 2 ) as a token of appreciation.

Support Us

We’re grateful on your by no means ending assist.

Leave A Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More