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 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.201.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 action type sed common action types: Action p print text line (print) n remove one line (next) d delete (delete) s string Replace (subst) 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 the line containing the word apple (in shell, the 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'/\ <apple \>/P' sed_test.txt 1 apple a, B, d, f Delete the specified row (no-I here, no operation is performed on the original file, only output the processed result .): (1) Delete 2nd to 4 rows $ sed '2, 4d 'sed_test.txt 1 apple a, B, d, f 5 eat http://blog.csdn.net/xia7139 (2) delete the row containing apple 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.201.1 4 dog www.google.com 5 eat http://blog.csdn.net/xia7139 (2) Comment shell script (insert # At the beginning) $ 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 you do not write the start address and end address, all rows are 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 Unix two text processing tools awk and sed, I 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/123456 ': '<title> Scale-isometric polytopal graphs in hypercubes and Z <sub> n </sub>. </title> \ n', 'books/daglib/100': '<title> Discrete total l <sub> p </sub>-norm approximation problem for the function. </title> \ n', 'books/daglib/p/AveneauCFM11 ':' <title> A Framework for <I> n </I>-Dimensional Visibility Computations. </title> \ n', 'books/daglib /P/carter11': '<title> Using <I> Dungeons and Dragons </I> to Integrate Curricula in Classroom. </title> \ n', 'books/daglib/p/CasolaLRV11 ':' <title> Access Control in Cloud-on-Grid Systems: the <I> PerfCloud </I> Case Study. </title> \ n', 'books/daglib/p/chunkzdmz11': '<title> Reverse Engineer of Gene Networks with Application <I> in silico </I> Network. </title> \ n', 'books/daglib/p/chungk11': '<title> EQC Mapp Ing for Functional Classes of <I> Saccharomyces cerevisiae </I> Genes wssion. </title> \ n', 'books/daglib/p/goldman11 ': '<title> A Model for Computer Graphics Based on Algebra for \ xe2 \ x84 \ x9d <sup> 3 </sup>. </title> \ n', 'books/daglib/p/liz11': '<title> Line Geometry over \ xe2 \ x84 \ x9d <sup> 3, 3 </sup>, and Stewart Platforms. </title> \ n', 'books/daglib/p/liestol11': '<title> <I> Situated Simulations </I> Reality and Designing a Narrative Space. </title> \ n'} Now, you only need to use the sed command to remove the items in each row, such as labels: [plain] $ sed-e's/<title> // g; s/<\/title> // G'-e's/<I> // g; s/<\/I> // G'-e's/<sub> // g; s/<\/sub> // G'-e's/<sup> // 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 Integrate 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', 'B Ooks/daglib/p/ChungK11 ': 'eqtl ing for Functional Classes of Saccharomyces cerevisiae 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 ry over \ xe2 \ x84 \ x9d3, 3, and Stewart Platforms. \ n', 'books/daglib/p/liestol11': 'situated Simulations Between Reality and Designing a Narrative Space. \ n'} As shown in If you want to modify it 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.