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

Sed command

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

Share

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

The sed command is the stream editor.

The workflow is: each time the corresponding line = "pattern space (in memory) =" pattern matching in processing = "output the contents of the pattern space to the screen.

Note: the sed command does not process the original data by default, but a copy of the original data.

Usage

Format: sed 'AddressCommand' file

Command (command processing) for lines that conform to Address (address filtering) in the specified file

Usage supported by Address:

1, start line, end line. Example '1Personnal 100' (lines 1 to 100)

2Perfect / regular matching / support regular pattern matching. In'/ ^ root/' (the line beginning with root)

3Magazine / regular matching 1Universe match / regular matching 2 / represents the beginning of the line matched by module 1 to the line matched by pattern 2

4, the line number matches exactly to a line, and $represents the last line. Example'1' (first line)

5, start line, + n start from the start line to the next N line

Command usage:

D: delete rows that meet the criteria. Example sed '1d` / etc/passwd (delete the first to second lines in / etc/passwd) sed' / ^ root/d' / etc/passwd (delete the line beginning with root in / etc/passwd) sed'/ ^\ / d' / etc/passwd (delete the line at the beginning of / in etc/passwd) p: displays the line that meets the criteria. Example sed'/ ^\ / / p'/ etc/passwd (shows the lines in / at the beginning of the etc/passwd)

A (apend) / string: appends the string string new line after the specified line,\ n indicates a newline

I (insert) / string: inserts the string string new line before the specified line,\ n indicates a newline

R file: adds the contents of the specified file to the eligible line

W file: save (will save) the contents within the specified scope to the specified file

S find and replace: replace the matching content

Usage sed's / find content (regular) / replace content (string, regular is not supported) / modifier 'file. In each line, only the content that is matched for the first time in that line is replaced by default.

Modifier: G global replacement. I ignore case.

Note: in the s command, as long as the three delimiters are the same, most symbols can be used as delimiters, so that you can replace the delimiters in case of matching conflicts to avoid using transfer characters.

Example: sed's #\ (l.. e\) #\ 1rroomg' file name. Or the filename of sed's rooml..

$: everything that matches for the reference pattern.

The difference between $and backward references: referencing only part of the matching content is that only backward references can be used.

Example: sed's Secretl\ (.e\) # L\ 1Secretg' file name

Sed command options:

-h: disable silent mode, no longer display the contents of the mode space

-I: modify the original file directly

-e: execute multiple script-e address commands-e address commands at the same time.

-f specifies the file: sed-f / path/scripts file indicates that the contents of the script come out of the file

-r: supports extended regularization

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