Search for strings or patterns within plain text from terminal

about search for strings or patterns

In the following article we will see some command-line tools used to find matching strings or patterns within text files. These tools are generally used in conjunction with regular expressions, abbreviated as REGEX, which are unique strings to describe a search pattern.

Regular expressions are patterns used to find a certain combination of characters within a text string. Regular expressions provide a very flexible way to search or recognize text strings. Although in the following lines we will not see regular expressions, we will use patterns or strings.

Search for strings or patterns from the terminal

Grep command

Grep is an acronym for Global Regular Expression Print. This is a powerful command line tool that is useful when looking for a specific string or pattern in a file. With grep we will be able to use a wide range of functions, such as those that a colleague explained to us in this blog a while ago.

The syntax for using the grep command is pretty simple:

grep PATRON [RUTA AL ARCHIVO]

For example, to search for the string 'Ubuntu'in a file that in this example we will call text.txt, without taking into account the distinction between upper and lower case, in a terminal (Ctrl + Alt + T) we will only have to execute the command:

grep -i text

grep -i Ubuntu texto.txt

Command sed

Thirst is short for StreamEditor. It is another useful tool for the terminal with which we can manipulate text in a file. Sed searches, filters and replaces strings in a given file.

The sed command by default prints the output in STDOUT (Standard output). This implies that the result of the execution is printed to the terminal instead of being saved to a file.

The Sed command can be used as follows:

sed -OPCIONES COMANDO [RUTA AL ARCHIVO]

For example, to replace all occurrences of 'Linux'in a text by'Linux', the command to use would be the following:

Command sed

sed 's/Linux/GnuLinux/g' texto.txt

If what we are looking for is redirect the output to a file instead of printing it to the terminal, we are going to use the redirect sign as follows:

sed 's/Linux/GnuLinux/g' texto.txt > salida.txt

The output of the command is saved in the file output.txt instead of being printed on the screen.

sed command output

To see more options, you can consult the man pages:

man thirsty

man sed

ack

Ack is a fast command line tool written in Perl. It is considered a friendly replacement for the grep utility, which also generates results in a visually attractive way.

For install ack in our system we will have to execute in the terminal (Ctrl + Alt + T):

install ack

sudo apt install ack

The command Ack searches the file or directory for the lines that contain the match for the search criteria. Then highlight the corresponding string. This tool has the ability to distinguish files based on their extensions.

The syntax of the Ack command would be something like the following

ack [OPCIONES] PATRÓN [RUTA AL ARCHIVO]

For example, to search for the term Linux in a file, we would have to execute:

command ack search

ack Linux texto.txt

The search tool is pretty smart and if the user does not supply any files or directories, it searches the current directory and subdirectories for the search pattern.

In the following example, no file or directory is provided. Ack automatically detects the available file in the directory and looks for the matching pattern:

ack search without file

ack Linux

ripgrep

ripgrep is a cross-platform utility to search for regular expression patterns. It's faster than all the search tools mentioned above, and it searches directories recursively for matching patterns. It also allows you to search for specific file types. By default, ripgrep will skip binaries and hidden files / directories.

For install ripgrep on the system, all you have to do is run the following command in the terminal (Ctrl + Alt + T):

ripgrep installation

sudo apt install ripgrep

The syntax for using ripgrep is pretty straightforward:

rg [OPCIONES] PATRÓN [RUTA AL ARCHIVO]

If we wanted to search for the chain 'Linux'in files located within the current directory, we will only have to execute the command:

manrg linux

rg Linux

To see more options, users can use man pages:

man rg

man rg

SilverSearcher

For install this toolIn Ubuntu we will only have to open a terminal (Ctrl + Alt + T) and write the command:

silversearcher-ag installation

sudo apt install silversearcher-ag

Silver Searcher is a cross-platform, open source search tool similar to ack but with an emphasis on speed. It makes it easy to find a specific string within files in the shortest possible time. The syntax to use would be:

ag OPCIONES PATRÓN_DE_BÚSQUEDA /ruta/al/archivo

For example, to search for 'Linux' in the File text.txt, we would have to write in the terminal (Ctrl + Alt + T):

search with silversearcher

ag Linux texto.txt

To see more options we can consult the man pages:

man silversearcher

man ag

These are just some of the most commonly used command line tools for searching, filtering, and manipulating text in Linux.


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.