Linux Text View command sed

Source: Internet
Author: User

Stream editor to do line processing

The default is not to directly edit the original file, in memory processing (but the entire processing), temporary storage space is called the pattern space (mode). If the keyword can be matched and then edited (after editing is complete, you can put the new content in hold space, to keep the room), if the keyword is not matched by default output to standard output.

The-n option does not output content from the mode space to the screen, which, by default, is output to the screen regardless of whether the content of the pattern space matches the keyword.

-e scripts can only process one script at a time, and the-e script may be followed by multiple scripts, one-E for multi-point editing.

-f/path/to/sed_script_file one edit command per line

-R supports extended regular expressions

-I directly edit the original file

Address delimitation

Specify editing scope

Empty address means processing the full text

Single address # Specify line

/pattern/each line to which the keyword is matched

Address range

#,# Specifying row Ranges

#,+# Specify the start line and down # line

#,/pattern/The specified line begins, to the first match of the key row

/pattern1/,/pattern2/starting with the first match keyword 1, to the first match of the keyword 2 line

#表示步进, odd lines starting from the first line

2~2 even rows starting from the second line


Edit commands are in operation mode space

D Delete the specified range contents

The original file has not changed

Delete (do not display) the line containing the UUID string

Note: The position of this two times D is once in the ' ' inside, once in//outside


Can be on the outside

Now look at the effect of-n

Plus-----nothing's going to be seen.

Show Odd lines

Deleting even numbers is odd

Show even rows

Delete Odd is even

P: Display the contents of the mode space

Why did 1 and 2 appear two times? Because the SED mode will output the contents of the pattern space, P's effect is to output 1 and 2, so 1 and 2 output two times, in order not to let the mode space content output, only see the effect of P, you can add a-n

Show Odd lines


Show 1 to 3 rows

Add content at a specified location

# \text

Add the specified content after line 3rd

Added effect, and-n effect if there are spaces in the appended content


Add multiple lines of content on the third line

Note: You need to write out what you want to append in single quotes


Append the contents to the third line, and the previous third line becomes the fourth line.

Replaces the specified row with the specified content

C \text

The number 3 is gone, replaced by this is a test string

W save content in the specified range as a different document

Outputs a line that starts with a non-# number

As can be seen here, SED is supported by regular expressions, the difficulty of this example is how to identify the non-# number


R appends the contents of other files to the specified line

Append the content after line fourth.


Print line numbers for a specified line

Print the line number first, and the contents of the output line

! Conditional inversion

Outputs a line that starts with a non-# number

The first line is a non-# number, the second is the line that starts with the #, and the second command I use! The condition is reversed, and note here! Need to be escaped, or resolve to the most recent command that starts with P and executes


S find replacement

This is similar to the Vim Editor's find and replace operation, the delimiter can execute the specified @ # can be, G for global substitution (the default option) W Save the replaced content as, p display replace the successful row

Remove all whitespace characters from the beginning of all lines in a file that begin with a blank character

This is the original.

This is after the changed, the default is the global and the

This will show the modified line.


Change 1 in the document to a

Change 1 of the specified line to a


Sed can perform multiple edits at once

Change the UUID in/etc/fstab to UUID, and then delete the row where the UUID is located

Enter an absolute path to the SED command, take out its directory, similar to the dirname command

Found that no t was gone

I'm going to add a trailing locator.

I found the I character is gone, so it's easy to take out the dirname now.

Enter an absolute path of SED, take out its file name, similar to BaseName's command

The place to replace is a range that needs to be \1 in a way


Advanced editing commands

H Overwrite the contents of the pattern space into the hold space

H Appends the contents of the pattern space to the hold space

G will keep the contents of the space covered in the pattern space

G Appends the contents of the hold space to the pattern space

X swaps the content in the pattern space with the content in the hold space

N reads the next line of matching rows into the pattern space.

N reads the next line of matching rows to the pattern space appended

D Delete rows in the pattern space

D Delete all rows in multi-line mode space

P Display the contents of the mode space

Case One Show even rows

Because the operation is not specified, the program first reads the first line, and the effect of n is to let the command read the line and read the contents into the pattern space, and the effect of P is to output the content in the matching pattern space.


Case two displaying file contents in reverse order

Sed ' 1! G;h;$!d ' file_name

From this example, the first: the content read from the SED is first put into the pattern space, followed by a match and then placed in the hold space second: SED reads the content in the behavior unit. The third $ symbol means that the last line of the content is read. The process of this command is similar to the game of Han Nota.


Case three shows the last line

Sed ' $!d ' filename


Case four shows the last two lines

Sed ' $! n;$! D ' shows the last two lines

When the system reads to the second-to-last line, because the second-to-last line is not the penultimate line then N, reads the penultimate line and appends. Now there are 2 lines in the pattern space, and the program reads the penultimate line and does not delete it (! D), final output to screen


Case Five

Show Odd lines

Sed ' n,d ' filename



Linux Text View command sed

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.