The SED command line format is:
sed [-nefri] ' command ' input text/file
Common options:
-N: Cancels the default output, using quiet (silent) mode. In the usage of general sed, all data from stdin is generally listed on the screen. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.
-E: Multiple edits, which are used when multiple SED commands are applied to the input line. Action editing of SED directly in command-column mode
-F: Specifies the file name of the SED script. Directly write the SED action in a file, and-f filename to perform the SED action within the filename
-r:sed's actions support the syntax of an extended regular expression. (the default is the underlying regular expression syntax)
-I: Directly modifies the contents of the read file instead of the screen output
Common commands:
A: New, a can be followed by a string, and these strings will appear on a new line (the next line)
C: Replace, C can be followed by a string, these strings can replace the line between N1,N2
D: Delete, because it is deleted, so D usually does not pick up any content after
I: Insert, I can be followed by a string, and these strings will appear on a new line (the current line)
P: Print, that is, print out a selected material. Usually P is used in conjunction with the parameter sed-n
S: Replace, can be directly to replace the work. Usually this s action can be paired with regular expressions. such as 1,20s/old/new/g
Addressing
Addressing is used to decide which rows to edit. The address can be in the form of a number, a regular expression, or a combination of both. If no address is specified, SED processes all lines of the input file. The address is a number that represents the line number; a "$" symbol, which indicates the last row. For example:
sed -n ‘3p‘ datafile 只打印第三行
|
Displays only the file contents of the specified line range, for example:
# View only lines 100th through No. 200 of the file
Sed-n ' 100,200p ' Mysql_slow_query.log
Addresses are separated by commas, the address that needs to be processed is the range between the two lines, including both. Ranges can be represented by numbers, regular expressions, or a combination of both. For example:
sed ‘2,5d‘ datafile #删除第二到第五行 sed ‘/My/,/You/d‘ datafile #删除包含"My"的行到包含"You"的行之间的行 sed ‘/My/,10d‘ datafile #删除包含"My"的行到第十行的内容
|
Example: (Suppose we have a file named ab)
Delete a row
[[email protected] ruby] # sed ' 1d ' ab #删除第一行
[[email protected] ruby] # sed ' $d ' ab #删除最后一行
[[email Protected] ruby] # sed ' 1,2d ' ab #删除第一行到第二行
[[email protected] ruby] # sed ' 2, $d ' ab #删除第二行到最后一行
Show a row
. [[email protected] ruby] # sed-n ' 1p ' ab #显示第一行
[[email protected] ruby] # sed-n ' $p ' ab #显示最后一行
[[email protected] ruby] # sed-n ' 1,2p ' ab #显示第一行到第二行
[[email protected] ruby] # sed-n ' 2, $p ' AB #显示第二行到最后一行
Querying using a pattern
[[email protected] ruby] # sed-n '/ruby/p ' ab #查询包括关键字ruby所在所有行
[[email protected] ruby] # sed-n '/\$/p ' AB #查询包括关键字 $ where all lines, using backslashes \ Shielding special meaning
Add one or more lines of string
[email protected] ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
[[email protected] ruby] # sed ' 1a drink tea ' ab #第一行后增加字符串 "Drink Tea"
Hello!
Drink tea
Ruby is me,welcome to my blog.
End
[[email protected] ruby] # sed ' 1,3a drink tea ' ab #第一行到第三行后增加字符串 ' drink tea '
Hello!
Drink tea
Ruby is me,welcome to my blog.
Drink tea
End
Drink tea
[[email protected] ruby] # sed ' 1a drink tea\nor coffee ' ab #第一行后增加多行, using line break \ n
Hello!
Drink tea
or coffee
Ruby is me,welcome to my blog.
End
Instead of one row or more rows
[[email protected] ruby] # sed ' 1c Hi ' AB #第一行代替为Hi
Hi
Ruby is me,welcome to my blog.
End
[[email protected] ruby] # sed ' 1,2c Hi ' ab #第一行到第二行代替为Hi
Hi
End
Replace a section in a row
Format: sed ' s/string to replace/new string/g ' (the string to replace can be used with regular expressions)
[[email protected] ruby] # sed-n '/ruby/p ' ab | Sed ' s/ruby/bird/g ' #替换ruby为bird
[[email protected] ruby] # sed-n '/ruby/p ' ab | Sed ' s/ruby//g ' #删除ruby
Insert
[[email protected] ruby] # sed-i ' $a bye ' ab #在文件ab中最后一行直接输入 "Bye"
[email protected] ruby]# Cat AB
Hello!
Ruby is me,welcome to my blog.
End
Bye
Replace:
-E is an edit command that is used in cases where SED performs multiple editing tasks. All edit actions are applied to the line in the pattern buffer before the next line begins editing.
sed -e ‘1,10d‘ -e ‘s/My/Your/g‘ datafile
#选项-e用于进行多重编辑。第一重编辑删除第1-3行。第二重编辑将出现的所有My替换为Your。因为是逐行进行这两项编辑(即这两个命令都在模式空间的当前行上执行),所以编辑命令的顺序会影响结果。
# Replace two or more spaces for one space
Sed ' s/[' []*//g ' file_name
# Replace two or more spaces as delimiters:
Sed ' s/[' []*/:/g ' file_name
# If the Space and tab coexist, replace with the following command
# Replace with spaces
Sed ' s/[[:space:]][[:space:]]*//g ' filename
# Replace with delimiter:
Sed ' s/[[:space:]][[:space:]]*/:/g ' filename
==============
sed command calls :
type commands at the command line, insert the SED command into the script file, and then invoke sed, insert the SED command into the script file, and make the SED script executable
sed [option] sed command input file To use the SED command at the command line, the actual command is to enclose the single quotation mark
sed [option]-F SED script file input file use sed script file
sed script file [option] input file The first line of the SED script file with the SED command interpreter
option is as follows:
n does not print; SED does not write edit rows to standard output, the default is to print all lines (edited and not edited), the P command can be used to print the edit line
c the next command is to edit the command, add this option when using multiple edits
F If you are invoking the SED script file, use this option to inform SED that a script file supports the SED command, such as
sed-f myscript.sed input_file Here myscript.sed is the file that supports the SED command
use the redirect file to save the SED output
How to position text in text using sed :
x x is a line number, such as 1
X, y indicates that the line number range is from 2nd to Y, as indicated by line 2,5 from line 5th
/pattern/query for rows that contain patterns, such as/disk/or/[a-z]/
/pattern/pattern/queries a row that contains two patterns, such as/disk/disks/
/pattern/,x queries for rows containing patterns on a given line number, such as/disk/,3
x,/pattern/matches rows by line number and pattern query, such as 3,/disk/
x,y! Query does not contain rows with the specified line number x and Y
basic sed Edit command:
P Print matching line c/replace positioned text with new text
= Show file line number s replace the pattern with the replacement mode
A/Append new text message after locating line number R read text from another text
i/inserting new text information after locating the line number W write text to a file
D Delete Anchor Line Q exit when the first pattern match is complete or exit immediately
L Display control character y transfer character equivalent to octal ASCII code
N reads the next line of text from another text and appends the command group to the next line {} on the anchor row
G Paste the pattern 2 into the/pattern n/
basic SED programming examples:
Display line using P (rint): sed-n ' 2p ' Temp.txt only shows line 2nd, using option N
Print Range: sed-n ' 1,3p ' temp.txt print lines 1th to 3rd
Print mode: Sed-n '/movie/' p temp.txt print lines with movie
Use pattern and line number query: Sed-n ' 3,/movie/' P temp.txt only find movie in line 3rd and print
Show entire file: Sed-n ' 1,$ ' P temp.txt $ for last line
Any character: Sed-n '/.*ing/' P temp.txt Note is. *ing, not *ing
Print line number: Sed-e '/music/= ' temp.txt
Additional text: (Create sed script file) chmod u+x script.sed, runtime./script.sed temp.txt
#!/bin/sed-f
/name1/a/#a/means the text is added here.
Here, ADD NEW line. #添加的文本内容
Insert text:/name1/a/change to 4 I/4 for line number, I insert
Modified text:/name1/a/change to/name1/c/will modify the whole line, C modify
Delete text: sed ' 1d ' temp.txt or sed ' 1,4d ' temp.txt
Replace text: sed ' s/source/okstr/' temp.txt replace source with Okstr
Sed ' s//$//g ' temp.txt all the $ symbols in the text are removed
Sed ' s/source/okstr/w temp2.txt ' Temp.txt writes the replaced record to the file Temp2.txt
Replace modified string: sed ' s/source/' ADD before "&/p ' Temp.txt
The result will precede the source string with "add before", where & represents the source character found and is saved
SED results written to file: Sed ' w temp2.txt ' Temp.txt
Sed '/name/w temp2.txt ' temp.txt
Read text from file: sed '/name/r temp2.txt ' temp.txt
Add text at the end of each column: sed ' s/[0-9]*/& pass/g ' temp.txt
Transfer value from Shell to sed: echo $NAME | Sed "s/go/$REP/g" Note the need to use double quotation marks
Quick Line Command :
' s//.$//g ' Delete end line with a period
'-e/abcd/d ' delete lines containing ABCD
' s/[][][]*/[]/g ' Delete one to the sky, with a space instead
' s/^[][]*//g ' to delete the beginning of a space
' s//. [][]*/[]/g ' Delete period followed by two or more spaces, with a space instead of
'/^$/d ' Delete empty lines
' s/^.//g ' removes the first character, the difference ' s//.//g ' removes all periods
' s/col/(.../)//g ' Delete the three letters immediately following COL
' s/^////g ' removes the first in a path/
///////////////////////////////////////////////////////////////////////
, using a period to match a single characterPeriod "." can match any single character. “.” You can match a string header or any character in the middle. Assuming that a text file is being filtered, for a script set with 1 0 characters, the first 4 characters after x C are required, the matching operation is as follows: .... X C ....
2. Match the string or character sequence at the beginning of the line with ^^ only allow to match characters or words at the beginning of a line. The 4th character at the beginning of the line is 1, and the matching operation is expressed as: ^ ... 1
3. Match strings or characters at the end of a rowIt can be said that in contrast to ^, it matches a string or character at the end of a line, and the $ symbol is placed after the matching word. If the word J E T 0 1 is matched at the end of the line, the operation is as follows: J E T 0 1 $ If you only return a row that contains one character, the operation is as follows: ^. $
4. Use * to match single character or its repeating sequence in a stringUse this special character to match any character or string that repeats multiple expressions.
5. Use/block the meaning of a special characterSometimes you need to find some characters or strings, and they contain a character that the system specifies as a special character. If you want to match in the regular expression with *. p A s end of all files, can do the following:/*/. P a S
6. Use [] to match a range or setUsing [] matches a specific string or set of strings, you can use commas to separate the different strings to match in parentheses, but this is not mandatory (some systems promote the use of commas in complex expressions), which can increase the readability of the pattern. Use "-" to denote a range of strings, indicating that the string range starts with the character "-" to the left and ends with the "-" right character. Assuming that you want to match any number, you can use: [0 1 2 3 4 5 6 7 8 9] to match any letter, use: [A-z] indicates a range of letters from a-Z, A-Z.
7. Number of occurrences of matching pattern results using/{/}Use * to match all matching results any time, but if you specify the number of times you should use/{/}, there are three forms of this mode, namely:
pattern/{n/} match pattern appears n times.
pattern/{n,/} The matching pattern appears at least n times.
PATTERN/{N,M} The matching pattern occurs between N and M times, N, M is 0-2 5 5 in any integer.
Match letter A appears two times and ends with B, and operates as follows: A/{2/} B matches a value of a a B matches a at least 4 times, using: A/{4,/} b
===============
The replacement single quotation marks are empty:
It can be written like this:
Sed ' s/' ' "'" '//g '
Sed ' s/' \ '//g '
Sed s/\ '//g
The use of SED in Linux