Linux grep Regular Expression

Source: Internet
Author: User

Command alias Settings
An alias can be used when the command is very long
See what aliases are available
#alias
#alias rm= ' Rm-i '
Remove command aliases
#unalias LM

grep can parse a line of text, get the keyword, if the line exists in the keyword, will be the whole row out.

grep [-ACINV] [--color=auto] ' search string ' filename

-A: binary files are searched for data in the form of a text file
-C: Count the number of ' search strings ' found
-I: Ignores case differences, so case is considered the same
-N: Output line number
-V: Reverse selection, which shows the line without the ' search string ' content!

#grep-N root/etc/passwd

Find Jsen or JSON in the current directory
#grep-N Js[eo]n *

Regular expressions

[]: Match [] inside the character, it represents only a "one" character, can be a single character, or it can be a sequence of characters. can be used-to denote [] the range of character sequences, as in [1-5] instead of [12345]
^word: string to be searched (word) at the beginning of the line example: Grep-n ' ^# ' test.txt (find the row beginning with the beginning of #, parallel travel number)
word$: String to find word at line end
\: Escape character, special meaning of special symbol removal Example: grep-n \ ' test.txt find meaning single quote ' of the line
[^]: reverse Selection,
[^g]oo: Do not want OO front has g
. (decimal point): The representative must have an arbitrary character meaning
* (asterisk): represents the first character repeating 0 to an infinite number
\{n,m}\ successive N to M of the previous re character, if \{n}\ is a continuous n of the previous re character, if \{n\}\ is a continuous n more than the previous re character
Grep-n ' go\{2,3}\ ' g test.txt between G and G there are 2 to 3 o strings present

#grep-N [^g]oo test.txt

Oo don't want to have lowercase characters in front of you
#grep-N [^a-z]oo test.txt

Get the line with numbers
#grep-N [0-9] Test.text

The only one listed at the beginning of the line
#grep-n ' ^the ' test.txt
Don't want lowercase characters in front of OO
#grep-N [^a-z]oo test.txt

The line that starts with lowercase characters
#grep-N ^[a-z]oo test.txt
Or
#grep-n ' ^[[:lower:]] ' test.txt

Find the end of the line ending as a decimal point (.) Of that line
#grep-n ' \.$ ' test.txt
\ is an escape character

Blank line (command does not work)
#grep-n ' ^$ ' test.txt

No, it's the line.
Grep-v ' ^# '

Find out g?? The character of D, that is, a total of 4 characters
Grep-n ' G.. d ' test.txt

Find at least one O character before
Grep-n ' oo* ' test.txt

Both the beginning and the end are G, but there can be at least one o between the two G
Grep-n ' Goo*g ' test.test

Grep-n ' G*g '

The beginning of the end is G
Grep-n ' G.*g '

Any number line
Grep-n ' [0-9][0-9]* ' test.txt

Practice two O strings
Grep-n ' o\{2}\ '
{} has a special meaning in the shell, so it is necessary to use escape characters to lose special meaning characters

G after 2 to 5 O
Grep-n ' go\{2,5}\ ' test.txt

List connection file header will be ' lrwxrwxrwx '
#ls-L/etc | grep ' ^| '

Sed awk tool

SED can share standard input and can replace, delete, add, and select specific lines of data

#nl/etc/passwd | Sed ' 2,5d '
Delete Rows 2 to 5

Linux grep Regular Expression

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.