SED tutorial (iii) SED syntax

Source: Internet
Author: User

SED is simple to use, and we can provide SED commands directly on the command line or in the form of text files with SED commands. This tutorial explains the example of calling SED, which is available in both ways:

Sed command Line

Here's what we can refer to as the order quotes in the command line SED command format as follows:

Example

Consider that we have a text file Books.txt to be processed, it has the following content:

1) A Storm of Swords, George R. R. Martin, 1216 2) the Towers, J. R. R. Tolkien, 352 3) The Alchemist, Paulo Coelho, 1 4) The Fellowship of the Ring, J. R. R. Tolkien, 432 5) The Pilgrimage, Paulo Coelho, 288 6) A Game of Thrones, George R. R. Martin, 864

First, let's use the full display of the SED file without any commands as follows:

[jerry]$ sed ' books.txt

Executing the above code, you will get the following result:

1) A Storm of Swords, George R. R. Martin, 12162) The Towers, J. R. R. Tolkien, 3523) The Alchemist, Paulo Coelho, 197  4) The Fellowship of the Ring, J. R. R. Tolkien, 4325) The pilgrimage, Paulo Coelho, 2886) A Game of Thrones, George R. R. R. Martin, 864

Now, we show from the above file that we will see the delete command for sed delete some rows. Let's delete the first, second, and fifth lines.

Here, to delete a given three rows, we have specified three separate commands with the-e option.

Executing the above code, you will get the following result:

Sed script file

Here's the second form, we can provide an SED script file sed command:

sed [-n]-F scriptfile files

First, create a text Commands.txt file that contains a single line, one line at a time for each sed command, as shown in:

We can now instruct SED to read instructions and perform operations from a text file. Here, we achieve the same result in the above example.

[jerry]$ sed-f Commands.txt Books.txt

Executing the above code, you will get the following result:

SED standard options

SED support provides the following standard selections from the command line.

-N option

This is the default printing option for pattern buffers. The GNU SED interpreter provides the--quiet,--silent option as an alternative to the-n option.

For example, the following SED command does not display any output:

-E option

The edit option for the-e option. By using this option, you can specify multiple commands. For example, the following SED command prints two times per line:

[jerry]$ sed-e '-e ' P ' quote.txt

Executing the above code, you will get the following result:

There is only one thing that makes a dream impossible to achieve:the fear of failure. There is only one thing that makes a dream impossible to achieve:the fear of failure.  -Paulo Coelho, the Alchemist  -Paulo Coelho, the Alchemist
-F option

The-f option is used to provide a file containing the SED command. For example, we can specify a print command from a file as follows:

[jerry]$ echo "P" > commands.txt [jerry]$ sed-n-F commands quote.txt

Executing the above code, you will get the following result:

There is only one thing that makes a dream impossible to achieve:the fear of failure.  -Paulo Coelho, the Alchemist

SED tutorial (iii) SED syntax

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.