The sed of the Linux Text Processing tool

Source: Internet
Author: User

Concept:
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 a live multiple file.
Format:
sed [-nefri] ' command ' input text
Common options:
-N: Use Quiet (silent) mode. In the usage of general sed, all data from stdin is generally listed on the terminal. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed.
-E: Action editing of SED directly in instruction-column mode
-F: 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 grammar that extends the formal representation (the syntax of a few formal notation)
-I: Directly modify the contents of the read file instead of the screen output
Action Description:
[N1[,N2]] common commands: Do not necessarily exist, generally represents "select the number of lines of action", for example, if my action is required between 10 to 20 rows, then "10,20[Action Behavior"

Common commands:
A: Append, a can be followed by a string, and these strings will appear on a new line (the next line in the current)
C: Change, C can be followed by strings, these strings can replace the line between N1,N2
D: Delete, don't accept anything behind D.
I: Insert, I can be followed by strings, and these strings will appear on a new line (the current line)
P: Print, tell a selection of the data printed, usually with the parameter-N appears
S: Replace, can be directly replaced by the work, sed ' s/to replace the string/new string/flags ', here the modifier replaces the flags flags are:
G: Represents a global substitution within a row, and no g is usually replaced only when it first appears
A number between n:1-512 that replaces the nth occurrence of the specified pattern in this mode.
P: Content of print mode space
W: Writes the contents of the schema space to file

Example:
1. Delete a row
A.sed ' 2d ' file #删除第二行
b.sed ' $d ' file #删除最后一行
c.sed ' 1,5d ' file #删除第一行到第五行
D.sed ' 5, $d ' file #删除第五行到最后一行
e.sed '/my/,/you/d ' file #删除包含 "My" line to rows that contain "you"
f.sed '/my/,10d ' file #删除包含 "My" to the contents of line tenth
g.sed '/^$/d ' file #删除空行
2. Display a row
A.sed-n ' 2p ' file #显示第二行
B.sed-n ' $p ' file #显示最后一行
C.sed-n ' 1,5p ' file #显示第一行到第五行
D.sed-n ' 5, $p ' file #显示第五行到最后一行
3. Mode for Query
A.sed-n '/sed/p ' file #查询包括关键字sed所在的所有行
B.sed-n '/\$/p ' file #查询包括关键字 $ where all rows, using "\" Mask special meaning
4. Add one or more lines of string
A.sed ' 1a test ' file #在第一行后新增字符串 "test"
b.sed ' 1,3a test ' file #在第一行到第三行后新增字符串 "test"
C.sed ' 1a test\ntest ' file #在第一行后新增多行, using newline characters \ n
5. Replace one or more lines or parts of a row
A.sed ' 1c test ' file #第一行的内容替换为test
b.sed ' 1,2c test ' file #第一行到第二行的内容替换为test
c.sed ' 1,5s/a/a/' file #第一到第五中的首个a替换为A
d.sed '/b/s/a/a/g ' file #将有B的行中的a替换为A
6. Insert
A.sed-i ' $a test ' file #在最后一行直接输入test
7. Multi-point editing
A.sed-e ' 3, $d '-e ' s/test/test ' file #先删除第三行到最后一行的数据, and then replace "test" with "test", $ need to be separated from the regular expression, where the end of the line is represented
B.sed-e ' 3, $d; S/test/test ' File #效果同上
8. Scripts
A. The following command is saved in a text (sed-script)
3i\
~~~~~~~~~~~~~~~~~~~~~
3, $s/\ (hrwang\) is \ (mjfan\)/\2 is \1/
$a \
We'll love eachother forever!!
B. sed-f Sed-script file
The script above can also be further optimized.
A. Keep the following command in a SH script (sed-script)
#!/bin/sed-f
3i\
~~~~~~~~~~~~~~~~~~~~~
3, $s/\ (hrwang\) is \ (mjfan\)/\2 is \1/
$a \
We'll love eachother forever
B.. /sed-script file
Attention:
1.sed-i This action will be directly modified to the original file, with caution.
2.sed-e This action means multi-point editing
A 3.sed script is a series of SED commands written in a file. The script requires no extra space or text at the end of the command. If you have multiple commands in a row, separate them with semicolons. When executing a script, sed first copies the first line of the input file to the pattern buffer and then executes all the commands in the script. After each line has been processed, SED copies the next line of the file to the pattern buffer and executes all the commands in the script. When using sed scripts, no quotation marks are used to ensure that the SED command is not interpreted by the shell.
The 4.sed command can specify 0, one, or two addresses. Each address is a regular expression that describes a pattern, line number, or line addressing symbol. If no address is specified, the command is applied to each row, and if there is only one address, the command is applied to any row that matches the address, and if two addresses are specified with a comma, then the command is applied to the first and subsequent lines of the first address until the row of the second address (including this row) is matched. You can think of the first address as an enable action and a second address as a disable action, and if the address is followed by an exclamation point (!), then the command is applied to all rows that do not match that address.
Use of row positioning
The difference between C and s in the 5.sed command (C direct change, s replacement)

The sed of the Linux Text Processing tool

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.