Linux----->shell Advanced programming----SED applications

Source: Internet
Author: User

background

sed is a good file processing tool, itself is a pipeline command, mainly in the behavior of the unit processing, you can replace the data rows, delete, add, select and other specific work.

SED is intended to be a stream editor (Streameditor), which is common in shell scripts and makefile as filters, that is, the output of the previous program is introduced into the SED input, and a series of editing commands are converted to another format output. Sed and vi both originate from the early Unix Ed tools, so many SED commands are the same as the last command of VI.


the SED command line format is:  

sed option ' script ' file1 file2 ...

s Ed option-f scriptfilefile1 file2 ...


Common parameters:

--version

Show sed version

--help

Show Help Document

-N
Cancel default Output
-E Specifying multiple instruction executions
-I.
Modify the contents of the file (the original file content is not modified by default)
-R
Using an expanded regular expression

Output placeholders

A (Append)

Additional

I (insert)
Insert
D (Delete)
Delete
S (substitution)

Replace

P (print)
Print


The main introduction of so much, below began to practice (in practice out of the truth)

1. Show second line

Raw data

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/83/B1/wKiom1d6bh3gf_QxAAAQaqiayLw561.png "title=" 1.png " alt= "Wkiom1d6bh3gf_qxaaaqaqiaylw561.png"/>


[#20 #[email protected]~ 22:06:04] $sed ' 2p ' test.txt

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/83/B0/wKioL1d6bqeS_V5CAAATuPYlM6s384.png "title=" 2.png " alt= "Wkiol1d6bqes_v5caaatupylm6s384.png"/>

at this point we found [bbbbb.....bbb] multiple output line, proof 2p printed the second line, at this time we found that the original data is also printed, does not conform to test instructions.

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/B0/wKioL1d6b96B9d9fAAAMjn8ZR2A706.png "title=" 3.png " alt= "Wkiol1d6b96b9d9faaamjn8zr2a706.png"/>

Add the [-n] parameter at this time to print only the second line exactly



2. Use the-e multiple statements to output data for a specified row

Raw Data

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/B1/wKiom1d6cTvhIheiAAARIIHYiqY629.png "title=" 4.png " alt= "Wkiom1d6ctvhiheiaaariihyiqy629.png"/>

[#23 #[email protected]~ 22:06:04]$ sed-n-E ' 2p '-e ' 4p ' test.txt

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/B1/wKiom1d6ccTze_BgAAAPLotKpjE670.png "title=" 5.png " alt= "Wkiom1d6cctze_bgaaaplotkpje670.png"/>



3. The output assumes that there are 1-100 rows in a text (test.txt) that require the printing of 20-30 rows of data

First we use the loop mechanism to simulate 100 rows of data.

[#24 #[email protected]~ 22:06:04]$ for i in ' seq 100 '; Do echo $i >> test.txt; Done

View the number of file lines with Wc-l

[#25 #[email protected]~ 22:32:29] $WC-L test.txt

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M01/83/B1/wKiom1d6c8HijVZ3AAAPF_V-1ek560.png "title=" 6.png " alt= "Wkiom1d6c8hijvz3aaapf_v-1ek560.png"/>


The topic is implemented as follows

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M02/83/B0/wKioL1d6dEPStLVGAAAfWCssaMU001.png "title=" 7.png " alt= "Wkiol1d6depstlvgaaafwcssamu001.png"/>



Note: SED features more than this, due to time and production environment is limited, more detailed sed introduction article will be released later, please pay attention.


This article is from the "Sea" blog, be sure to keep this source http://lisea.blog.51cto.com/5491873/1795754

Linux----->shell Advanced programming----SED applications

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.