Linux Learning materials-Basic formal notation (grep)

Source: Internet
Author: User
Tags diff goto uppercase character

Basic formal notation:

 

--------------------------------------------------------------------------------

Grep
 
Syntax: [Root @test/root]# grep [-acinv] ' search string ' filename
Parameter description:
-A: Search data in binary files in text file mode
-C: Calculates the number of ' search string ' found
-I: Ignores case differences, so case is considered the same
-N: Output line number by the way
-V: Reverse selection, which shows the line without the ' search string ' content!
Example:
[Root @test/root]# grep ' root '/var/log/secure
Show/var/log/secure the root line in this file.
[Root @test/root]# grep-v ' root '/var/log/secure
If the line doesn't have root, show the data to the screen!

[Root @test/root]# Last | grep root
If the line has root, the data will show up on the screen!

[Root @test/root]# grep [a-z]anpath/etc/man.config
Will/etc/man.config this file, all of them:
[any one uppercase character] the line after the Anpath is displayed!
For example Aanpath, Banpath .... Zanpath, wait!

Description
grep is a very common instruction, the most important function is the comparison of string data, it is necessary to note that "grep in a file to search for a string, he is the" whole line "as a unit of data retrieval! "That is, if there are 10 rows in a file, two lines with the string you are searching for, then the two lines are displayed on the screen, and the others are discarded!" In addition to the file search, grep is often used in input/output data processing, such as the Common Pipeline command (pipe) can often see his trace! In the above example, we can see that the first two examples are the search for the contents of the file, plus-V shows the result is "the opposite!" , and the third example is the processing of the data with the function of the pipe Oh! As for the last example, what does it mean to use the brackets [] in a special way?
 
Inside the brackets is "only one character", and the inside content is "the range of characters"! So [A-z] means: "A to Z of any one of the characters can be accepted, but only one character" meaning! So, if I want all the numbers to be accepted, that's [0-9], what if I just accept 1,3,5? [135] or it can be separated by commas [1,3,5]! So, the minus sign-in brackets, means "a range"! There are many uses for the brackets! It will be described in more detail below!
 
grep is one of the simplest formal representation search commands, and he doesn't support some of the more rigorous formal notation content, but it's already quite handy! Let's take a file as an exercise! Let's take a look at the contents of this file:
[Root @test/root]# vi regexp.txt
This is a cat, however, I need a dog.
I want to "Happy" and <Happy> and/happy/here.
* I don ' t know how does this *
# I don ' t Know how does this $
^ I don ' t KNOW how does this @
Ok! Everythins is OK
Now, I'll eat my food
is finished your work
What does you 123 goto where
Tesaaaaatest
Quit Bbbbqqa Testglgs
I'll shutdown my mechine
Bye bye
 
This file was edited by Brother Bird in MS dos, so he also contains some special characters Fu Yi! The use of VI to view the time is not very clear, if you use cat-a to view, what will happen?
[Root @test/root]# cat-a regexp.txt
This is a cat, however, I need a dog.^m$
I want to "Happy" and <Happy> and/happy/here.^m$
* I don ' t know how does this *^m$
# I don ' t Know how does this $^m$
^ I don ' t KNOW how does this @^m$
Ok! ^ieverythins is ok^m$
Now, I'll eat my food^m$
Is you ^ifinished your work^m$
What does you 123 goto where^m$
tesaaaaatest^m$
Quit Bbbbqqa testglgs^m$
I'll shutdown my mechine^m$
Bye bye^m$
 
The bold part of the above is the special character under the DOS file! What is the difference between using VI and CAT-A to show the results? and the main DOS characters are:
^m: Represents a break character;
^i: Indicates the <tab> caused the key symbol;
$: means the end of each line!
This regexp.txt file you can download back in http://linux.vbird.org/download/index.php#regular to do the exercises Oh! Well, let's say you've got this file for him to get under his Linux, and at this point, you should do a little exercise! Let's see!
Example one: Find out if this file contains know characters, and list the line numbers:
[Root @test/root]# grep-n ' know ' regexp.txt
3:* I don ' t know how does this *
grep will be a line of know this string, please note that the case is not the same!
When the string is not know in the row, grep discards the line without displaying it.
However, on the third line, there is a know, so the third row will be displayed!
Example two: Find out this file contains * This character, and the line number is listed:
[Root @test/root]# grep-n ' \* ' regexp.txt
3:* I don ' t know how does this *
Please pay special attention to the existence of a considerable number of "special characters" in the formal notation, and to
When you cancel the properties of these special characters, the presence of the backslash "\" is especially important!
In this example, you may find that using grep-n ' * ' regexp.txt can also be
The same results, however, good habits are needed to develop, here, please with more orthodox
Formal notation to remove the properties of this special character, you will certainly have a lot of benefits in the future!

Example three: I would like to list all the know, regardless of the case, and the travel number:
[Root @test/root]# grep-ni ' know ' regexp.txt
3:* I don ' t know how does this *
4:# I don ' t Know how does this $
5:^ I don ' t KNOW how does this @
If you ignore the difference in case, add the-I parameter! The column travel number is reached by-n!

 

--------------------------------------------------------------------------------

Special characters of formal notation (charaters) and egrep directives
 
Formal notation is more than looking for simple strings, and he has quite a bit of extra functionality to provide us with a combination of users, parsing strings, let's start by listing a few common formal notation special characters, with the file you just downloaded, and then use the example on the right to practice it!
Special character representation meaning and example
^word meaning: the character to be searched at the beginning of the line
Description
Search word for this string, "and word must be the beginning of this line."
Example:
Find the line that starts with I in the file and ignore the case:
[Email protected] root]# grep-in ' ^i ' regexp.txt
2:i want to "Happy" and <Happy> and/happy/here.
12:i'll shutdown my mechine
word$ meaning: characters to be searched at the end of the line
Description
Search word for this string, and word must be the end of the line for this line
Example:
Because the file provided by the bird brother is actually the product under DOS file, so the word end will have the character of ^m, therefore, we first to tr this instruction will this character to remove, and then find the word tail for ye of that line, and show out! For more information on the use of the TR directives, we will explain them later.
[email protected] root]# cat regexp.txt|tr-d ' \ R ' grep-n ' ye$ '
13:bye bye
. Meaning: Represents "one" any character, must have "one character" exists
Description
This "." In the formal notation is commonly used, representing the "one" any character, not 0 or more, just good is "one".
Example:
I'm going to take the line with E and T in the file and have a character in the middle. E and T must have a character in the middle of the existence is a success Oh! For example, the following example:
[Email protected] root]# grep-n ' e.t ' regexp.txt
7:now, I'll eat my food
10:tesaaaaatest
11:quit Bbbbqqa Testglgs
\ skip symbol turns special characters into normal characters
? Any one "single" character
* Random number of characters
The characters in the [list] List
The characters in the range in the [Range] List
[^list] Reverse selection, contrary to [list]
[^range] Reverse selection, as opposed to [range]
\{n\} with the previous same word floating continuously n
\{n,m\} with the previous same word floating continuously n-m
 
It is important to note that the "special character of formal notation" is not the same as the "universal character" that is normally entered in the instruction column, for example, in the universal character, * represents the meaning of 0 ~ infinite number of characters, but in the formal notation, * is the meaning of repetition of the previous character ~ the meaning of use is not the same, Don't confuse it! (Brother Bird when I first touched the formal notation it was easy to confuse!) Because this is the most error-prone place for novices, be very careful! )
Q: I want to know, under/etc, as long as the line containing any one character of XYZ three characters is listed, how to do?
For:
"As long as" contains X or Y or Z will be the line out, therefore, our range is very wide! This time will need to use [] this boom! Do you remember the use of brackets? That is, "in the brackets, I would like to represent a character!" "And this bracket is a" representative ", can be a string of words can also be a few discontinuous words! Here we only need one of XYZ, so we can write this:
grep [XYZ]/etc/*
So long as in each row, as long as X or Y or Z is found, it will be printed out! This is "totally different" from grep xyz/etc/*! Please think carefully about the sunglass!
Example: I want to know in/etc, as long as the first sentence is w-z to print him out?
For:
The same, use the brackets!
grep ^[w-z]/etc/*
 
Of course there are many other uses! After all, a formal notation with some of his attached instructions, almost a book, here is just to let everyone understand his benefits! That's why it's written here! Others, waiting for you to go to the details and discover!

--------------------------------------------------------------------------------

Diff
Syntax: [Root @test/root]# diff file1 File2
Parameter description:
Example:
[Root @test/root]# diff index.htm Index.html
Description
This is used to compare the contents of two files if there are inconsistent instructions!

--------------------------------------------------------------------------------

Awk
Syntax: [Root @test/root]# Last | awk ' {print $ ' "$ $} '
Parameter description:
Example:
[Root @test/root]#
Description
In construction

--------------------------------------------------------------------------------

Sed
Syntax: [Root @test/root]# Last | Sed 10,\ $d
Parameter description:
Example:
[Root @test/root]#
Description
In construction

Linux Learning materials-Basic formal notation (grep)

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.