Detailed explanation of sed commands in CentOS

Source: Internet
Author: User

Sed

Introduction sed is a non-interactive editor. It does not modify the file unless you use shell redirection to save the results. By default, all output rows are printed to the screen. Sed editor processes files or inputs row by row and sends the results to the screen. The specific process is as follows: sed first saves the row being processed in a temporary cache area, also known as the mode space, and then processes the row in the temporary buffer area and then sends the row to the screen. Sed deletes a row from the temporary buffer after processing and then reads the next row for processing and display. After processing the last line of the input file, sed stops running. Sed edits this copy in a temporary buffer for each row, so the original file is not modified.

Syntax format: sed [parameter] [address function] [file]

Addressing [addressing is used to determine which rows to edit.] The address format can be numbers, regular expressions, or a combination of the two. If the address sed is not specified, all rows of the input file will be processed. If the addresses are separated by commas, the address to be processed is the range between the two rows, including the two rows. The range can be represented by numbers, regular expressions, or combinations of the two.

Option

-E

Multiple edits are used when multiple sed commands are applied to the input line.

-N

Cancel the default output-because the default sed outputs the file by default. Sometimes the-n option is required if repeated output is not avoided.

-F

Specify the sed script file name

-R

Use extended regular expressions in the script

Use the extended regular expression in the script

Command

Command

Function

A \

Add one or more rows after the current row. "\" Is required at the end of each line except the last row for multiple rows. Note that the \ Two \ more ones must be added to the end of a, and the \ post will be commented out. To display \, three \\\

C \

Replace the text in the current line with the new text after this symbol. "\" Is required at the end of each line except the last line when multiple rows exist.

I \

Insert text before the current row. "\" Is required at the end of each line except the last line when multiple rows exist.

D

Delete row

H

Copy the content in the mode space to the temporary buffer.

H

Append the content in the mode space to the temporary buffer.

G

Copy the content in the temporary buffer to the mode space to overwrite the original content.

G

Append the content of the temporary buffer to the mode space to the end of the original content.

L

List non-printable characters

P

Print rows

N

Read the next input line and process it from the next command instead of the first command.

Q

End or exit sed

R

Read the input line from the file, such as sed '/test/r file'. Log reads the file from the file after matching the test line.

!

Apply commands to all rows other than the selected row

S

Replace another string

G

Global replacement within a row

I

Write the selected row to a file

X

Swap content of the temporary buffer and mode space

Y

Replace the character with another character. You cannot use the y command for the regular expression.

W

$ Sed-n'/test/w file' example ----- all rows containing test in example are written to file.

Regular Expression metacharacters: Like grep, sed also supports Special metacharacters for Pattern Search and replacement. The difference is that the regular expression used by sed is the mode between the slash line.

The sed command contains two parts: 1 operation 2 Site Selection

Use instance

PCommand

[Root @ LiWenTong ~] # Ifconfig eth0 | sed-n'/inet addr:/p'-'. If n is not added, all input is printed again by default.

Inet addr: 192.168.1.104 Bcast: 192.168.1.255 Mask: 255.255.255.0

[Root @ linux-lwt tmp] # sed-n '1p' 1.txt ---> specify the input line

Root: $1 $ OSqWjVsf $ Lebv2EkKzV0AA2ps. hTTk1: 16058: 0: 99999: 7 :::


D command

Command d is used to delete the input line. Sed first copies the input line from the file to the mode space, then runs the sed command on the line, and finally displays the content in the mode space on the screen. If the command d is issued, the input row in the current mode space will be deleted and not displayed.

Retrieve only the IP address of eth0

Instance

1. Delete rows. Replace rows with printed rows.

[Root @ linux-lwt tmp] # sed-n 'P' 1.txt

Hello atong

Bin: *: 16058: 0: 99999: 7 :::

[Root @ linux-lwt tmp] # sed-I '1d '1.txt

[Root @ linux-lwt tmp] # sed-n 'P' 1.txt

Bin: *: 16058: 0: 99999: 7 :::

[Root @ linux-lwt tmp] # sed '1, 2c tidaihang '1.txt

Tidaihang

Daemon: *: 16058: 0: 99999: 7 :::

Adm: *: 16058: 0: 99999: 7 :::

2. Add content after a line ---> to add content to a configuration file in the script.

[Root @ linux-lwt tmp] # cat 1.txt

Bin: *: 16058: 0: 99999: 7 :::

Daemon: *: 16058: 0: 99999: 7 :::

[Root @ linux-lwt tmp] # sed-in '1a hello this is atong '1.txt

[Root @ linux-lwt tmp] # cat 1.txt

Bin: *: 16058: 0: 99999: 7 :::

Hello this is atong

If you want to add multiple rows, add \ n and then link the content of the next row.

3. query and output a string.

[Root @ linux-lwt tmp] # sed-n'/hell/P' 1.txt

Hello this is atong

Daemon: *: 16058: 0: 99999: 7 :::

You can even search in the specified row.

[Root @ linux-lwt tmp] # head-n 5 1.txt

Bin: *: 16058: 0: 99999: 7 :::

Hello this is atong

Daemon: *: 16058: 0: 99999: 7 :::

Adm: *: 16058: 0: 99999: 7 :::

Hello atong

[Root @ linux-lwt tmp] # sed-n'1, 4p' 1.txt | sed-n'/hello/P'

Hello this is atong


4. The instance obtains the IP address.

[Root @ LiWenTong ~] # Ifconfig eth0 | grep "inet"

Inet addr: 192.168.1.104 Bcast: 192.168.1.255 Mask: 255.255.255.0

[Root @ LiWenTong ~] # Ifconfig eth0 | grep "inet" | sed '/^. * addr:/d'-the first part of "addr:/d'" is about to delete the previous part, but the result is not displayed. The whole row is deleted instantly. So it is replaced.

[Root @ LiWenTong ~] # Ifconfig eth0 | grep "inet" | sed's/^. * addr: // G'

192.168.1.104 Bcast: 192.168.1.255 Mask: 255.255.255.0

[Root @ LiWenTong ~] # Ifconfig eth0 | grep "inet" | sed's/^. * addr: // G' | sed's/Bcast. * $ // G' --- "filters sed again.

192.168.1.104


5-e perform multiple operations

[Root @ LiWenTong ~] # Man | sed-e '10, $ d'-E'/^ $/d' ---> retain the first 10 lines of man and delete the blank lines. When using the d command, you do not know when to add/to separate it.

Man (1) man (1)

NAME

Man-format and display the on-line manual pages

SYNOPSIS

Man [-acdfFhkKtwW] [-- path] [-m system] [-p string] [-C config_file]


-I will directly modify the content in the file

[Root @ LiWenTong ~] # Sed-I '$ a this is end' 1. log | sed-n' $ P' 1.log

This isend

6) append the content of command

[Root @ LiWenTong ~] # Sed-n'/belive/a you can do it '1.log

You can do it

[Root @ LiWenTong ~] # Grep 'can '1. log ----> you can see that the last row is inserted

I belive I can fly

You can do it

7 markString

$ Sed-n's/\ (love \) able/\ 1rs/P' example love is marked as 1. All loveable will be replaced with lovers and the replaced lines will be printed out. And whatever characters followed by the s command is considered as a new separator. Here, \ 1 is the replacement of \ (love. If you replace p with g, it is a global search and I is replaced. The yellow three/should have the structure s // g.

This indicates that \ n can be used in the sed replacement command to replace the fields in the previous regular expression search.

Such as sed-n's/\ (haha \ | xixi \) hao/\ 1 buhao/gp '1.txt indicates that the haha or xix marked with \ 1 will be marked as \ 1, and then one of the actually replaced hao --> buhao.

Instance

[Root @ linux-lwt tmp] # sed-n's/\ (haha \ | xixi \) hao/\ 1 buhao/gp '1.txt

Hahabuhao

Xixibuhao

[Root @ linux-lwt tmp] # cat 1.txt

Nonohao

Hahahao


8 specify the sed script file[This part has not been verified]

The Sed script file is a column of sed commands written in the file. The script requires that the end of the command cannot contain any extra space or text. If multiple commands in a row are separated by semicolons. When executing the script

-------------------------- Sed script content --------------------

# Handle datafile
3i \
~~~~~~~~~~~~~~~~~~~~~
3, $ s/\ (hrwang \) is \ (mjfan \) /\ 2 is \ 1/---> here, use \ 1 to improve hrwang and use \ 2 to replace mjfan and then use s/to reconcile \ 1 \ 2.
$ \
We will love eachother forever

-----------------------------------------------------------


9. Use a regular expression in the command

[Root @ LiWenTong ~] # Cat aa.txt

Ad I am atong hah aha

Hah I am liwentong haha

Ad I amatonghah aha

Hah I amliwentonghaha

[Root @ LiWenTong ~] # Sed-n-r's/. * (atong | liwentong). */\ 1/gp 'aa.txt

Atong

Liwentong

Atong

Liwentong

Summary searches for the rows matching the specified text from a text and outputs only the matched text.

Sed search display

Sed specifies a line of output in the result or output similar to the grep command.

Sed-n'4p' outputs 4th rows.


----------------------------- Subsequent self summary ------------------------------

Sed command is a very powerful command lineThe content of the textThe actions are as follows:Display, replace, query, delete, and insert operations. And cannot perform operations on the input/output content or on the content of a file. In this way, we can easily modify a write configuration file using the command line method in the script. Therefore, this command is very useful if you are familiar with it.

In addition, I personally think that if you want to combine it well with regular expressions, it will be more powerful.

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.