Overview of Getting Started with SED for linux commands

Source: Internet
Author: User

Sed:stream Editor,linux is a stream editor that mainly filters and replaces text content. Using the command man sed, you can see an introduction to the SED command

The following general guidelines:

As for the process of SED, the individual thinks that it is not necessary to understand the work process if we do not have to learn all the functions of sed. The vast majority of people, especially for those who want to get started as soon as possible, master how to use the basic SED function should be more important!

Learn about the use of SED commands, mainly in conjunction with the hints in man sed:

SED synopsis (outline): Format of the command

    • sed "option""script""Input_file"

The learning of SED is in the area of the above three marker colors, which are described in the following three areas:

the "option" field: The main purpose is to specify how the SED should render the results after the data has been processed (may not be properly described), such as how the results are displayed, whether the text itself is modified, etc.

Null: When you do not specify a parameter, sed outputs the text content that will be processed, that is, the text content of the file is displayed first.

-N: (silent, also known as silent mode) cancels the default output, which is the opposite of the above. Only the rows that have been processed are printed out, and the rows that do not match are output as they are.

-i:sed directly manipulate the text content, any content modification to it will change the file. (sed By default does not manipulate the text content of a file, but instead copies its contents into its own workspace for Operation )

Script field: The purpose of this field is to specify which rows to do with which actions!

The field is actually divided into two parts: Specify row + operation (Address+command), the former can be less, but the latter cannot be less!

    • How to show the line to be processed: address
    • What to do: command

  How do you represent a row? there are the following options: (90% of the main wording, individual less used not suitable for getting started understanding not written on)

  1. Number: Specify a digit directly, such as 5, that is, the line object of the operation is line 5th
  2. first~step: The direct example (three-in-one) refers to the match from the third line, followed by every 4 lines. Therefore, section 3,7,11, ... Row is the line that is being manipulated. First refers to the line to be matched, and step means that each step line is matched to the next line, and the Linux Man Handbook example is (one to one), matching all odd lines, as an example of a deeper impression
  3. $: Directly a $, matching the last line of text
  4. /regexp/ or \cregexpc : Matches a line that conforms to a regular expression, note:C can be any character just to ensure consistency! (emphasis here is not on regular expressions, unfamiliar to other notes)
  5. ADDR1,ADDR2: if (1,5) matches 1 to 5 lines, lines 1th and 5th will be matched to
  6. Addr1,+n : if (1,+5) matches line 1th, match 5 rows in the future, that is, match 6 rows altogether. Will match line ADDR1 and next n rows
  7. Addr1,~n : if (10,~8) matches lines 10th through 16th (the most recent one is a multiple of 8 ). Line that is about to match line ADDR1 and the last line number that is a multiple of n

  How do i indicate an operation? the following options are available: (60% main wording, parameters that involve keeping space are not included)

Allow parameters with no address or only 1 addresses:

    1. =: Prints the line that is being manipulated, that is, when the forward
    2. a \text: Appends the text content to the specified line in the form of a new row, enabling multiple-row insertion using \ n
    3. i \text: Append the text content in the form of a new line before the specified line, enabling multi-line insertion using \ n
    4. r filename: reads from the specified file append to the current line merge

Parameters that allow matching of one address range:

    1. d: Delete the current row (delete the contents of the mode space)
    2. c \text: Replaces the specified row with the text content, enabling multi-line insertion using \ n
    3. P: Print the current line (that is, print the current mode space)
    4. s/regexp/replacement/: matches the specified content with a regular expression on the action line and replaces it with replacement
    5. {;}: If you have more than one operation at a time, you can include multiple action parameters with {}, and the parameters can be separated, such as {a \text;p}

"Input_file" field: Specifies the file to be processed

raise 2 chestnuts :

The test file reads as follows:

1, sed-n ' 4,7p ' test01

- N: corresponding option domain; ' 4,7p ': Corresponds to the Script field, where 4,7For the address section, PMatching operation parameters section; test01: corresponding input-file domain

Meaning: Print output to line 4th to 7th (p parameter), the-N option indicates silent output, no original text is output, and the-n parameter is the following:

2. Sed-i ' 4,+2s/[0-9]/12345/' test

When you do not add the-i parameter, the modification does not affect the original text content

After adding the-I parameter, the modification will be written to the original content

Meaning: The 4th line of the test file starts, the next 2 lines (that is, 4,5,6 three lines) match to the content to replace, [0-9] match a number, replace the number with 12345, write to the original file.

Summary: Individuals think that the introduction of SED as an overview is a good choice, the above explanation describes the following questions:

1. The syntax of the SED command

2,the meaning of the various parts of the SED command, as well as what are the simple, easy to understand the optional parameters

3 . Simple Use Example

In-depth Tutorials This article does not cover, this article has the error, please ask the reader to correct ~

Overview of Getting Started with SED for linux commands

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.