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

How to understand the sed command in Linux

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly analyzes the relevant knowledge points of how to understand the sed command in Linux, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor and learn more about "how to understand sed commands in Linux".

Sed is a "non-interactive" character stream-oriented online text editor that can process the contents of multiple files and lines at the same time without making changes to the source file.

Sed command

There are two forms of invoking the sed command:

*

Sed [options] 'command' file (s)

*

Sed [options]-f scriptfile file (s)

A\

Add a line of text after the current line.

B lable

Branch to the marked place in the script, or to the end of the script if the branch does not exist.

C\

Change the text of this line with the new text.

D

Delete rows from the Pattern space location.

D

Delete the first line of the template.

I\

Inserts text above the current line.

H

Copy the contents of the mold plate to the buffer in memory.

H

Append the contents of the template block to the buffer in memory

G

Gets the contents of the memory buffer and replaces the text in the current module plate.

G

Gets the contents of the memory buffer and appends it to the text of the current module block.

L

A list cannot print a list of characters.

N

Read the next input line and process the new line with the next command instead of the first command.

N

Append the next input line to the template block and embed a new line between the two to change the current line number.

P

Print the lines of the template plate.

P (uppercase)

Print the first line of the template plate.

Q

Exit Sed.

R file

Read lines from file.

T label

The if branch, starting on the last line, will result in a branch to the labeled command, or to the end of the script, once the condition is met or the TMaget command is met.

T label

The error branch, starting at the last line, will result in a branch to the labeled command or to the end of the script in the event of an error or the TMaget command.

W file

Write and append the template to the end of the file.

W file

Write and append the first line of the template block to the end of file.

!

Indicates that subsequent commands have an effect on all rows that have not been selected.

S/re/string

Replace the regular expression re with string.

=

Print the current line number.

#

Extends the comment before the next newline character.

The following is the replacement tag

*

G means full in-line replacement.

*

P represents a print line.

*

W means to write a line to a file.

*

X represents the text in the interchange mode plate and the text in the buffer.

*

Y means to translate one character into another (but not for regular expressions)

\ 4. Option

-e command,-expression=command

Multiple editors are allowed.

-h.-help.

Print the help and display the address of the bug list.

-n.-quiet.-silent.

Cancel the default output.

-f.-filer=script-file.

Boot sed script file name.

-V.-version.

Print version and copyright information.

\ 5. Metacharacter set ^

Anchoring the beginning of a line such as: / ^ sed/ matches all lines that begin with sed.

$

The end of an anchor line such as: / sed$/ matches all lines that end in sed.

.

Matches a non-newline character such as: / s.d/ matches s followed by any character followed by d.

*

Match zero or more characters such as: / * sed/ matches all lines where the template is one or more spaces followed by sed.

[]

Matches a specified range of characters, such as / [Ss] ed/ matches sed and Sed.

[^]

Matches a character that is not in the specified range, such as: / A-RT-Zed/ matches a line that does not contain a letter from Amurr and Tmurz, followed by ed.

(..)

Save matching characters, such as s / (love) able/\ 1rsrecoverable is replaced with lovers.

&

Save search characters to replace other characters, such as love.

Anchor the beginning of a word, such as: / love/ matches a line that contains a word that begins with love.

>

Anchor the end of the word, such as / love > / match the line that contains the word ending in love.

X {m}

Repeat the character xQuery m times, for example: / 0 {5} / matches a line containing 5 os.

X {m,}

Repeat the character x at least m times, such as: / o {5,} / matches a line with at least 5 o.

X {m,n}

Repeat the character x at least m times and no more than n times, such as: / o {5jue 10} / lines matching 5m o.

\ 6. Example

Delete: d command

*

$sed '2d' example-- deletes the second line of the example file.

*

D 'example-- deletes all lines from the second line to the end of the example file.

*

D 'example-- deletes the last line of the example file.

*

$sed'/ test/'d example-- deletes all lines in the example file that contain test.

Replace: s command

*

$sed's test TestUniGO example-- replaces test with mytest across the entire line. If there is no g tag, only the first matching test in each line is replaced with mytest.

*

The $sed-n's / ^ test / mytest/p' example-- (- n) option, used with the p flag, means that only those lines that are replaced are printed. That is, if the test at the beginning of a line is replaced with mytest, print it.

*

The $sed's / ^ 192.168.0.1 / & localhost/' example--& symbol indicates the part of the replacement string that was found. All lines starting with 192.168.0.1 are replaced with their own localhost and become 192.168.0.1localhost.

*

$sed-n's / (love) able/\ 1rsexample--love is marked as 1, all loveable will be replaced with lovers, and the replacement line will be printed.

*

No matter what character, the s command followed by the s command is considered to be the new delimiter, so "#" is the delimiter here instead of the default "/" separator. $sed's delimiter 10 characters 100% g 'character is considered the new delimiter after the s command. Means to replace all 10 with 100.

Range of selected lines: comma

*

$sed-n'/ test/,/check/p' example-- all lines within the range determined by the templates test and check are printed.

*

$sed-n'5J / ^ test/p' example-- prints all lines from the fifth line to the first line that contains the line starting with test.

*

/ sed test/' example-- for lines between template test and west, the end of each line is replaced with the string sed test.

Multipoint editing: e command

*

The $sed-e '1cr 5d'-e's example-- option allows multiple commands to be executed on the same line. As shown in the example, the first command deletes lines 1 to 5, and the second command replaces test with check. The order in which the command is executed affects the result. If both commands are replacement commands, the first replacement command affects the result of the second replacement command.

*

$sed-expression='s/test/check/'-expression='/love/d' example-- A better command than-e is-expression. It assigns values to sed expressions.

Read from file: r command

*

The contents of $sed'/ test/r file' example--file are read in and displayed after the lines that match the test, and if there are multiple lines, the contents of the file are displayed below all the matching lines.

Write to file: W command

*

$sed-n'/ test/w file' example-- all lines in example that contain test are written to file.

Append command: a command

*

$sed'/ ^ test/a\-> this is an example' example

Insert: I command

$sed'/ test/i\

New line

-- 'example

If the test is matched, the text after the backslash is inserted in front of the matching line.

Next: n command

*

$sed'/ test/ {n; s aa BB example-- if the aa is matched, move to the next line of the matching line, replace the aa of this line, change to bb, print the line, and then continue.

Morph: y command

*

$sed '1Magne10yCharacterABCDE example-- converts all abcde in 1Merry 10 lines to uppercase. Note that regular expression metacharacters cannot use this command.

Exit: Q command

*

After $sed '10q' example-- prints line 10, exit sed.

Keep and get: h command and G command

*

When G example-- processes files in sed, each line is saved in a temporary buffer called pattern space, and unless the line is deleted or the output is cancelled, all processed lines will be printed on the screen. The pattern space is then emptied and stored on a new line to be processed. In this example, when a row that matches test is found, it is stored in the pattern space, and the h command copies it and stores it in a special buffer called the hold cache. The second statement means that when the last line is reached, the G command takes out the line that holds the buffer, puts it back in the pattern space, and appends it to the end of the line that already exists in the pattern space. In this case, it is appended to the last line. Simply put, any line that contains test is copied and appended to the end of the file.

Keep and swap: h command and x command

*

$sed-e'/ test/h'-e'/ check/x' example-- swaps the mode space and holds the contents of the buffer. That is, swap lines that contain test and check.

\ 7. Script

The Sed script is a list of sed commands that boot the script file name with the-f option when Sed is started. Sed is very picky about the commands entered in the script. There can be no white space or text at the end of the command, and if there are multiple commands on one line, separate them with semicolons. Behaviors that begin with # comment lines and cannot span lines.

Sed is a prerequisite for learning automated shell scripts, and sed is also a very important command in Linux.

What are the versions of Linux? the versions of Linux are Deepin, UbuntuKylin, Manjaro, LinuxMint, Ubuntu and so on. Among them, Deepin is one of the best-developed Linux distributions in China; UbuntuKylin is a derivative release based on Ubuntu; Manjaro is a Linux release based on Arch; LinuxMint's default Cinnamon desktop is similar to Windows XP's easy-to-use; Ubuntu is the Linux operating system based on desktop applications.

This is the end of the introduction on "how to understand sed commands in Linux". More related content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!

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

Development

Wechat

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

12
Report