"Linux learning is not difficult" Linux Common Operations Command (+): grep command to find a string in a file that matches a condition

Source: Internet
Author: User
Tags perl regular expression

8.14 "Linux learning is not difficult" Linux Common Operations Command (+): grep command finds a string in a file that matches a condition

Use grep command can finds a string within a file that matches a condition.
Command syntax:

grep [ options ] [ Find mode ] [ file ]

The meanings of the options in the command are as shown in the table.

Options

option meaning

- e

Pattern is an extensible regular expression

- F

A pattern is a set of fixed-length strings separated by a break character

- P

Pattern is a Perl regular expression

- b

Displays the byte offset in the file of the line containing the matching string before each line of the output

- C

Show only the number of matching rows

- I.

Comparison is case insensitive

- L

Show only matching file names

- L

Show only filenames that don't match

- N

Add the line number of the line that matches the string before the output (the first line of the file is the line number 1)

- v

Show only lines that do not contain matching characters

- E < Mode >

Using patterns to perform matching operations

- F < file >

Get the pattern from the file

- R

Recursively, read all files in each directory

Example: in a file KKK match characters in search "Test File" .

[[email protected] ~]# grep ' test file ' KKK test file

Example: in a file AA all include at least 5 the row data contents of a contiguous lowercase character.

[[email protected] ~]# grep ' [a-z]\{5\} ' AA aaaaa aaaaaa

Example: in /ROOT/AA in the file Find out the b the line content at the beginning.

[[email protected] ~]# grep ^b/root/aa BBB

Example: in /ROOT/AA in the file the output is not b the line content at the beginning.

[Email protected] ~]# grep-v ^b/root/aa aaaaa aaaaa BBB aaaaaa

Example: in the /ROOT/KKK in the file output to le the line content at the end.

[[email protected] ~]# grep le$/root/kkk test file


This article extracts from "Linux Learning is not difficult" textbook, which is one of the most practical, theoretical and practicalLinuxIntroductory materials. To read the entire book, please visitLinuxoperation and Maintenance person(www.linuxywz.com) website.


"Linux learning is not difficult" Linux Common Operations Command (+): grep command to find a string in a file that matches a condition

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.