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

Introduction to the usage of sed command in linux

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

Share

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

This article introduces the relevant knowledge of "introduction to the usage of sed command in linux". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

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

This is the end of the introduction to the usage of the sed command in linux. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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