Linux Basic Learning-day seventh (regular expressions and grep)

Source: Internet
Author: User
Tags control characters diff

2016-08-04

Content of the lesson:

Various text tools to view, analyze, and count text files (more, less, cat, cut, sort, WC, uniq, head, tail, paste, diff)

Grep

Regular expressions

Extending regular Expressions


"" All text viewing, analysis, statistics, etc. are simply changing the way standard output is displayed, and the contents of the original file are not changed

Cat [OPTION] ... [FILE] ... : File view, accept standard input

-N: Show line numbers

-e: Show End character

-A: Show all control characters

More [OPTIONS ...] FILE ... : Pagination View


[Email protected] ~]# more F1 F2 F3::::::::::::::f1::::::::::::::nihaomanizainalicahifanlemaddassada:::::::::.::: F2::::::::::::::NIHAOMANI1ZAINALICAHIFANLEMADDASS2ADA::::::::::::::F3:::::::::::::d SDASDASDADSADADADAASDSA

Less FILE ...: paged view, man help is called less command to view content

"" when opening multiple files, use the following command to switch between multiple files

: N-Browse next file

:p-View Previous file

The difference between more and less:

Both are File View command, the former view can only page back, and automatically exit after reading, the latter can press n/n to scroll through the page, while supporting the search, after reading will not automatically exit the file, you need to press Q to exit

Head [OPTION] ... [FILE] ...

-n#: Viewing the first # lines of a file

tail [OPTION] ... [FILE] ...

-n#: View the file's Countdown # line contents

-F: Real-time tracking display of new additions to files, common log monitoring

Cut [OPTION] ... [FILE] ...

-D: followed by the specified delimiter, the default is tab

-F: followed by the specified column

-C: cut by character

Paste [OPTION] ... [FILE] ... : Merge "multiple" files with row number columns to one line, cat can merge two consecutive files to display

-D: Specify the delimiter to display after merging

[Root[email protected] ~]# paste F1 f2nihaomahenhaonizainalizhelichifanlemachile[[email protected] ~]# paste-d* F1 f2nih Aoma*henhaonizainali*zhelichifanlema*chile[[email protected] ~]# paste-d: F1 F2 F3nihaoma:nihaoma: Dsdasdasdanizainali:ni1zainali:dsadcahifanlema:cahifanlema:adadaddassada:ddass2ada:asdsa

WC [OPTION] ... [FILE] ... : Statistics File Information

-L: Count rows

-C: Total Statistics bytes

-W: Count the total number of words

Sort [OPTION] ... [FILE] ... : Sort text information to display

-N: Sort by number size

-R: Reverse Display

-T: Delimiter (equivalent to the-D option of cut)

-K: Sorting through the first few columns of information

Uniq [OPTION] ... [FILE] ... : Remove duplicate front and back rows from input

-C: Number of occurrences of repeated rows

-D: Show only duplicate rows

Used together with the sort command to count the number of occurrences of the same row:

Sort Userlist.txt | Uniq-c

diff [OPTION] ... Files: Compare the differences between the "two" files

[[email protected] ~]# cat-n F1 f2 1nihaoma 2nizainali 3cahifanlema 4ddassada 5nihaoma 6ni1zainali 7cahifanlema 8ddass2ad A[[email protected] ~]# diff F1 f22c2< Nizainali---> ni1zainali4c4< ddassada---> Ddass2ada

2nd, 4 lines have a difference (change)

Regular expression: A pattern written by a class of "special characters" and "characters", in which some characters (metacharacters) do not denote character literal meaning, while the function of control or wildcard

Character classification: Character matching, number matching, position anchoring, grouping


Basic Regular Expressions:

Character Matching:

.: Denotes any "one character"

[]: Represents the "any single" character in parentheses within the match

[^]: denotes "Any single" character outside of the brackets

Number of matches:

*: Any number of occurrences of "its preceding character"

. *: Denotes any character of any length, equivalent to a wildcard *

\?: Indicates a match for "its first character" once or 0 times (optional)

\+: Indicates "its first character" appears at least once

\{m\}: Indicates "its first character" appears m times

\{m,n\}: Indicates "its first character" appears at least m times, up to N times

Location anchoring:

^: Indicates that subsequent characters are at the beginning of the line

$: Indicates that the preceding character is at the end of the line

\< or \b: The first anchor of the word, used for the left side of the word pattern

\> or \b: the ending anchor; for the right side of the word pattern

Group:

\ (spring\): The entire string inside the parentheses will be searched

\1,\2,\3: Indicates a reference to the preceding "section". parentheses "first character searched" This is the meaning of the grouping, that is, to refer to the previously searched string, if no reference is required, and the result is the same as without parentheses "

[[email protected] ~]# grep -n  "\ (L.. e\) " f11:he love his lover.2:she like her liker.3:he like his  lover.5:He love his lover and liker.6:She like her liker  And liker.7:he like his lover and like. [[email protected] ~]# grep -n  "L.. E " f11:He love his lover.2:She like her liker.3:He like his  Lover.5:he love his lover and liker.6:she like her liker and  liker.7:he like his lover and like. [[email protected] ~]# grep -n  "\ (L.. e\). *\1 " f11:he love his lover.2:she like her liker.5:he love his  lover and liker.6:She like her liker and liker.7:He like  His lover and like. 



To extend the regular expression:

Character matching (as with basic regular expressions):

.

[]

[^]

Number of matches (less "\" than the basic regular expression):

*

+

{m}

{M,n}

Positional anchoring (as with basic regular expressions):

^

$

\< \b

\> \b

grouping (less "\" than basic regular expressions)

(Spring)

or (the basic regular expression does not have this function)

C|cat: Indicates C, cat

(C|C) at: Indicates Cat

grep [OPTIONS] PATTERN [FILE ...] : a command that filters text using patterns defined by a basic regular expression

-I: Ignore case search

-O: Show only what you have searched for

-V: do not display the searched content

-N: Show line numbers

-E: Pattern search defined with extended regular expression

-A #: Displays the matched lines and the following # lines

-B #: Displays the matched line and its previous # line contents

-C #: Displays the matched line and its front and back # line contents

-E: Implementing a logical or relationship between multiple options "equals the ' | ' of an extended regular expression 】

[Email protected] ~]# grep-en "L (ove|ike)" F11:he love he lover.2:she like she liker.3:he like he lover.5:he love his Lover and Liker.6:she like she liker and liker.7:he like he lover and like. [Email protected] ~]# grep-e ' like '-e ' love ' F1he love his lover. She like her liker. He like his lover. He love his lover and Liker. She like her liker and Liker. He like his lover and like.


This article from the "6638225" blog, reproduced please contact the author!

Linux Basic Learning-day seventh (regular expressions and 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.