Linux Command Learning program "SED"

Source: Internet
Author: User

Introduction:

The SED command is a command used in Linux for Text line processing.

For illustrative purposes, I created a dictionary words under/usr/dict and used it as a presentation template

First print the words content in NL:

* Printed article:

Q1: How do I print a row of data?

If you want to print the first row of data, use:

Sed–n 1p Words

If you want to print the last row of data, use:

Sed–n ' $p ' words

Note that if you have a specific line number, you do not need to enclose the quotation marks, if you have special characters such as ' $ ' or some patterns match.

Do you think it's just that you can see the data in the head row and the end row, what's so strange? Imagine if a text, such as a ' huge ' log, simply depends on the 10,000th row of data, how to look at it? If a page to find will be troublesome, and open a large file to find is also unrealistic. It is very convenient to use the SED command. See!

Sed-n 10000p Words

Q2: Print a continuous line?

Print consecutive lines as long as you write this:

Sed–n 2,6p Words

Represents the printing of 2-6 rows of data

Print data from the end of line 5th, so write:

Sed–n ' 5, $p ' words

Q2: How do I print a discontinuous line?

For example, I want to print only the 5th, 8, 10 data:

The "-E" parameter is used here, and each-E can be followed by an expression + execute action, and SED is executed sequentially.

Q3: Print lines that contain the specified characters (pattern matching)?

When we look for data in a large document, we often do not know what to look for in the row, we know that a row of data may contain what string or what kind of law, this time to use the pattern matching, to match the format between two '/', as follows:

/pattern-text/

。 For example, to view lines with ' abc ':

sed–n/abc/p words

To view a line that contains 3 consecutive identical characters:

Sed–n '/\ (\w\) \1\1/p ' words

* NEW article

Q1: What do I do to add a new line after a row?

For example, there are three scripts under Sedtest:

These scripts are written by the author, the author after writing found it necessary to leave their name in the source code. So he did:

Sed–in ' 1a #author is Elvis ' sh01.sh sh02.sh sh03.sh

Plus-I means that the modified content is directly ' writeback ' to the document and not on the screen output

1a means inserting a new row after the first line, because the first line is often ' #! ' Yes. You can also change the 1a to 2i, which means inserting a row before the second line (of course the default is a script file with at least 2 lines)

The author is not satisfied with the name, and wants to add the version and date information one at a time and add a line to the comment ' This was for test '. Sed can also be used to add multiple lines at once, either by adding a newline character directly behind it, or by entering "\+ carriage return" in the shell to separate each line of input:

In addition, SED can read the contents of a text file and append it to the following line:

For example, to append the contents of sh04.sh to the end of 3 script files:

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

Sh04.sh wrote a lot of things, including defining methods, such as the bomb method defined in sh04.sh. This method can be inserted at the end of the 3 script by the above command.

* Delete Article

The SED command makes it easy to delete a specified line in a text file.

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

For example: I want to see a shell script, but do not want to see the comment, and do not print blank lines, you can do so;

Careful you may find that I have less "-n" parameter, in the printing and add the time is usually added "-n" parameter, because "-n" means that only the matching line is output.

Let's rewrite the case above and see what happens if we don't add this parameter to the print.

Hey? Why did you print all of it? And it's printed again!

This is because the original rows are output in non-quiet mode, and the display does not match the results we want. Add "-n" to "quiet"!

Because the deletion of the action, is to display the original row after the deletion of data, so do not need to add "-n", if added will not see any effect.

* Modified article

The SED command can modify the data of a text document.

1, the whole line to replace

Sed n1,n2c textfilepath

For example, I want to take the empty line in the words with three ' # ' instead, you can do this:

2. Partial modification

The regular expression is a powerful tool, and it's a great way to make the text tools like Sed,awk. Sed can modify a text document by matching any of the places you want to modify, such as:

Words the first letter of every line of words is not uppercase, through the SED command can be perfected this point, see!

For example, I want to add "0x" prefixes to every word in words:

* Foreign article

1, Summary:

Through the above introduction, summed up the use of SED rules,

sed [–nrfe] + regular expression or line number + Execute action + file, perform action with character identification such as A,c,i.

At the same time SED is also a pipe command, the so-called pipe command, the simple understanding is not only can produce data, can also receive and process other commands to produce data.

For example, there are a lot of duplicate lines in a document, it is not necessary to print all the duplicate lines when the SED is used, then the duplicate lines can be removed with the Uniq command, then the data will be handed over to SED for processing:

2. Extension:

The meaning and use of the-E,-F,-R parameters

Some people do not understand the 4 parameters of Nrfe in sed, in fact, it is not difficult to understand them, manual + practice is done.

-N above has been explained, here skip.

First use the man sed to see the description of the parameter:

You can see that both the-e and-f functions are the same, adding execution actions to the command line, except that-e is adding the expression directly, and-F is specifying a script file.

I have seen the use of-e in the printed article, yes! An SED command can be continuously connected to several-e exp, which tells us that an sed command actually does a lot of things.

For example, in the modified article, I would like to implement the function of converting uppercase letters and adding prefixes, which can be implemented with the-e parameter:

The role of-F is to specify a file full of execution actions:

For example, I wrote a sed_print file with a command that says I want to print a few lines:

I just specify this document after the SED command to print the 2nd row, line 5th, 8th row of data, is not very convenient

The function of-R is to apply the extended regular expression

If you do not add the basic regular expression that this parameter uses by default, some syntax is not available, such as "+" and "?". Let's look at the following comparison:

For example, words is not delimited by line, but separated by a space, I want to capitalize each of the English words in the first letter:

The above regular expression uses the "+", so must add "-r" parameter, otherwise it will not match, this is also we use the SED command to be very careful of the place.

What exactly does an extended regular expression extend?

In addition to the above-mentioned + and, there are:

1. {M,n} characters match

2. |,or Matching

3, (), grouping and reverse referencing

Linux Command Learning program "SED"

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.