Linux Command learning plan [sed], linux Command sed

Source: Internet
Author: User

Linux Command learning plan [sed], linux Command sed

Introduction:

The Sed command is used for text line processing in linux.

For ease of instruction, I created the dictionary words under/usr/dict and used it as a demo template.

Print words with nl:

* Print:

Q1: how to print a row of data?

To print the first line of data, use:

Sed-n 1 p words

To print the last row of data, use:

Sed-n' $ P' words

NOTE: If it is a specific line number, no quotation marks are required. If it contains special characters such as '$' or some pattern matching, quotation marks are required.

You will think, just to see the data of the first row and the end row, what's strange? Imagine if a text, such as a huge log, only needs to read 10,000th rows of data. How can this problem be solved? It will be difficult to find one page, and it is unrealistic to open a large file. The sed command is very convenient. Look!

Sed-n 10000 p words

 

Q2: Print consecutive rows?

To print a row, write as long:

Sed-n 2,6 p words

Print 2-6 rows of data

Print the data from the end of the 5th Street. Write it as follows:

Sed-n'5, $ P' words

 

Q2: how to print discontinuous rows?

For example, I want to print only 5th, 8, and 10 Data:

Here the "-e" parameter is used, and each-e can be followed by an expression + to execute the action. sed is executed in order.

Q3: print the row containing the specified character (pattern matching?

When we look for data in a large document, we often don't know where the data to be searched is. What we know is what strings or rules a row of data contains, in this case, pattern matching is used to place the format to be matched between two '/', as shown below:

/Pattern-text/

. For example, to view rows containing 'abc:

Sed-n/abc/p words

 

To view rows that contain three consecutive identical characters:

Sed-n'/\ (\ w \) \ 1 \ 1/P' words

 

 

* Add an article

Q1: How do I Add a new row after a row?

For example, sedtest has three scripts:

These scripts are written by an author. After writing the script, the author finds it necessary to keep his name in the source code. So he did this:

Sed-in '1a # author is elvis 'sh01.sh sh02.sh sh03.sh

-I indicates that the modified content is directly written back to the file, but not output on the screen.

1a indicates inserting a new line after the first line, because the first line is usually '#! 'Line. You can also change 1a to 2i, indicating to insert a line before the second line (of course, a script file contains at least 2 lines by default)

The author is not satisfied with the big name. He wants to add the version and date information at one time and add a line of comment 'this is for test '. Sed can also add multiple rows at a time. You can add the linefeed \ n directly behind it, or you can separate the input of each row by entering \ + carriage return in shell:

In addition, sed can read the content of a text file and append it to the end of the specified row:

For example, you need to append the sh04.sh content to the end of the three script files:

Sed-in '$ r sh04.sh' sh01.sh sh02.sh sh03.sh

Sh04.sh writes a lot of content, including defining some methods. For example, sh04.sh defines the bomb method. The above command inserts the method into the end of the three scripts.

* Delete an article

The sed command can be used to easily Delete specified lines in a text file.

Usage: sed n1, n2d textfilepath; or sed '/pattern-text/d' textfilepath

For example, if you want to view a shell script, but do not want to view comments, and do not print blank lines, you can do this;

You may find that the "-n" parameter is missing, and the "-n" parameter is usually added during printing and addition, because "-n" indicates that only matching rows are output.

Let's rewrite the above case to see how printing will happen if this parameter is not added.

Why? Why did I print all the data? It is also printed again!

This is because the original rows will be output in non-quiet mode, and the results do not match what we want. Adding "-n" is much more "quiet!

Because the delete action shows the data of the original row after deletion, you do not need to add "-n". If yes, no effect is displayed.

* Modify

Sed command can modify the data of a text document.

1. Replace the whole line

Sed n1, n2c textfilepath

For example, if you want to replace the empty lines in words with three '#', you can do this:

2. Local Modification

Regular Expressions are a powerful text tool like sed and awk. Sed can match to modify any part of a text document you want to modify, for example:

The first letter of each line of words in words is not in uppercase. You can use the sed command to complete this!

For example, I want to add the "0x" prefix to each full number word in words:

* Extra article

1. Summary:

Through the above introduction, we will summarize the usage rules of sed,

Sed [-nrfe] + regular expression or line number + execution action + file. The execution action is identified by characters such as a, c, and I.

Sed is also a pipeline command. The so-called pipeline command can not only generate data, but also receive and process data generated by other commands.

For example, a document contains many duplicate rows. When using sed to locate rows, you do not need to print all duplicate rows. In this case, you can use the uniq command to remove duplicate rows, then, the duplicated data is handed over to sed for processing:

2. Expansion:

-E,-f, and-r Parameters

Some people do not understand the four parameters nrfe in sed. In fact, it is not difficult to understand them. This can be done in manual + practice.

-N: as described above, it is skipped here.

Use man sed to view the parameter description:

We can see that both-e and-f serve the same purpose. They all add execution actions to the command line, but-e directly adds an expression to the end, and-f specifies a script file.

You can see the usage of-e in the print article, right! A sed command can be connected to several-e exp consecutively. It tells us that a sed Command actually does a lot of things.

For example, if you want to convert uppercase letters and add prefixes at the same time, you can use the-e parameter:

-F is used to specify a file with full execution actions:

For example, I have written a sed_print file, which contains the commands that I want to print a few lines:

I only need to specify this document after the sed command to print 2nd lines, 5th lines, and 8th lines of data. Is it very convenient?

-R is used to apply extended regular expressions.

If this parameter is not added, the basic regular expression is used by default. In this way, some syntaxes cannot be used, such as "+" and "?". Let's take a look at the comparison below:

For example, words are not separated by lines, but by spaces. I Want To uppercase the first letter of each English word:

The above regular expression uses the "+", so it must be added with the "-r" parameter. Otherwise, it cannot be matched. This is also the place where we should be very careful when using the sed command.

 

What are the extensions of the extended regular expression?

Except for the sum of the above + and? There are also:

1. Matching of {m, n} characters

2. |, OR matches

3. (), group, and reverse reference

 

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.