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

Linux basic command-text editing sed

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

Sed

Sed is a stream editor that reads content from an input stream and completes the conversion, either from a file or from a pipe.

The scope of this command: RedHat, RHEL, Ubuntu, CentOS, SUSE, openSUSE, Fedora.

1. Grammar

Sed [options] file

2. List of options

Option

Description

-- version

Display command version information

-- help

Show help documentation

-n |-- quite |-- slient

Silent mode

-e |-- expression=script

Add a script to an instruction

-f |-- file=script-file

Append the contents of the file as a script to the instruction

-- follow-symlinks

Follow symbolic links when handled in place; hard links will still be broken.

-I [SUFFIX] |-- in-place [= SUFFIX]

Edit the file in place (backup if an extension is provided). The default mode of operation is to break symbolic and hard links. This can be changed by following symbolic links and copying.

-c |-- copy

When shuffling files in-I mode, use copy instead of renaming. Although this will avoid breaking links (symbols or hard links), the editing of the results is not atomic. This is rarely the desired pattern;-following symbolic links is usually sufficient, and it is faster and more secure.

-l |-- line-length=N

Specify the maximum number of characters per line, and wrap automatically if you exceed it.

-- posix

Disable all GNU expressions

-r |-- regexp-extended

Use extended regular expressions in scripts.

S

Treat a file as a separate, rather than a separate, continuous long string

-u |-- unbuffered

Load a minimum amount of data from the input file and flush the output buffer more frequently.

If the-e,-- expression,-f, or-file options are not given, the first non-option parameter is interpreted as a sed script. The remaining parameters are the name of the input file; if no input file is specified, the standard input is read.

Command

Description

Command without address

: label

Labels for the b and t commands

# comment

The comment extends to the next line (or the end of the-e script snippet).

}

Closing parentheses of {} block

A command without an address or an address

=

Print the current line number

A\

Text

Append text with an embedded backslash before the newline character

I\

Text

Insert text with an embedded backslash before the newline character

Q [exit-code]

Exit the sed script immediately without processing any more input, and the current mode space will be printed unless automatic printing is not disabled. The exit code parameter is an GNU extension.

Q [exit-code]

Exit the sed script immediately without processing any more input. This is a GNU extension

R filename

Append text read from the file.

R filename

Append a line read from the file. Each call to the command reads a line from the file.

Accept commands for address range

{

Start a command block (ends with)

B label

Branch to the tag; if the tag is omitted, branch to the end of the script.

T label

If "sswap /" has been successfully replaced since the last input line was read and since the last t or T command, it branches from "branch to tag"; if "tag" is omitted, branch to the end of the script.

T label

If no "sbig /" has been successfully replaced since the last input line was read, and since the last t or T command, branch to tag; if omit, branch to end of script. This is an GNU extension.

C\

Text

Replace lines like Xu with text, with an embedded backslash before the newline character

D

Delete the schema space. Start the next cycle.

D

Removes the first embedded newline character in the schema space. Start the next cycle, but skip reading from input if there is still data in the pattern space.

H H

Copy / append mode space to hold space

G G

Copy / append hold space to pattern space

X

Swap content that holds spaces and pattern spaces

L

Lists the current line in the form of visual clarity.

L width

List the current line in the form of "visual clarity" and split it at the width width. This is an GNU extension.

N N

Read / append the next line of input in the pattern space

P

Print to current mode space

P

The first embedded newline character printed to the current mode space

S/regexp/replacement/

Try to match the regexp to the pattern space. If successful, replace the section with the replacement section. Substitutions can contain special characters & refer to that part of the matching pattern space, and special escapes\ 1 through\ 9 refer to the corresponding matching subexpressions in regexp.

W filename

Writes the current mode space to the file name

W filename

Writes the first line of the current mode space to the file name. This is an GNU extension.

Y/source/dest/

Transliterates the characters in the pattern space that appear in the source into the corresponding characters in dst.

3. Address

The SED command can be given without an address, in which case the command will be executed on all input lines; using one address, the command will be executed on only the input lines that match that address, or two addresses, in which case, the command will be executed on all input lines that match the range of include lines starting from the first address and continuing to the second address. There are three things to note about the address range: the syntax is addr 1 addr addr 2 (that is, addresses are separated by commas); lines that match with addr 1 will always be accepted, even if addr 2 selects the previous lines; and if addr 2 is regexp, it will not be tested against lines that match with addr 1.

After the address (or address range) and before the command, you can insert a "!" Which specifies that the command is executed only if the address or address range does not match The following address types are supported "

Number, matching only the specified line number.

First~step, starting with the specified line first, is matched once per step line.

$to match the last line.

/ regexp/, which matches the line of the regular expression regexp.

\ cregexpc, which matches the line of the regular expression regexp. C can be any character

0 addr2 addr2, starting with the "matching first address" state until the address is found. This is similar to 1dyne addr 2, but if addr 2 matches the first line of input, the 0meme addr 2 form will be at the end of its range, while the 1meme addr 2 form will still be at the beginning of its range. This only works if addr 2 is a regular expression.

Addr1,+N, which will match the N lines after addr1 and addr1.

Addr1,~N, which matches the lines after addr1 and addr1 until the next line is entered with a multiple of N.

4. Examples

Read content from one file to input to another file

[root@192 weijie] # cat 1.c / / View the contents of the file

Hello world

I am david.

I love linux

Love code.

Nihao ed

[root@192 weijie] # sed r 1.c > > 2.c / / read 1.c and write to 2.c

[root@192 weijie] # cat 2.c / / View content

one hundred and twenty three

twenty-three

two hundred and twelve

Hello world

I am david.

I love linux

Love code.

Nihao ed

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