Sed tutorial (ii): Basic syntax, loops, branching

Source: Internet
Author: User
Tags tag name

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 invoking SED in two ways: sed command line

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

sed [-n] [-e] ' command (s) ' files 
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 C  Oelho, 197 
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, 1216
2) The Towers, J. R. R. Tolkien, 352
3) The Alchemist, Paulo C  Oelho, 197
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

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.

[jerry]$ sed-e ' 1d '-e ' 2d '-e ' 5d ' books.txt 

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

3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones, Geo Rge R. R. Martin, 864 
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 the following figure:

1d 
2d 
5d 

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

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

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

3) The Alchemist, Paulo Coelho, 197 
4) The Fellowship of the Ring, J. R. R. Tolkien, 432 
6) A Game of Thrones,geor GE R. R. Martin, 864 
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:

[jerry]$ sed-n ' Quote.txt 
- 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




Like other programming languages, SED also provides a loop and branching tool to program the execution flow. This tutorial explores how to use the loops and branches of SED.

The SED cycle works like a goto statement in a modern programming language. Sed can jump to the line of the tag tag and continue with the remaining commands that provide the label below.

The following is a syntax for defining a label in SED. Here, the name after the colon (:) implies the label name.

: Label 
: Start 
: End 
: Up

To jump to a specific tag, we can use the B command followed by the tag name. If the name of the label is omitted, SED jumps to the end of the sed file.

Consider that we have a pending text file Books.txt, which has the following content:

A Storm of Swords
George R. R. Martin the Towers J. R. R. Tolkien the
alchemist
Paulo Coelho
T  He Fellowship of the Ring
J. R. R. Tolkien the
pilgrimage
Paulo Coelho
A Game of Thrones
George R. R. Martin

The following example connects the title of the book and separates the author's name with a comma in a row. It then searches for the pattern "Paulo". If it is able to match, it prints a hyphen (-) in front of the line, otherwise jumps to the print line to print the label.

[jerry]$ sed-n ' 
h;n; H;x 
s/\n/,/ 
/paulo/!b Print 
s/^/-/ 
:P rint 
p ' books.txt

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

A Storm of Swords, George R. R. Martin, 
the Towers, J. R. R. Tolkien 
-The Alchemist, Paulo Coelho the 
Fell Owship of the Ring, J. R. R. Tolkien 
-The pilgrimage, Paulo Coelho
A Game of Thrones, George R. R. Martin 

At first glance, the above script may look mysterious. Let's see what the situation is.

The initial sed read-in mode buffers the first line that is the title and keeps the buffer empty. After the Execute-H command mode buffer is copied to the reserved buffer. Now, these two buffers contain the book, which is the title. A Storm of Swords. The next n command prints the current mode buffer (not printed in this example, because the-n option) clears the current drawing buffer from reading the next line of input. The mode buffer now contains George R. R. Martin.

The third command jumps to the label print that is executed by the fourth instruction only if the pattern does not match otherwise.

:P Rint is just a label name and P is the Print command.

To improve readability, each sed command is placed on a separate line. However, one can choose to have all the commands in a single line, as follows:

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

A Storm of Swords, George R. R. Martin, 
the Towers, J. R. R. Tolkien 
-The Alchemist, Paulo Coelho the 
Fell Owship of the Ring, J. R. R. Tolkien 
-The pilgrimage, Paulo Coelho 
A Game of Thrones, George R. R. Martin


You can create a branch with the T command. The T command jumps to the label only if the previous replacement command was successful. Let's take the same example as in the previous chapters, but not print a hyphen (-) and now we print four hyphens. The following example demonstrates the use of the T command.

[jerry]$ sed-n ' 
h;n; H;x 
s/\n/,/ 
: Loop 
/paulo/s/^/-/ 
/----/!t loop 

When the above code is executed, the following result is produced.

A Storm of Swords, George R. R. Martin the 
Towers, J. R. R. Tolkien 
----The Alchemist, Paulo Coelho the 
Fe Llowship of the Ring, J. R. R. Tolkien 
----The pilgrimage, Paulo Coelho 
A Game of Thrones, George R. R. Martin

We have discussed the first command in the previous section. The third command defines a label loop. The IV command is preceded by the hyphen (-), if the line contains the string "Paulo" and the T command repeats the process until there are four hyphens at the beginning of the line.

To improve readability, each sed command is written in a separate line. Otherwise, we can write one line of an sed as follows:

When the above code is executed, the following result is produced.

A Storm of Swords, George R. R. Martin the 
Towers, J. R. R. Tolkien 
----The Alchemist, Paulo Coelho the 
Fe Llowship of the Ring, J. R. R. Tolkien 
----The pilgrimage, Paulo Coelho 
A Game of Thrones, George R. R. Martin


From:http://www.yiibai.com/sed/sed_useful_recipes.html

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.