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 sed Command under Linux

2025-01-19 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 "the usage of sed command under 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!

A brief introduction to Sed

The Sed:Stream Editor streaming editor, also known as the line editor, edits only one line at a time. Sed work is done in the "schema space" and does not manipulate the source files. There is no harm to the source file.

II. Sed format

The common format of the Sed command is as follows:

(1) Sed [options] 'script' input_file...

(2) Sed [options]-f script_file input_file...

(3) Sed [options] 'ADDR1,ADDR2command' input_file...

For example: # sed'1 fstab 2d' can delete the first two lines of fstab and display them, but it does not change the source file.

# Sed'1 fstab 2 contains' delete all rows except the first and second rows.

(4) Sed [options]'/ PATTERN/command' input_file

&: sqqure l.. eUnierCompact: refers to all the characters that were previously matched.

For example: sed'/ [[: upper:]] / d 'binary.sh represents a line that deletes all uppercase letters.

(5) Sed'/ PATTERN1/,/PATTERN2/command' input_file.

Note: this means that the command command is executed from the first line matched by PATTERN1 to the middle line matched by PATTERN2 for the first time.

Third, the common options of Sed:

-n: only rows matched by sed are displayed. The rest of the lines are not displayed. There are examples below.

-I: you can manipulate the original file directly. By default, sed does not change the original file, but the-I option can modify the original file, which should be used with caution.

-r: standard regular expressions can be used. By default, sed only supports basic regular expressions, but extended regular expressions are supported with the-r option

-e: multiple scripts are shared. You can execute multiple commands at the same time. For example:

The code is as follows:

[root@localhost ~] # sed-e's @\ @ H\ 1pragg'-e's @\ (l.e\)\ .@\ 1r.pragg'test

He love His lover.

She like Her liker.

Dove love Her liker.

It can also be used this way, separated by a semicolon:

The code is as follows:

[root@localhost ~] # sed's @\ @ H\ 1\ (l.e\)\ .@\ 1r.pragg' test

Fourth, use examples

The code is as follows:

# sed-e Command Line processing-a New

> sed-e'$a widuu' test.txt # add widuu at the end of test.txt

The code is as follows:

# replace-e with-n

> sed-n'$a widuu' test.txt # shows only the row being processed

The code is as follows:

#-c how many lines to replace the string for you, here my test.txt content is shown in the figure

> sed-e'2 xiaowei' test.txt 5c xiaowei' test.txt # replace lines 2-5 with the string xiaowei

The code is as follows:

# multiple command execution-e

> sed-e'2 xiaowei' 5c xiaowei'-e's Universe cccc Universe Widuu Universe 'test.txt

The code is as follows:

#-I do not output-s replacement

> sed-I's Uniqer 1111Universe Widuu Universe g 'test.txt

The code is as follows:

# I insert custom string insert xiaowei in the second line of test.txt

> sed-e'2i xiaowei' test.txt

The code is as follows:

# d Delete any string entered

> sed-e '2d' test.txt # Delete the second line

This is the end of the introduction of "the usage of the sed command under Linux". Thank you for 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