Linux basic Regular expression: grep,sed

Source: Internet
Author: User

The influence of family language on regular expressions is first explained
Lang=c:0,1,2,3,4...a,b,c,d ... Z a b c d ... z
Lang=zh_cn:0,1,2,3,4...a a b b c c d d ... z Z
Because different languages are screened differently by [A-z], special symbols appear
[: Alnum:]: That is, 0-9,a-z,a-z, English uppercase and lowercase characters and numbers,
[: Alpha:]: a-z,a-z, any English uppercase and lowercase characters
[:d igit:]: 0-9, all numbers
[: Upper:]: A-Z, all uppercase characters
[: Lower:]: A-Z, all lowercase characters
[: Space:]: Any character that generates whitespace, including spaces, tabs, and CR, etc.
[: Blank:]: Space and Tab
[: Cntrl:]: Control keys on keyboard, including C,lf,tab,del, etc.
Other reference online Related materials
It's okay to see now, here's an example

Some of the advanced parameters of grep

The basic usage of grep has been discussed in 11.5.
grep: Parsing a single line of information
grep ' Find the string ' filename
If found, lists the rows where the string exists.
Grep-i ignoring case
Grep-n by the way output line number
Grep-c only counts the number of occurrences, does not display rows
Grep-v ' filename shows no rows
grep--color=auto ' filename keyword has a special color

grep-an1-bn2 ' string ' filename
-a:after, showing the next few lines
-b:before, show more previous lines
-C: Up and down a few lines
As long as there is found, it will be below or above the n rows

If the chosen color is not
Individual environment variables can be customized
Add alias grep= ' grep--color=auto ' to ~/.BASHRC and Sourc ~/.BASHRC

Sample text content
Regular_express.txt
"Open Source" is a good mechanism to develop programs.
Apple is my favorite food.
Football game isn't use feet only.
This dress doesn ' t fit me.
However, this dress was about $3183 dollars.
GNU is free air isn't free beer.
She hair is very beauty.
I can ' t finish the test.
Oh! The soup taste good.
Motocycle is cheap than car.
This window is clear.
The symbol ' * ' is represented as start.
Oh! My god!
The GD software is a library for drafting programs.
You are the mean of the best is.
The world <Happy> is the same as "glad".
I like dog.
Google is the best tools for search keyword.
Gooooole yes!
Go! Go! Let ' s go.
#I am Vbird

grep All usage examples: (each example will list-n displays the number of rows)
A. (reverse, case-insensitive) find a specific string
Grep-n[v][i] ' string ' file



If you find a special character (. *\{} ') that needs to be escaped, the following (c) describes
decimal point, asterisk, backslash, curly brace, single quotation mark, not including parentheses (parentheses can be found directly)

B. Use the brackets [] to find the set character (which is what may appear in brackets, plus ^ becomes nonexistent)

Find strings that are not followed by ' Oo ', starting with G


Culling lowercase characters

We know that because of language differences, you may have an impact. You can use special symbols to select





c. Any one character. With repeating characters *
. (decimal point): The delegate must have an arbitrary character
* (asterisk): Represents the repetition (re,repeat) of the previous 0 to infinity meaning, for the combined form
The * in this case is not the same as the wildcard character, and the * in the wildcard is the meaning of 0 or more characters.
Two characters in the middle of a g,d

Find more than two O-

At least one o between two g

. * represents 0 or more characters

Find out all the numbers

Indicates that the asterisk is not necessarily a fixed thing, it may be a regular expression

D. Use the line start character ^ and the trailing character $
^ is not the same as the previous reverse selection
The inverse selection character is in brackets [],
All just see ^ in [], then it's reversed, otherwise it's the beginning of the line character

To find the beginning of a character

Don't want the letters to start.

Or


Find the characters that end with.
Because it is a special character, you need to escape with \

Note: The break characters in windows are judged differently, so there may be problems that cannot be found
the specific solution will have time to update or Baidu has explained

Find (remove) blank lines
  ^$



remove blank lines and comments from a file
grep-v ' ^$ ' filename ' | grep-v ' ^# '
You can analyze it yourself

Originally there were 48 lines, after removal



E. Qualifying consecutive re (repeating) character range {n1,n2} (N1,n2 appears one also can)
Also because {} is an escape character, you need to lose special meaning with \
Find out that O appears two times





Other special character queries




sed Tools
itself is also a pipeline command, you can analyze standard input, and SED can also replace the data, delete, add, select specific lines and other functions
sed [-NEFR] [' action ']
Parameters:
-N: Quiet mode, only the line (or operation) that has been specially processed by SED will be listed
Both the default stdin and the modified information are printed.
-E: Action edit for SED directly in command line mode
-F: The SED action is written directly in a file, and-f filename can perform the SED action within filename
The-r:sed action supports an extended regular expression (the default is the underlying regular expression syntax)
-I: Directly modify the contents of the read file, not by the screen output (the default is to read the modified output to the screen, the original file will not change)
Action =[n1[,n2]]function
function
A: New, followed by a string that inserts a row on the next line of the specified line
C: Replace, followed by string, replace line between N1,N2
D: Delete, without string
I: Inserting, followed by a string, and then inserting a row on the previous line
P: Print, print a selected data, usually run with Sed-n
S: Replace, you can do the replacement work directly. Usually paired with regular expressions

new (SED ' -2a str ') and delete (sed ' n1,n2d ') functions in line (hang):
Sed ' 2,5d '
SED ' 2a Hello world '
Sed ' 2a Hello Word \[enter] Yes '




Replace with the behavior unit (SED ' n1,n2c str ') with the display (sed-n ' n1,n2p ') function
Display just like Head tail combo function
Sed ' 2,5c No 2-5 number '
Sed-n ' 2,5p '



Find and replace part of data function (send ' s/to be replaced by string/new string/g ' remove sed '/string/d ')
The string to be replaced can be a regular expression
Sed ' s/#.*$//g ' replaces comment lines with blank lines


directly modify file contents (sometimes available, but important files remember to back up)
Sed- I.' s/. /.. /g ' filename






Linux basic Regular expression: grep,sed

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.