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

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

 

This information takes a tour of a number of the finest command-line instruments which might be used for looking matching strings or patterns in textual content information. These instruments are normally 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 robust command-line device that turns out to be useful when looking for a particular string or a sample in a file.

Grep ships with fashionable Linux distributions by default and provides you the pliability to return numerous search outcomes. With grep, you may carry out an enormous array of functioning resembling:

  • Seek for strings or matching patterns in a file.
  • Seek for strings or matching patterns in Gzipped information.
  • Rely the variety of string matches.
  • Print the road numbers that include 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

5 Best CLI Plain-Text Data Search Tools Using Regular ExpressionsSearch 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 (Commonplace Out), implying that the results of the execution is printed on the terminal as an alternative of being saved in a file.

Sed command is invoked as follows:

$ sed -OPTIONS command [ file to be edited ]

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

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

5 Best CLI Plain-Text Data Search Tools Using Regular ExpressionsChange String in a File

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

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

5 Best CLI Plain-Text Data Search Tools Using Regular ExpressionsRedirect Output to File

The output of the command is saved to the output.txt file as an alternative of being printed on the display.

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 leads to a visually interesting method.

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

Ack command syntax:

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

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

$ ack Linux hey.txt

5 Best CLI Plain-Text Data Search Tools Using Regular ExpressionsVerify a String in a File

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

Within the instance under, no file or listing has been offered, however ack has robotically detected the obtainable file and looked for the matching sample offered.

$ ack Linux

5 Best CLI Plain-Text Data Search Tools Using Regular ExpressionsSearch 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 in addition a textual content processing and knowledge manipulation device. It searches information or applications that include 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 complete program is enclosed in single quotes.

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

$ date

5 Best CLI Plain-Text Data Search Tools Using Regular ExpressionsVerify 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.

5 Best CLI Plain-Text Data Search Tools Using Regular ExpressionsAwk Command Examples

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

5. Silver Searcher

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

Syntax:

$ 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

5 Best CLI Plain-Text Data Search Tools Using Regular ExpressionsDiscover String in File

For extra choices, go to the person pages:

$ man ag

These are a number of the most generally used command-line instruments for looking, filtering, and manipulating textual content in Linux. You probably have different instruments you are feeling we’ve got neglected, 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 neighborhood web site for any sort of Linux Articles, Guides and Books on the internet. Thousands and thousands of individuals go to TecMint! to go looking or browse the hundreds of printed articles obtainable FREELY to all.

If you happen to like what you’re studying, please contemplate shopping for us a espresso ( or 2 ) as a token of appreciation.

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

We’re grateful in your by no means ending help.