The grep sed for the Linux Text Processing tool
In the Linux system, there is a philosophical thought: Under the Linux system, everything is file.
This shows how important the tool for text processing is, and we often use text processing tools such as GREP,SDE, as well as awk, which is called the "Three Musketeers", and in this blog focus on the concepts and usage of grep and SED, hoping to help you with your Linux learning path. , if there is insufficient, please correct me!
Directory
- grep Concept and Analysis
- grep command Options
- Regular expressions and extended regular expressions
- The concept and analysis of SED
- SED command options
grep Concept and Analysis
grep (Global search regular RE) and print out of the line, full search of regular expressions and print out the lines, is a powerful text search tool that can use basic regular expressions to search for text and print matching lines.
grep is a common and commonly used command, and its main function is to compare string data and then print out strings that match the user's needs, but be aware that grep is filtering data in "whole rows" when it looks for a string in the data.
The Egrep command is equivalent to GREP-E, which allows you to search for text using an extended regular expression and print a string that meets the user's needs.
The Fgrep command is equivalent to Grep-f, which uses a fixed string to search for text, but does not support a reference to a regular expression, so the command executes the fastest. Fgrep are fixed grep or fast grep, which treat all the letters as words, that is, the metacharacters in the regular expression returns to its own literal meaning, no longer special.
grep command Options
--color=auto: Coloring the text to match to a display
-V: Displays rows that are not matched by pattern
-I: Ignore character case
-N: Show matching line numbers
-C: Count the number of matching rows
-O: Show only the matching string
-Q: Silent mode, does not output any information
-A #:after, after # line
-B #:before, front # line
-c #:context, front and back # lines
-E: Implementing a logical or relationship between multiple options
Grep–e ' Cat '-e ' dog ' file
-W: Matches entire word
-E: Use ere
-F: Equivalent to Fgrep, does not support regular expressions
Regular expressions and extended regular expressions
(1) Introduction to Regular expressions
Regular expressions, also known as formal notation, regular notation (Regular expression, often abbreviated as regex, RegExp, or re in code), regular expressions use a single string to describe, match, a string of a set of syntactic rules of a series of symbols. In many text editors, regular expressions are often used to retrieve and replace text that is a pattern of symbols.
(2) meta-characters and definitions
^: Beginning of the line
$: End of line
.: Any single character
Any single character within []:[]
[^]: any single character in addition to []
:Previous character repeat indeterminate number
+:+ the preceding character repeats more than one indeterminate number of times
\?:? The preceding character repeats 0 or 1 times
\: Escape character
.: Any length character
{n}: The preceding character repeats n times
{N,}: The preceding character repeats more than n times
{m,n}: The preceding character repeats between M and N times
[: Alnum:]: Letters and numbers
[: Alpha:]: Represents any English uppercase and lowercase characters, i.e. A-Z, A-Z
[: Lower:]: lowercase letters
[: Upper:]: Uppercase
[: Blank:]: horizontal whitespace characters (spaces and tabs)
[: Space:]: All horizontal and vertical whitespace characters (than [: blank:] contain a wide range of
[: Cntrl:]: Non-printable control characters (backspace, delete, alarm ...)
[:d igit:]: decimal digits
[: Graph:]: printable non-whitespace characters
[:p rint:]: Printable characters
[:p UNCT:]: Punctuation
[: Xdigit:]: hexadecimal digits
(3) Extended regular expression meta-characters and definitions
. Any single character
[]: Specify a range of characters
[^]: Characters not in the specified range
: matches the preceding character any time
?: 0 or 1 times
+:1 Times or more
{m}: matches M-Times
{M,n}: At least m, up to N times
^: Beginning of the line
$: End of line
\<, \b: the first language
\>, \b: The end of the language
|: Or, a|b, match A or b
(): grouping, such as N (ame| AME)
() +: discriminant of multiple repeating groups, such as finding the end of a beginning B, with more than one XYZ a (XYZ) +b in the middle
The concept and analysis of SED
Sed is an online editor that processes a single line of content at a time. Processing, the current processing of the row is stored in a temporary buffer, called "pattern space", and then with the command to handle the buffer content, processing completed, the contents of the buffer output. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you are using redirected store output. SED is used primarily to edit one or more files.
Function: Mainly used to automatically edit one or more files, simplify the repeated operation of the file, write the conversion program, etc.
Usage: sed [option] ... ' Script ' Inputfile ...
SED command options
-N: does not output mode space content to the screen, i.e. does not print automatically
-e: Multi-point editing
-f:/path/script_file: Reading the edit script from the specified file
-R: Support for using extended regular expressions-i.bak: Backing up files and editing them in situ
Script: ' Address command '
D: Delete the line matching the pattern space and immediately enable the next round of loops
P: Print the current mode space content after appending to the default output
A []text: Append text after the specified line supports using \ n to implement multi-line append
I []text: inserting text in front of a row
c []text: Replace a line of behavior or multiple lines of text
W/path/somefile: Save the pattern matching line to the specified file
R/path/somefile: Reads the text from the specified file to the line in the pattern space
=: Prints line numbers for lines in pattern space
!: matching row in pattern space reverse processing
Sed Advanced editing commands
P: Print mode space start to \ n content, and append to default output
H: Overwrite the contents of the pattern space in the hold space
H: Append the contents of the pattern space to the hold space
G: Remove data from hold space to pattern space
G: Append content from hold space to mode space
x: Swap the content in the pattern space with the content in the hold space
N: Reads the next line of the matched row to overwrite the pattern space
N: Reads the next line of the matched line to the pattern space
D: Delete rows in the pattern space
D: If the pattern space contains a newline character, the text in the pattern space until the first line break is deleted, and the new input line is not read, and the loop is restarted using the pattern space of the composition. If the pattern space does not contain a newline character, the normal new loop is started as if the D command was emitted
Above is I in the Linux study of grep,sed usage and insight, if there is insufficient, please correct me!
The grep sed of the Linux Text Processing tool