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 use the Sed command in Linux?

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

Share

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

Sed introduction:

Sed is a text processing tool that reads text content and processes such as deleting, replacing, adding, etc., according to specified conditions.

Sed is a stream editor, it is a very important tool in text processing, can be used perfectly with regular expressions, and its function is remarkable. When processing, the currently processed line is stored in a temporary buffer, called "pattern space" (pattern space), and then the contents of the buffer are processed with the sed command, and when the processing is completed, the contents of the buffer are sent to the screen. Then process the next line, which is repeated until the end of the file. The contents of the file have not changed unless you use redirect to store the output.

Sed is mainly used to automatically edit one or more files, simplify the repeated operation of files, write conversion programs, and so on.

Work flow chart

The three main functions of Sed:

1. Read: sed reads a line from the input stream (file, pipe, standard input) and stores it in a temporary buffer

two。 Execution: by default, all sed commands are executed sequentially in the mode space, and sed commands will be executed on all lines in turn unless the address of the line is specified

3. Display: send the modified content to the output stream, and then send the data, the mode space will be emptied.

Note: by default, all sed commands are executed in the schema space, so the input file will not change unless you use redirection to store the output.

The Sed command has two formats:

Among them, "parameter" refers to the target file of the operation, which is used when there are multiple operating objects, and the files are separated by commas, while scriptfile represents the script file, which needs to be specified with the "- f" option. When the script file appears in front of the target file, it means that the input target file is processed through the specified script file.

Sed [option] 'Operation' parameter

Sed [option]-f scriptfile parameter

Common Sed command options:

Syntax: sed [options] [script] [inputfile]

-e: allows multiple scripts to be executed

-f: read commands from script files

-h: show help

-n: cancel the default output

-I: modify the original file directly

-l: specify the line length

-u: minimum cached inputs and outputs

-r: use regular expressions in scripts

Common operation options:

A: increase by adding a specified line below the current line

C: replace, replace the selected line with the specified content

D: delete, delete selected row

I: insert, insert a specified line above the selected row

P: print, if you specify a line at the same time, it prints the specified line; if you do not specify a line, it prints everything, usually with the-n option

S: replace, replace specified character

Y: character conversion

Sed metacharacter set

^: match lines begin, such as: / ^ sed/ matches all lines that start with sed.

$: match line ends, for example: / sed$/ matches all lines ending in sed.

. : matches any character that is not a newline character, such as: / s.d/ matches s followed by any character, followed by d

: matches 0 or more characters, such as: / * sed/ matches all lines in which 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 within the specified range, such as: / [^ A-RT-Z] ed/ matches the beginning of a letter that does not contain A M R and T M Z, followed by a line of ed.

(.): match the substring and save the matching characters, such as s / (love) able/\ 1rsrecoverable is replaced with lovers.

&: save search characters to replace other characters, such as love.

\

< : 匹配单词的开始,如:/\ : 匹配单词的结束,如/love\>

/ matches lines that contain words ending in love.

X {m}: repeat the character XBI m times, such as: / 0 {5} / matches a line with 5 zeros.

X {m,}: repeat the character x at least m times, for example: / 0 {5,} / matches lines with at least 5 zeros.

X {mdirection n}: repeat the character x at least m times, no more than n times, for example: / 0 {5 ~ 10} / matching lines with 5 ~ 10 zeros.

Sed common operation selection text

Sed-n 5p aa.txt

Sed-n'2p 5p 'aa.txt # Select lines 2 to 5

Sed-n '2pscape 5p' aa.txt # Select lines 2 and 5

Sed-n'/ ^ 5Compp aa.txt # lines starting with 5

Sed-n'$p 'aa.txt # print the last line

Sed-n '1lines 2p' aa.txt # means that every line will be printed from the first line

Sed-n'/ cc$/p' aa.txt # Lines ending in cc

Sed-n'/ fff/,10p' aa.txt # indicates printing from the oil string fff to line 10

Note: add-n only to show, will not change the original file, to change the original file, add sed-I.

Replace text

G: gets the contents of the memory buffer and replaces the text in the current module.

P: print the line of the template plate.

W: file writes and appends the mold plate to the end of the file.

Example:

Sed's Placement ePlacer 'aa.txt

In the full text of sed's aa.txt, e is replaced by E

Sed'1, 5s, aa.txt, e, between lines 1 and 5, e were replaced by E.

Sed'6 aa.txt / ^ 6 Charger f *

All lines starting with 6 are replaced by E for sed'/ ^ 6 aa.txt.

Note: without adding g, only the string that will appear for the first time on each line will be replaced, sed will not change the original file, slash separation is not necessary between parameters, and any character other than spaces and line breaks can also be used to change the original file, add-I.

Delete text

Sed-e '3d' aa.txt # Delete the third line

Sed-e '3pr 5d' aa.txt # Delete 3 to 5 lines

Sed-e'$d' aa.txt # Delete the last line

Sed-e '1dscape 5d' aa.txt # Delete 1BI 5 lines

Sed-e '1delete 2d' aa.txt # Delete odd lines

Sed-e '0deleted 2d' aa.txt # Delete even rows

Sed-e'1 aa.txt / ^ 6 go to DT # Delete lines starting with 1 to 6

Sed-e'5 aa.txt # deleted from 5 lines to the last

Sed'^ $d' aa.txt # Delete blank lines

Note: to delete the contents of the original file, add-I

Additional text

Sed '3a20200229' aa.txt # append after the third line

Sed'/ ^ 8gamma a2020008' aa.txt # append the line starting with 8

Note: you can also append it using echo, but >, > > is different.

Echo "9988" > > aa.txt is appended at the end of the text

Equal to sed'$a9988' aa.txt

Echo-e "999" > aa.txt directly overwrites the original text

Insert text

Sed '3i2128' aa.txt # third row insert

Sed'/ ^ 9Universe i2128' aa.txt # insert a line starting with 9

Sed'$i2348' aa.txt # the last line insert, which is actually the penultimate line

Combination command 1. Use the-e option

Sed-n-e's GRAMG-n-e's GRG'- e '2GHANG-e-e' 2MAL 3p 'aa.txt

two。 Split with semicolon

Sed-e's aa.txt Dhamma Dhampacht 3i6868p'Unix

3. Use multiple subcommands for an address

Sed-n '1pr 5 {

S/c/C/g

S/e/E/g

2i5566

P

} 'aa.txt

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