Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of Linux sed command

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "the usage of Linux sed command". In daily operation, I believe many people have doubts about the usage of Linux sed command. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the usage of Linux sed command"! Next, please follow the editor to study!

The format of the sed command line is:

Sed [- nefri] 'command' input text

Common options:

-n ∶ uses quiet (silent) mode. In the normal use of sed, all data from STDIN is usually listed on the screen. But if you add the-n argument, only the row (or action) that has been specially handled by sed will be listed.

-e ∶ edits sed actions directly in instruction line mode

-f ∶ directly writes sed actions in a file, while-f filename can perform sed actions in filename.

The action of-r ∶ sed supports the syntax of extended regular representations. (the preset is the basic formal representation syntax)

-I ∶ directly modifies the contents of the read file instead of outputting it on the screen.

Common commands:

A ∶ is added, a can be followed by strings, and these strings will appear on a new line (the current next line) ~

C ∶ instead, c can be followed by strings, these strings can replace the lines between N1 and N2!

D ∶ is deleted, because it is deleted, so there is usually no knock after d.

I ∶ insert, I can be followed by strings, and these strings will appear on a new line (the current previous line)

P ∶ printing, that is, printing a selected data. Usually p works with the parameter sed-n ~

S ∶ replacement, you can directly carry out the replacement work! Usually the action of this s can be matched with the formal representation! For example, 1Magazine 20s Universe, OldCompact newscop g is it!

For example: (suppose we have a file called ab)

Delete a row

[root@localhost ruby] # sed '1d' ab # Delete the first line

[root@localhost ruby] # sed'$d' ab # Delete the last line

[root@localhost ruby] # sed'1 ab # delete the first line to the second line

[root@localhost ruby] # sed'2 ab # delete the second line to the last line

Show a row

[root@localhost ruby] # sed-n '1p' ab # shows the first line

[root@localhost ruby] # sed-n'$p'ab # displays the last line

[root@localhost ruby] # sed-n'1J 2p'ab # shows the first to second lines

[root@localhost ruby] # sed-n'2 ab # shows the second line to the last line

Use schema for query

[root@localhost ruby] # sed-n'/ ruby/p' ab # query includes all lines where the keyword ruby is located

[root@localhost ruby] # sed-n'/\ $/ p'ab # query includes all lines where the keyword $is located, using backslash\ to mask special meaning

Add one or more lines of string

[root@localhost ruby] # cat ab

Hello!

Ruby is me,welcome to my blog.

End

[root@localhost ruby] # sed'1a drink tea' ab # add the string "drink tea" after the first line

Hello!

Drink tea

Ruby is me,welcome to my blog.

End

[root@localhost ruby] # sed '1jue 3a drink tea' ab # add the string "drink tea" after the first to third lines

Hello!

Drink tea

Ruby is me,welcome to my blog.

Drink tea

End

Drink tea

[root@localhost ruby] # sed'1a drink tea\ nor coffee' ab # add multiple lines after the first line, using the newline character\ n

Hello!

Drink tea

Or coffee

Ruby is me,welcome to my blog.

End

Instead of one or more lines

[root@localhost ruby] # sed'1c Hi' ab # replace the first line with Hi

Hi

Ruby is me,welcome to my blog.

End

[root@localhost ruby] # sed'1 Hi' ab 2c replace lines 1 to 2 with Hi

Hi

End

Replace a part of a line

Format: sed's / string to be replaced / new string / g' (string to be replaced can use regular expression)

[root@localhost ruby] # sed-n'/ ruby/p' ab | sed's bird replacement of ruby for bird

[root@localhost ruby] # sed-n'/ ruby/p' ab | sed's _

insert

[root@localhost ruby] # sed-I'$a bye' ab # enter "bye" directly on the last line in the file ab

[root@localhost ruby] # cat ab

Hello!

Ruby is me,welcome to my blog.

End

Bye

Delete matching lines

Sed-I'/ match string / d 'filename (Note: if the match string is a variable, you need "" instead of "". I remember it seems to be)

Replace a string in a matching line

Sed-I'/ match string / s / replace source string / replace target string / g 'filename

At this point, the study of "the usage of the Linux sed command" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report