Search pattern in file linux

Find text in files using the Linux grep command

Using grep, you can quickly find text matching a regular expression in a single file, a group of files, or text coming from stdin.

Compass

Searching for patterns of text in files or text streams is one of the most common tasks you’ll perform in your sysadmin career. This is a valuable skill that allows you to check a variety of system configurations, analyze data, troubleshoot logs, and perform many other activities.

Training & certification

The most common way to find text in a Linux system is using the command-line utility grep . This utility was originally developed for the Unix operating system in the early 1970s. Grep evolved over the years, and the most common version available today for Linux, GNU grep, has additional features such as colored output. However, its main functionality is still the same.

Using grep , you can quickly find text matching a regular expression in a single file, a group of files, or text coming from stdin using the shell pipe operator.

This article covers how to use the grep command to find text.

Find text in a file

The most basic way to use grep is searching for text in a single file. To do this, type grep followed by the text pattern to search for and the file name to search in. For example, to find which port the Secure Shell (SSH) daemon uses, search for Port in file /etc/ssh/sshd_config :

$ grep Port /etc/ssh/sshd_config Port 22 #GatewayPorts no

Notice that grep finds all lines that match the text pattern regardless of where the pattern is located.

Читайте также:  Администрирование операционной системы astra linux special edition

Extend grep with regular expressions

In the previous example, when you searched for Port in the SSH configuration file, grep returned two lines. The line you were looking for, Port 22, and an additional line containing the search pattern. In some cases, that’s exactly what you want. In other cases, grep could find too many entries that you’re not interested in, requiring you to sort through them to find the desired information.

To avoid that, you can use regular expressions to be more specific about what you’re looking for. For example, to find only lines that start with the word Port , you can use the regular expression operator ^ , like this:

$ grep ^Port /etc/ssh/sshd_config Port 22

Источник

How to find lines containing a string in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

I have a file in Linux, I would like to display lines which contain a specific string in that file, how to do this?

5 Answers 5

The usual way to do this is with grep , which uses a regex pattern to match lines:

Each line which matches the pattern will be output. If you want to search for fixed strings only, use grep -F ‘pattern’ file . fgrep is shorthand for grep -F .

Читайте также:  Linux вывести все исполняемые файлы

addition grep -rn ‘string’ /path/ if you want to search a string in a folder in which file including and line number

Besides grep , you can also use other utilities such as awk or sed

Here is a few examples. Let say you want to search for a string is in the file named GPL .

Your sample file

$ cat -n GPL 1 The GNU General Public License is a free, copyleft license for 2 The licenses for most software and other practical works are designed 3 the GNU General Public License is intended to guarantee your freedom to 4 GNU General Public License for most of our software; 
$ grep is GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to 
$ awk /is/ GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to 
$ sed -n '/is/p' GPL The GNU General Public License is a free, copyleft license for the GNU General Public License is intended to guarantee your freedom to 

Источник

Оцените статью
Adblock
detector