Shell Script Learning notes (stream editor sed)

Source: Internet
Author: User
Tags printable characters

      sed means Stream editor used as a filter in shell scripts and makefile is very common

Sed not only supports the normal form. It has some other powerful features.

Let me give you a sample to see if there is any way to solve it.

How does the content in the above HTML file turn it into the following non-<> content through sed? I expect very few people here can get it done very quickly, this is a topic I met, took half an hour to get it done.

I used the SED, first to solve the problem to solve Jiechan it ^_^



The above knowledge is a sample, the following is to start introducing the use of the SED tool.

There are two ways to run SED first:

1. Sed option ' script ' file1 file2 ...
2. Sed option-f scriptfile file1 file2 ...

The ability to see SED support for multiple file operations is, in fact, a one-line resolution, just like running water, knowing that it runs to the end of the file. It's edit command

As:/pattem/action the pattern is the normal form, action is an edit operation, the following is a brief introduction to its use it.

1./pattern/p: Print lines matching pattern

Note that the SED default is to print all the processing that has been processed, so the above results will be printed and matched again and again, so at this point we need to

Use the '-n ' option to print the section we are interested in.


2./PATTERN/D: Delete lines that match pattern

It is important to note that while ' d ' is the deletion of a matching row, it does not delete the contents of the file's surface. In order for the changes to take effect in the SED, we

Need to use '-i ' option.


3./pattern/s/pattern1/pattern2/: Find the line that matches the pattern and replace the string with the first match pattern1 of the line with PATTERN2 4./pattern/s/pattern1/pattern2/G : Finds rows that match the pattern and matches all of the rows PATTERN1 string is replaced with pattern2

It is important to note the difference between the last ' G ' and the absence, and the effect of a character on the whole is actually very large.


5. Sed-i: The operation will change the original file

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">

The use of this parameter should be noted. It's really going to make the changes effective. Think about the project you've already written, and look at the way you've replaced some small places, but you're not careful.

Write wrong, the result will be the whole project of important code to replace, finally want to enter project press ' U ' restore (at this time by U restore is invalid), at this time you really have the urge to chop hands.


 6.

Addressing used to decide which rows to edit. The address can be in the form of a number, a formal representation, or a combination of both. Assuming no address is specified, SED processes all lines of the input file.

However, there are several cases of addressing:

the above-mentioned situation is based on a row to be able to print or delete a row or multiple lines, it is relatively simple to use, such as sed ' 1,3d ' file,

Sed-n ' 3p ' file, sed-n ' 2,5p ' file, etc. But the following conditions are more complex:

Of

1. Sed '/pattem1/,/pantem2/d ': Delete all lines including ' pattem1 ' and ' pattem2 ', as well as lines between them, from top

Can be seen in the examples.

2. Sde '/patten1/,nd ' : Delete the contents of rows to nth rows including ' pattem1 '

The above two points we need to go down a lot to try, because it is not easy to understand.


7. The regular form of the expression

At the beginning of the SED is said to support the form of the formula, but it is not practical, hey. Here's how to use it.

(1). ' ^ ', ' $ '

(2). '. ', ' * '

(3). ' \< '. ' \> '

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">


The following two methods of use are more specific. is the special function of SED

In the above statement, the test is saved as label 1 and can be referenced later using ' \1 '.

The following examples are not very clear.

Will make you clear:

&: Save the lookup string for reference in the replacement string: s/[0-9]/*&*/g Symbol & Representative Find string. This is to try it yourself, so that you will

More familiar with the use of the method, the following another sample to help understand.

The above is the basic use of sed, sed in fact supports a lot of commands and some options, such as:

Command:A\: Add one or more lines after the current line. Multiple rows except the last row. The "\" continuation is required at the end of each lineC\: Replaces the text in the current line with the new text after this symbol.

Multiple rows, except for the last row, need "\" to continue at the end of each lineI\: Inserts text before the current line. Multiple rows except the last row, the end of each line requires "\" To continue the row D Delete RowH: Copy the contents of the pattern space to the staging buffer (hold space)H: Append the contents of the pattern space to the staging buffer(hold space)G: Copy the contents of the staging buffer to the schema space, overwriting the original contentG: Append the contents of the staging buffer to the schema space, appended to the original contentL: List non-printable charactersP: Print Line N: Reads into the next input line and starts processing it from the next command instead of the first commandQ: End or exit sedR: Read input line from file! : Apply commands to all rows except the selected rowS: Replace with a string with aG: Global substitution within a rowW: Write the selected line to the fileX: Swap the contents of the staging buffer with the pattern spaceY: replace character with one character (you cannot use the Y command on a regular table)Options:-e: Make multiple edits. That is, when you apply multiple SED commands to an input row-N: Cancel default output-F: Specifies the file name of the SED script

I don't have one by one explanations for these options, and the people who often need to use them will have to delve into it.

For some advanced uses of SED, such as in SED

The concept of "pattern space" and "state space" should be understood, and some of the commands given above are for these two types of space, and I'm not

Have a good study, so did not write out, hope understanding.

Shell Script Learning notes (stream editor sed)

Related Article

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.