sed tool execution principle; The parameters of sed and the common operation method of action; Addressing Pattern space and maintain space; Using labels
1. First look at what is sed?
The SED is called the Stream editor, and it is common to use it as a filter in shell scripts and makefile, which is to introduce the output of the previous program into the input of the SED and convert it into another format output after a series of editing commands. Let's look at the working mode of this schematic diagram:
The format of the edit command is, sed/pattern/action
After sed processing the file content has not changed, except for the use of redirected storage output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file; sed defaults to match the basic specification basically! That is, similar to () {} | Special characters need to be escaped, otherwise they will not be recognized, or they can be used in extended mode.
2. The following highlights the parameters of SED and how to operate the action
(1)-n parameter, action for p command
(2) The action of the D command
(3)/pattern/s/pattern1/pattern2/: Find the line that matches the pattern and replace the string with the first match pattern1 of the line with PATTERN2
/pattern/s/pattern1/pattern2/g: Find the line that matches the pattern and replace all strings that match pattern1 in the row with pattern2
This operation does not know whether people will associate to the Vim editor in the bottom row mode search, they are similar.
Here's a summary of the parameters in sed and how to do it:
[Plain] View plain copy
<strong> parameter selection:
-N: The general sed command will output all data to the screen, and if you add the-n option, only the lines processed by the SED command will be output to the screen.
-e: Allows multiple sed command edits to be applied to the input data.
-I: Writes the result of the modification directly to the file that reads the data, not the screen output. (1. Modification of documents; 2. Can be viewed by cat)
-F: Specifies the file name of the SED script.
Action
A: Append, adding one or more rows after the current line.
C: Line substitution, replacing the original data row with the string following C.
I: INSERT, insert one or more rows before the current line.
P: Print, output the specified line.
S: string substitution, replacing another string with one string. The format is "row range s/old string/new string/g" (if you do not add g, it means replacing only the first matched string per line).
</strong>
The above has not been practiced, the interest can go to try!
3. To introduce the addressing in SED
Addressing is used to determine which rows in a file are to be edited, in the form of numbers, regular expressions, or a combination of both. If no address is specified, SED processes all lines of the input file. Here are some examples:
The line between sed '/start/,/end/d ' file #删除包含 ' start ' line and ' End ' line
Sed '/start/, 10d ' file #删除包含 ' start ' to the contents of line tenth
4. Pattern Space and hold space
(1) Maintain space: used to store data, the equivalent of a warehouse, it can not process data;
(2) Mode space: The data is processed specifically by the unit of behavior.
In general, if you do not display the use of some options, it will not be used to maintain space.
[Plain] View plain copy
<span style= "color: #000000;" ><strong> command:
G: Copies the contents of the hold space to the pattern space, overwriting the original value of the pattern space.
G: Appends the contents of the hold space to the pattern space.
H: Copies the value of the pattern space to the hold space, overwriting the original value of the remaining space.
H: Appends the value of the pattern space to the hold space.
D: Delete all rows of the pattern space and read the next line to the pattern space.
D: Delete the first line of the pattern space, without reading the next line to the pattern space.
N: The output mode space row, read the next line to replace the current mode space of the row, and then execute the next processing command instead of the first command.
N: Reads the next line and appends to the pattern space line, where there are two lines in the pattern space.
x: Swap mode space and preserve the contents of the space.
</strong></span>
Example 1: Adding a blank line after each line
Example 2: The process of simulating reverse (TAC) printing with SED
Example 3. Append match line to end of file
Example 4: Turning a column of content into one row
Example 5: Finding the sum of 1-100
Example 6: Print out odd and even lines
5. Using labels
[Plain] View plain copy
: a means label A;
BA means jump to a tag;
$ represents the last line;
! Indicates that no subsequent action is done
Therefore, $!ba indicates that the last line does not have to jump to a tag to end this operation.
Here's an example:
Finally, add one point:
[Plain] View plain copy
Like grep, SED also supports special meta characters for pattern lookups and substitutions. The difference is that the regular expression used by SED is the pattern enclosed by the slash line "/".
If you want to change the regular expression delimiter "/" to another character, such as O, simply add a backslash before the character, followed by the regular expression after the character, followed by the character. Example: Sed-n ' O^56op ' datafile
^: line locator/^my/matches all lines starting with my;
$: Line tail locator/my$/matches all lines ending with my;
.: Matches a single character, except newline characters/M. y/matches the line containing the letter m, followed by two arbitrary characters, and then the letter Y;
*: Match 0 or more leading characters/test*/matches the line containing the string tes, followed by 0 or more T-letters;
[]: matches any character within the specified character group/t[ee]st/matches the line containing test or test;
[^]: matches any character that is not within the specified character group/t[^ee]st/matches a string that begins with T, but the character preceding the ST is not a line of E or E;
&: Save the lookup string to refer to the s/test/*&*/g symbol in the replacement string & represent the find string. Test will be replaced with *test*
<: Word? The first locator/<my/matches the line containing the word that begins with my;
: The ending locator/my>/matches the line containing the word ending with my;
X{M}: Consecutive m x such as:/9{5}/matches a row containing 5 consecutive 9;
X{m,}: At least m x such as:/9{5,}/matches a row containing at least 5 consecutive 9;
X{m,n}: At least m, but not more than n x such as:/9{5,7}/matches rows containing 5 to 7 consecutive 9. There is also a unit match--The problem of substitution:
An article to learn the SED of Shell tool article