Shell script learning-5 (SED command learning)

Source: Internet
Author: User

1 Have you ever wondered why SED is called sed?

Sed is short for stream editor. Sed is a stream editor. To put it bluntly, we enter a string of information into SED, which is processed and output. That's simple.

Similar to cut, sed is a row-oriented tool that uses "rows" as the processing unit and outputs the processed results to the standard output. Therefore, the SED command is actually safe and does not modify the file where the line is located.

Sed Command Format: sed command file

Among them, the command part is the processing of each row of data (this part is very important, you will know), file is the file to be processed, if the file parameter is ignored, sed uses the standard input as the processing object.

2. How does sed work?

Sed processes a row of content at a time. During processing, the currently processed rows are stored in the temporary buffer, called the pattern space. Then, the SED command is used to process the content in the buffer, send the buffer content to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage output.

3. Give an example of the simplest sed command! Let me have a perceptual knowledge first.

[Rocrocket @ rocrocket programming] $ cat roc.txt
Test 1
Test2
Testtest
Xtestx
Bbtest
[Rocrocket @ rocrocket programming] $ sed '/2/d' roc.txt
Test 1
Testtest
Xtestx
Bbtest
This example uses sedto Delete rows containing the character "2" in the roc.txt file. As you can see, the example is very simple. The command part of this command is/2/d, and it is enclosed in single quotes. You must also learn to do this. If SED is used, do not forget to enclose the command part in single quotes.

In/2/d, "D" indicates deletion. This means that if a row contains 2 characters, it is deleted. (If you do not forget, all the deletions of the SED are performed in the model space, and the original file roc.txt is undamaged)

4. How to Use sed to achieve the effect similar to cut-D:-F 1/etc/passwd?(For cut usage, see cut command-Linux Command: 19th of the five-minute Series)

[Rocrocket @ rocrocket programming] $ head-N 5/etc/passwd
Root: X: 0: 0: Root:/root:/bin/bash
Bin: X: 1: 1: Bin:/bin:/sbin/nologin
Daemon: X: 2: 2: daemon:/sbin/nologin
ADM: X: 3: 4: ADM:/var/adm:/sbin/nologin
LP: X: 4: 7: LP:/var/spool/lpd:/sbin/nologin

[Rocrocket @ rocrocket programming] $ head-N 5/etc/passwd | SED's/:. * $ //'
Root
Bin
Daemon
Adm
Lp

 

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.