"One of the flow art of sed"-linux command five-minute series 21

Source: Internet
Author: User

This original article belongs to "Linux greenhouse" blog, the blog address is http://roclinux.cn. The author of the article is rocrocket.

In order to prevent the vicious reproduction of some websites, special in each article before adding this information, but also hope that readers understand.

===

[Start of body]

1 Have you ever wondered why SED is called sed?

SED is the abbreviation for Stream editor, and Sed is a stream editor, so to put it bluntly, we enter a string of information into the SED, which we process and then output. It's so simple.

Sed and cut are similar, is a line-oriented tool, which takes "line" as the processing unit, the processed results will be output to the standard output. So, in fact, the SED command is very safe, it does not make any changes to the file itself.

The command format for sed is: sed command file

The command part is the processing of each row of data (which is important, and you will know), file is the document to be processed, and SED will use the standard input as the processing object if it is ignored.

2 How does sed work?

SED processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output.

3 Give an example of the most simple sed command! Let me first have a perceptual knowledge, hehe.

[email protected] programming]$ cat Roc.txt
Test 1
Test2
Testtest
Xtestx
Bbtest
[[Email protected] programming]$ sed '/2/d ' roc.txt
Test 1
Testtest
Xtestx
Bbtest
This example uses SED to delete the line containing the character "2" in the Roc.txt file. As you can see, the example is simple, the command part is/2/D, and it is enclosed in single quotes. You must also learn to do this, as long as you use SED, and don't forget to enclose the command part in single quotes.

D in/2/d means delete, meaning that if a line contains a character 2, it is deleted. (You must not forget that SED's so-called deletion is performed in the pattern space, and the original file roc.txt intact)

4 want to achieve similar to cut-d:-F 1/etc/passwd effect, with sed how to do? (cut usage See "Cut command"-linux command five-minute series 19)

[Email protected] 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:/sbin/nologin
Adm:x:3:4:adm:/var/adm:/sbin/nologin
Lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[Email protected] programming]$ head-n 5/etc/passwd|sed ' s/:.*$//'
Root
Bin
Daemon
cd/
Lp

See, we use the command is ' s/:.*$//', where the regular expression s///is the replacement of the classic format, if you do not understand, you can look at my previous blog feature article, is about the art of substitution: "VI in the art of substitution"- The Linux command five-minute series Xi.

continued:"stream Art of the SED II"-linux command five-minute series 22

"One of the flow art of sed"-linux command five-minute series 21

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.