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 quickly master grep commands and regular expressions in linux

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to quickly master grep commands and regular expressions in linux. I think it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Quick understanding of regular expressions

1. How to match what you are looking for?

A regular expression is simply a pattern that matches each input line.

Search for 'vivek'' in'/ etc/passswd'.

Grep vivek / etc/passwd

Output result case:

Vivek:x:1000:1000:Vivek Gite,:/home/vivek:/bin/bash vivekgite:x:1001:1001::/home/vivekgite:/bin/sh gitevivek:x:1002:1002::/home/gitevivek:/bin/sh

Search for 'vivek' (that is, size-insensitive) in any case:

Grep-I-w vivek / etc/passwd

Case-insensitive retrieval of 'vivek' and' raj':

Grep-E-I-w'vivek | raj' / etc/passwd

In the case of * *, a pattern that extends regular expressions is used.

Fixed location of the retrieved content:

You can use the ^ and $symbols to force a regular expression to match the beginning or end of a line, respectively. The following example shows text that starts with 'vivek'.

Grep ^ vivek / etc/passwd

Example of output:

Vivek:x:1000:1000:Vivek Gite,:/home/vivek:/bin/bash vivekgite:x:1001:1001::/home/vivekgite:/bin/sh

You can display only lines of text that begin with vivek. For example, it doesn't show the beginning of words like vivekgite and vivekg.

Grep-w ^ vivek / etc/passwd

Retrieve the text format that ends with 'foo':

Grep 'foo$' FILENAME

You can also search for blank lines in the following ways:

Grep'^ $'FILENAME

2. How to match specific characters?

Match 'Vivek' or' vivek':

Grep'[vV] ivek' FILENAME

Or it could be like this:

Grep'[vV] [iI] [Vv] [Ee] [kK] 'FILENAME

You can match numbers (such as matching vivek1 or Vivek2):

Grep-w'[vV] ivek [0-9] 'FILENAME

You can match two digits (for example, match foo11, foo12):

Grep 'foo [0-9] [0-9]' FILENAME

It's not just numbers, you can match letters:

Grep'[A-Za-z] 'FILENAME

Displays all lines of text that contain the letter "w" or "n":

Grep [wn] FILENAME

In the expression in parentheses, the name of the character class attached in "[:" and ":]": represents a list of all characters belonging to the class. Standard character class name:

[: alnum:]-alphanumeric characters. [: alpha:]-alphabetical order [: blank:]-spaces and tabs. [: digit:]-number:'0 1 2 3 4 5 6 7 8 9'. [: lower:]-lowercase letters:'a b c d e f'. [: space:]-Special characters: tabs, line breaks, vertical tabs, page feeds, carriage returns, and spaces. [: upper:]-Capital letters:'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'.

In the following example, all uppercase letters are matched:

Grep'[: upper:] 'FILENAME

3. How to use wildcards?

You can use "." Instead of a single character. In the following example, all three-character words that begin with the letter "b" and end with the letter "t" are queried.

Grep'\ 'FILENAME

In the above example:

\

< 在单词的开始位置匹配空格字符串 \>

Match a space string at the end of a word

Retrieve and output the results of all two letters:

Grep'^.. $'FILENAME

Retrieve and display all'.' And the result at the beginning of the number:

Grep'^\. [0-9] 'FILENAME

Escape character'.'

The following regular expression looking for the IP address 192.168.1.254 will not get the expected results:

Grep '192.168.1.254' / etc/hosts

Three of these points need to be escaped:

Grep '192\ .168\ .1\ .254' / etc/hosts

The following example matches only one address:

Egrep'[[: digit:]] {1 digit 3}\. [: digit:] {1 digit 3}\. [[: digit:]] {1 digit 3}\. [: digit:] {1 digit 3} 'FILENAME

The following matches the words Linux or Unix regardless of case:

Egrep-I'^ (linux | unix) 'FILENAME

Explore the advanced search mode of grep in depth

1. How do I retrieve a pattern that starts with'-'?

Use the-e option to search for all results that match'- test-'. Grep will try to parse'- test-' as an option:

Grep-e'- test--' FILENAME

2. How to use OR logic operation in grep?

Grep-E 'word1 | word2' FILENAME### OR # egrep' word1 | word2' FILENAME

Or you can do this.

Grep 'word1\ | word2' FILENAME

3. How to use AND logic operation in grep?

Display all the results that contain the words' word1' and 'word2' according to the following syntax:

Grep 'word1' FILENAME | grep' word2'

Or it could be like this:

Grep 'foo.*bar\ | word3.*word4' FILENAME

4. How to test the sequence?

You can test the number of times a character repeats in a sequence using the following syntax:

{N} {N,} {min,max}

Match the result of a string containing two letters v:

Egrep "v {2}" FILENAME

The following example retrieves the string results that contain "col" and "cool" in the file:

Egrep'co {1 FILENAME 2} 1 'FILENAME

The following example matches a result with at least three letters c:

Egrep'c {3,} 'FILENAME

The following example will match the mobile phone number in the format "91-1234567890" (that is, "two digits-ten digits")

Grep "[[: digit:]]\ {2\} [-]\? [[: digit:]]\ {10\}" FILENAME

5. How to highlight the output of grep?

Use the syntax of the following example:

Grep-color regex FILENAME

6. How to make the output of grep show only the matching part instead of the whole line?

Use the syntax of the following example:

Grep-o regex FILENAME

Regular expression operator summary

Regular expressions: operator meaning

Matches any single character. ? Matches the previous character 0 or 1 times. * match the previous character ≥ 0 times. + matches the previous character ≥ once. {N} matches the previous character N times. {N,} matches the previous character ≥ m times. {Nrecoery M} matches the previous character N to M times. -indicates the range if it is at the end of a list or range in the list. The ^ start tag indicates that an empty string is matched at the start position. Also represents characters that are not within the range of the list. $closing mark. Matches an empty string. \ b word lock. Matches an empty string at the edge of a word. \ B matches an empty string at a non-edge position of a word. \

< 匹配单词开始的空字符串。 \>

Matches an empty string at the end of the word.

About grep and egrep

Egrep, or grep-E, interprets the pattern as an extended regular expression. The grep help documentation defines In basic regular expressions the meta-characters?, +, {, |, (, and) lose their special meaning; instead use the backslashed versions\?,\ +,\ {,\ |,\ (, and\). Traditional egrep did not support the {meta-character, and some egrep implementations support\ {instead, so portable scripts should avoid {in grep-E patterns and should use [{] to match a literal {. GNU grep-E attempts to support traditional usage by assuming that {is not special if it would be the start of an invalid interval specification. For example, the command grep-E'{1 'searches for the two-character string {1 instead of reporting a syntax error in the regular expression. POSIX.2 allows this behavior as an extension, but portable scripts should avoid it. On "how to quickly master grep commands and regular expressions in linux" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

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