Sed for Unix text processing tools

Source: Internet
Author: User
Like the awk mentioned above, sed is also a Unix text processing tool. Sed is short for StreamEditor. it can be filtered based on pattern matching (filtering is to find rows that meet certain conditions in the file) modify the text (that is, match the found... like the awk mentioned above, sed is also a Unix text processing tool. Sed is the abbreviation of Stream Editor. it can be filtered based on pattern matching (the so-called filtering is to find rows that meet certain conditions in the file) modify the text (that is, modify the content that meets the conditions found ). 1. sed command format 1.1 The basic format of sed command has three main usage forms: sed 'edit directive 'file 1 file 2 ......: Output the processing result sed-n 'edit directive 'file 1 file 2 ......: Used to output only the lines affected by the editing command sed-I 'edit command 'file 1 file 2 ......: Used to directly modify text content on a text file (modifying files on a physical disk) 1.2 The edit command editing command consists of two parts: there are two addresses separated by commas (,) (or there is no comma, there is only one address), which indicates the starting position to the end position of the text to be processed; followed by the operation type to be performed. The format is as follows: [start address [, end address] operation type. if multiple edit commands are used in a sed command, the edit commands must be separated, you can also place multiple edit commands in multiple single quotes. in this case, you must add a-e before each single quotes. The following is a simple example: [plain] $ cat sed_test.txt 1 apple a, B, d, f 2 boy alsdjf, apple, kdjf 3 cat 163.2..1 .1 4 dog www.google.com 5 eat http://blog.csdn.net/xia7139 $ Sed-n '2, 5' sed_test.txt 2 boy alsdjf, apple, kdjf 3 cat 163.2.201.1 4 dog www.google.com 5 eat http://blog.csdn.net/xia7139 $ Sed-n '5' sed_test.txt 5 eat http://blog.csdn.net/xia7139 $ Sed-n-e '2p'-e '5' sed_test.txt 2 boy alsdjf, apple, kdjf 5 eat http://blog.csdn.net/xia7139 1.3 Common sed operations are as follows: Action p prints a line (print) n removes a line (next) d deletes (delete) s string replace (substitude) a. append a new text (append) 2. the example below the example is the operation of the file sed_test.txt: [plain] using a regular expression: (1) output from the first line containing kdjf to the last line ($ represents the last line) $ sed-n'/kdjf/, $ P' sed_test.txt 2 boy alsdjf, appleapple, kdjf 3 cat 163.2.201.1 4 dog www.google.com 5 eat http://blog.csdn.net/xia7139 (2) output of a line containing the word apple (in shell, a word refers to a string with spaces or other punctuation marks. Use a regular expression <> to define a word. use this symbol in sed to escape the word .) $ Sed-n'/\/p' sed_test.txt 1 apple a, B, d, f delete the specified row (here there is no-I, no operation is performed on the original file, only output the processed result .): (1) delete lines 2nd to 4 $ sed '2, 4d 'sed_test.txt 1 apple a, B, d, f 5 eat http://blog.csdn.net/xia7139 (2) delete the row containing appleapple and the last row ($) $ sed '/appleapple/d; $ d' sed_test.txt 1 apple a, B, d, f 3 cat 163.2.201.1 4 dog www.google.com (3) delete excluded (! Indicates that the row that does not meet the condition is selected.) the row of apple (so that only the row containing apple is left) $ sed '/apple /! D 'sed_test.txt 1 apple a, B, d, f 2 boy alsdjf, appleapple, and kdjf replace the specified text: (1) replace line 1-4 apple with AMAZON. S stands for replacement, and g stands for replacement if two apples appear in one row. $ Sed '1, 4S/apple/AMAZON/g' sed_test.txt 1 AMAZON a, B, d, f 2 boy alsdjf, AMAZONAMAZON, kdjf 3 cat 163.2..1 .1 4 dog www.google.com 5 eat http://blog.csdn.net/xia7139 (2) comment the shell script (insert at the beginning of the line #) $ sed '1, 3 s/^/#/g' sed_test.txt #1 apple a, B, d, f #2 boy alsdjf, appleapple, kdjf #3 cat 163.2.201.1 4 dog www.google.com 5 eat http://blog.csdn.net/xia7139 (3) delete the string apple (if the start address and end address are not specified, all rows will be deleted by default .) $ Sed's/apple // g' sed_test.txt 1 a, B, d, f 2 boy alsdjf, kdjf 3 cat 163.2.201.1 4 dog www.google.com 5 eat http://blog.csdn.net/xia7139 The above two articles introduce the two Unix text processing tools awk and sed, and hope to help you. 3. preliminary understanding of the power of sed. for example, you have a file with the following content: [python] test.txt: {'books/daglib/100 ':' Scale-isometric polytopal graphs in hypercubes and Zn.\ N', 'books/daglib/100 ':' Discrete total lp-norm approximation problem for the function.\ N', 'books/daglib/p/AveneauCFM11 ':' A Framework for <I> n </I>-dimen=visibility Computations.\ N', 'books/daglib/p/carter11 ':' Using <I> Dungeons and Dragons </I> to Integrate Curricula in Classroom.\ N', 'books/daglib/p/CasolaLRV11 ':' Access Control in Cloud-on-Grid Systems: The <I> PerfCloud </I> Case Study.\ N', 'books/daglib/p/chunkzdmz11 ':' Reverse Engineer of Gene Networks with Application <I> in silico </I> Network.\ N', 'books/daglib/p/chungk11 ':' EQTL ING for Functional Classes of <I> Saccharomyces cerevisiae </I> Genes wssion.\ N', 'books/daglib/p/goldman11 ':' A Model for Computer Graphics Based on Algebra for \ xe2 \ x84 \ x9d3.\ N', 'books/daglib/p/liz11 ':' Line Geometry over \ xe2 \ x84 \ x9d3, 3, and Stewart Platforms.\ N', 'books/daglib/p/liestol11 ':' <I> Situated Simulations </I> Between Reality and Designing a Narrative Space.\ N'} now, you only need to use the sed command: [plain] $ sed-e's/ // G; s/<\/title> // g'-e's/ // G; s/<\/I> // g'-e's // g; s/<\/sub> // g'-e's // g; s/<\/sup> // g' test.txt {'books/daglib/100': 'scale-isometric polytopal graphs in hypercubes and Zn. \ n', 'books/daglib/100': 'discrete total lp-norm approximation problem for the function. \ n', 'books/daglib/p/aveneaucfm11': 'A Framework for n-Dimensional Visibility Computations. \ n', 'books/daglib/p/carter11': 'Using Dungeons and Dragons to In Tegrate Curricula in Classroom. \ n', 'books/daglib/p/casolalrv11': 'Access Control in Cloud-on-Grid Systems: The PerfCloud Case Study. \ n', 'books/daglib/p/chunkzdmz11': 'reverse Engineer of Gene Networks with Application in silico Network. \ n', 'books/daglib/p/chungk11': 'eqtl ING for Functional Classes of Saccharomyces cerevisiae Genes wssion. \ n', 'books/daglib/p/goldman11': 'A Model for Comput Er Graphics Based on Algebra for \ xe2 \ x84 \ x9d3. \ n', 'books/daglib/p/liz11': 'Line Ry over \ xe2 \ x84 \ x9d3, 3, and Stewart Platforms. \ n', 'books/daglib/p/liestol11': 'Situated Simulations Between Reality and Designing a Narrative Space. \ n'} to be modified in the original file, you only need to add the-I parameter.

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.