Linux Learning-Text Processing tool SED

Source: Internet
Author: User
Tags processing text

The SED (stream editor) is a non-interactive flow editor that modifies the text flowing through it through a variety of transformations. However, by default, SED does not change the original file itself, but simply modifies it with the text of the SED command and prints the modified result to the standard output (that is, the screen). When the SED is processing text, it is in the behavioral unit, printing it immediately after each line is processed, and then processing the next line until the full-text processing is complete.

Prepare the following file Sed.txt

This was line 1,this is first linethis are line 2,the Second line,empty line followedthis are line 4,this is third linethis I S line 5,this are fifth line

The way to modify a file stream using SED is as follows:

sed [options] ' command ' file#options is a parameter that SED can accept #command is the command set of sed (altogether 25) #使用-e parameter and semicolon link multi-edit command # The parameter itself is just a simple parameter of sed, Indicates that the next string is resolved to the SED edit command # It is generally possible to ignore it, but the argument cannot be less when SED needs to pass multiple edit commands # The following example shows that when you change this to that, you also use the-e parameter before you change the line to line# two edit commands. , if there are more editing needs, and so on Sed-e ' s/this/that/g '-e ' s/line/line/g ' Sed.txt

#使用分号连接两个都编辑命令 # The above command can be rewritten with a semicolon: sed-e ' s/this/that/g;s/line/line/g ' Sed.txt

Delete

Use the D command to delete the specified row

#将file的第一行删除输出到屏幕 # Delete second line similar to Sed ' 1d ' Sed.txt

#由于sed默认不修改原文件, if you want to save the modified file you need to redirect Sed ' 1d ' Sed.txt > saved_sed.txt# if you want to directly modify the file, use the-i parameter # here will not have any output, but directly modify the source file, delete the first line #sed-i ' 1d ' file# deletes the specified range of rows (lines 1th through 3rd) #sed ' 1,3d ' Sed.txt

#删除指定范围的行 (first line to last row) #sed ' 1, $d ' sed.txt# emptied the file

#删除最后一行 #sed ' $d ' Sed.txt

#删除除指定范围以外的行 (keep line 4th only) #sed ' 4!d ' Sed.txt

#删除所有包含Empty的行sed '/empty/' d Sed.txt

#删除空行 #sed '/^$/d ' Sed.txt


Find and replace

Use the s command to replace the matched text content of a lookup with new text

#s命令用于替换文本, in this example, use line to replace line# Note that only the first line in each row is replaced by default, and only the content #sed ' s/line/line/' Sed.txt

#要想每行最多匹配2个line, and instead line, use the following method # Note that there are 3 lines in row 2nd, the first two are replaced, and the third one does not change Sed ' S/LINE/LINE/2 ' Sed.txt

#s命令利用g选项, you can complete the replacement of all matching values #sed ' s/line/line/g ' Sed.txt

#只替换开头的this为that #sed ' s/^this/that/' Sed.txt

Character substitution

Use the y command to convert characters by converting a series of character-by-address to another series of characters, with the following basic usage:

#该命令会将file中的O转换为N, L convert to e,d Convert to w# note the conversion character and the length of the converted characters are equal, otherwise sed cannot execute sed ' y/old/new/' file# The following command demonstrates converting the number 1 to a,2 to b,4 to C, 5 conversion to the usage of D Sed ' y/1234/abcd/' Sed.txt


Insert text

Use the I or a command to insert text, where I is inserted before the matching line, and a represents a match after inserting

#使用i在第二行插入文本sed ' 2 i Insert ' Sed.txt

#使用a在第二行后插入文本sed ' 2 a Insert ' Sed.txt


Read in text

Use the R command to read text from other files and insert matching lines

/ETC/PASSWD the contents of the #将 in the Sed.txt after the empty line (must have a blank line, otherwise does not take effect) sed '/^$/r/etc/passwd ' Sed.txt



Print

Use the P command to print, where you must add the-n parameter when using the SED command, indicating that rows that do not print are OK. Because SED works on a row-by-line basis, it has a large amount of output each time.

But there are some of these outputs that we don't need to see, but just need to output matching lines or processed rows.

#打印出文件中指定行sed-n ' 1p ' Sed.txt

#将the替换成THE #sed actually processed the second line, and the other lines did not deal with it because there was no match # but the SED works based on the flow, so all the lines that flow are printed out Sed ' s/the/the/' Sed.txt

#使用p命令, only the actual processed rows are printed, simplifying the output (using the-n parameter) sed-n ' s/the/the/p ' Sed.txt


Write a file

sed itself does not overwrite the original file by default, but only the text of the buffer is modified and output to the screen, so you want to save the file, in addition to using redirection or the-I parameter two ways, you can also use the W command to save the results to the external

Specify file

Sed-n ' W output ' Sed.txt #这里没有任何输出 because the output is redirected to a file

#文件output中的内容正是Sed the contents of the first two lines in a. txt file cat output


SED script

in normal work, some files may need to be analyzed regularly, this routine work often has some "standardized" operations, such as first remove all the blank lines in the file, and then replace some characters and so on. These processes are similar to the flow operations of the production line. In fact, these actions can be statically written to a file, and then invoke the SED command and use the-f parameter to specify the file, so that the series of actions can be ' loaded ' and applied to the specified file, which undoubtedly speed up the efficiency, this file is the SED script.

#该sed脚本的作用就是将全文的this改为THAT, and remove all empty rows cat sed.rules s/this/that/g/^$/d# Use the-f parameter to specify the script and apply it to sed.txt# from the output to see how the effect is performed sed-f Sed.rules Sed.txt


Summary of common commands for SED

Sed common commands


SED command Role
A Add text after matching lines
C Character conversions
D Delete Row
D Delete First row
I Add text to a matching line pinch face
H Copy the contents of a module to a storage space
H. Copy the contents of the Append module to the schema space
G To copy storage space content to a schema space
G Append the contents of the storage space to the pattern space
N
Reads the next input line and processes the new row with the next command
N Append the next input line to the MO Plate and insert a new row between the two
P Print a matching line
P Print the first line that matches
Q Exit SED
R Reading text from an external file
W Append Write file

The inverse of the match
S/old/new Replace the regular expression old with new
= Print the current line number


Common parameters for SED

Sed parameters Role
-E Multi-conditional editing
-H Help information
-N Do not output mismatched rows
-F

Specifying SED scripts

-V Version information
-I. Modify the original file directly


Regular expression matching used in sed


Metacharacters
Role
^ Matches the start of the line. Example:/^cat/matches all lines that start with cat
$ Matches the end of the line. Example:/cat$/matches all lines ending with cat
. Matches any line of non-newline characters. such as:/c.t/matches C after the access to any character, then the T
* Match 0 or any number of characters. If/*cat/matches a string of characters followed by all cat lines
[] Matches a character within a specified range. Example:/[cc]at/matches cat and Cat
[^] Matches characters not in the specified range. Example:/[^a-z]/matches a line that is not preceded by a case letter
\(..) \ Save the matching characters. such as s/\ (Love) able/\lrs/loveable is replaced with lovers
& Save search characters to replace other characters. Like S/love/**&**/,love into **love.
\< Anchor the beginning of the word. If/\<cat/matches a line that contains a word that begins with cat
/> Anchor the end of the word. If/cat/>\ matches a line that contains words ending with cat
X\{n\} Repeat character x,m times. Example:/o\{5\}/matches rows that contain 5 o
X\{m,,\} Repeat character X, at least m times, such as:/o\[5,\}/matches at least 5 rows of O
X\{n,m\} Repeat the character X, at least m times, no more than n times. If/o\{5,10\}/matches 5 to 10 o rows



Linux Learning-Text Processing tool SED

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.