Linux self-study journey-basic commands (grep command)

Linux self-study journey-basic commands (grep content search command)


Preface

1. In the previous section, we talked about the basic function and usage of wildcards. If you haven’t read it yet, please click the link below to watch: Wildcards

2. In this section, we continue to return to the command and describe another search command grep, but grep is different from the search file of find, grep is used to search the content of the file.


Tip: The following is the content of this article

One, grep introduction

The main function of grep is to extract the lines that match the string in the file.

  • Command name: grep
  • Location: /usr/bin/grep
  • Execution authority: all users
  • Function description: extract the line matching the string in the file content
命令格式
grep [选项] 查找内容 文件
常用选项:
-n:显示行号
-v:反向查找
-i:忽略大小写
-o:仅显示匹配到的字符串
--color=auto:搜索出的结果颜色高亮显示(默认选项)

Two, grep use

1. When we directly want to find a certain string in a file, and let grep display the line corresponding to the matched string:

grep root /etc/passwd: Represents the search for the passwd file in the /etc directory, and displays the line containing the root field .
Insert picture description here


2. When we directly want to find a certain string in a file, and let grep display the line corresponding to the matched string, there is a line number identification:

grep -n root /etc/passwd: It means to search the passwd file in the /etc directory, display the line that contains the root field, and display the number of lines in the file .
Insert picture description here

3. When we directly want to find a certain string in a file, and let grep display the line corresponding to the matched string, ignore the case:

grep -i user /etc/passwd: It means to search the passwd file in the /etc directory, and display the line that contains the user field, and the user field will be displayed ignoring case, that is, uppercase USER or lowercase will do .
Insert picture description here

4. When we directly want to find a certain string in a file, and let grep display all lines except the line corresponding to the matched string:

grep -v repos redhat.repo: means to search for the redhat.repo file in this directory, and do not display the lines that contain the repos field, and only display the lines that do not contain the repos field .
Insert picture description here


to sum up

In this section we talked about
{

Basic use of grep

} In the
next section, we will talk about regular expressions, when we are using it with grep.

Guess you like

Origin blog.csdn.net/qq313088385/article/details/113697059