Shell script learning-6 (SED command learning)

Source: Internet
Author: User

5 is there an option in sed? Is there anything that is commonly used?

Sed's most important part is the command part. If you have played this part, you are already very good. The premise of playing this part is to play with the regular expression. Even so, the-n option of the SED option is still very important. Understanding it is also crucial for you to improve the SED level.

As mentioned above, sed will output the rows in the mode space to the standard output after processing, which is the default processing method. That is to say, unless you use the D command to delete this line, it will be output to the screen more or less, no matter what shape it is replaced. The previous example demonstrates all of this. You don't believe it? Here is an example that best illustrates the problem:

[Rocrocket @ rocrocket programming] $ cat roc.txt
1
2
3
4
5
[Rocrocket @ rocrocket programming] $ sed '/4/P' roc.txt
1
2
3
4
4
5

It can be seen that all except the original content is output, and the lines containing character 4 are repeatedly output. This is the working principle of sed. It will output the original row without any need, and then execute the subsequent action. Here we set P to print the row. This output is not what we want. What we actually want is to output only four rows.

If you use the-n option, you will find that the result is as expected:

[Rocrocket @ rocrocket programming] $ sed-n'/4/P' roc.txt
4

Option-N tells sed not to output randomly unless it explicitly indicates the row to be output. (Now you know that-N is amazing. ^_^)

-N is usually used with-P, which means to output the changed rows.

6. The command part is complicated. Can you summarize it a little bit?

In this case, the command part can be divided into two parts: the scope part and the processing method part.

The scope can be determined in two ways:

1 specify the number of rows: for example, 3, 5 indicates 3rd, 4th, and 5th rows; 5, $ indicates 5th rows to the last row;

2. Specify with pattern matching: for example,/^ [DD]/indicates that the first line of a matched row is not a row starting with D or D.

There are many commands available in the processing method section. Here are some of the most commonly used commands:

D: deletes a row.

P: print this row

R: Read the content of a specified file.

W: Write a specified file

A/: Insert a new line of content below

7. Let's give you a few examples to illustrate the typical sed application.

Example 1 show rows 10th to 20 of the test file: sed-n'10, 20p' Test

Example 2 convert lowercase X of all rows starting with D to uppercase X: sed '/^ [DD]/S/x/G' Test

Example 3 Delete the last two characters in each line: SED's/... $ // 'test

(Someone may ask why sed '/... $/d' test is not used? This is because/.. $/matches all rows with two characters at the end and deletes such rows. Obviously this is incorrect)

Example 4 Delete the first two characters of each row: SED's/.. // 'test

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.