Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to find the contents of a file in linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to find file content in linux" related knowledge, small series through the actual case to show you the operation process, the operation method is simple and fast, practical, I hope this "how to find file content in linux" article can help you solve the problem.

In linux, you can use the grep command to find the contents of a file. The command is a powerful text search tool for finding strings in a file that match the criteria. You can use regular expressions to search text; the syntax is "grep [options] pattern filename."

This tutorial operates on Linux 5.9.8 and Dell G3 computers.

In linux, you can use the grep command to find file contents.

The grep (Global Regular Expression Print) command in Linux is a powerful text search tool for finding strings that match the criteria in a file. It can search text using regular expressions and print the matching lines.

The grep command traces its origins back to the early days of UNIX, where the search pattern patterns are called regular expressions (regular expressions), in order to search a file thoroughly, some users prefix the string to be searched with global (comprehensive). Once a match is found, the user will print it on the screen, and this series of operations will be integrated into global regular expressions print. This is the full name of the grep command.

The grep command searches one or more files for a specific character pattern (i.e., a regular expression), which can be a single character, string, word, or sentence.

A regular expression is a pattern that describes a set of strings. The composition of a regular expression mimics mathematical expressions, combining smaller expressions into a new expression by using operators. Regular expressions can be plain text or special characters used to generate patterns. To further define a search pattern, the grep command supports metacharacters (that is, wildcards) for these regular expressions as shown in Table 1.

Table 1 The wildcard function c* of regular expressions matches zero (that is, blank) or more characters c (c is any character). Will match any one character, and only one character. [xyz]Matches any character in square brackets. [^xyz] matches all characters except those in square brackets.^ Lock the beginning of a row.$ Lock the end of the line.

Note that in basic regular expressions, such as wildcard *,+,{,|,(and), etc., have lost their original meaning, and to restore their original meaning, add backslashes\before them, such as\*,\+,\{,\|,\(and\).

The grep command is used to search for a specific pattern in each file or (or on a specific output). When grep is used, each line containing the specified character pattern will be printed (displayed) on the screen, but the grep command does not change the contents of the file.

The basic format of the grep command is as follows:

[root@localhost ~]# grep [options] schema filename

The pattern here is either a character (string) or a regular expression. Common options for this command and their respective meanings are shown in Table 2.

Table 2 Common options and meanings of grep command Option Meaning-c List only the number of lines containing patterns in the file. - i Ignore upper and lower case letters in patterns. - l Lists file names with matching rows. - nList the row numbers at the beginning of each row. - v Lists rows with no matching pattern. - w Search for expressions as complete single characters, ignoring partially matched lines.

Note that if multiple files are searched, the grep command's search results show only the file names in which matching patterns are found; if a single file is searched, the grep command's results show each line containing matching patterns.

[Example 1] Suppose there is a list of employees at www.example.com. Now to search this file and find all employees with the position CLERK, execute the following command: emp.data

[root@localhost ~]# grep CLERK emp.data#ignore output

On this basis, if you only want to know the number of employees whose position is CLERK, you can use the "-c" option and execute the following command:

[root@localhost ~]# grep -c CLERK emp.data#Ignore output

[Example 2] Search emp.data file, use regular expression to find data line starting with 78, execute command as follows:

[root@localhost ~]# grep ^78 emp.data#Ignore output

The grep command is very powerful, and by using its different options and a wide variety of regular expressions, we can get any information we need.

About "how to find file content in linux" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report