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

Detailed explanation and example of sed command

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

SED is a flow editor, it is a very good tool in text processing, can be perfectly used with regular expressions, extraordinary features. During processing, the currently processed line is stored in a temporary buffer called a "pattern space," and then the contents of the buffer are processed with the sed command. When the processing is completed, the contents of the buffer are sent to the screen. Then proceed to the next line and repeat until the end of the file. The file content does not change unless you use redirection to store the output. Sed is mainly used to edit one or more files automatically; simplify the iterative operation of files; write conversion programs, etc.

sed options, commands, replacement tags

command format sed [options] 'command' file(s)

sed [options] -f scriptfile file(s)

Options:

-e: Process the input text file with the script specified in the option

-f: Process the input text file with the script file specified in the option

-h: Show help

-n or--quiet: Read next line to pattern space

-v: Display version information

sed command:

-r: matching directory

a\Insert text below the current line.

i\Insert text above the current line.

c\Changes the selected line to new text.

d Delete, delete the selected row.

D Delete the first line of the template block.

s replaces the specified character h to copy the contents of the template block into a buffer in memory.

H appends the contents of the template block to a buffer in memory.

g Gets the contents of the memory buffer and replaces the text in the current template block.

G Gets the contents of the memory buffer and appends it to the end of the current template block text.

List cannot print a list of characters.

n Reads the next input line and processes the new line with the next command instead of the first.

N appends the next input line to the template block and inserts a new line between them, changing the current line number.

p Prints the lines of the template block.

P(upper case) Print the first line of the template block.

q Exit Sed.

b lable Branches to marked places in the script, or branches to the end of the script if no branch exists.

r file reads lines from file.

t label if branches, starting at the last line, and once the condition is satisfied or the T, t command causes a branch to the labeled command or to the end of the script.

T label error branch, starting at the last line, which causes a branch to the labeled command or to the end of the script if an error or T, t command occurs.

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

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

! Indicates that the following command works on all unselected lines.

= Print the current line number.

#Extend the comment until the next newline.

sed replacement tag

g indicates an overall replacement within a row.

p indicates print lines.

W represents writing lines to a file.

x means to interchange text in a template block with text in a buffer.

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

\1 Substring Match Tag

& Matched string tag

sed metacharacter set

^matches the beginning of a row, e.g.:/^sed/matches all rows starting with sed.

$matches the end of a row, e.g./sed$/matches all rows ending in sed.

. Matches an arbitrary character that is not a newline character, such as: /s.d/matches s followed by an arbitrary character, and finally d.

* Matches 0 or more characters, e.g.:/*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, e.g.:/[^A-RT-Z]ed/Matches a line that begins with a letter that does not contain A-R and T-Z, followed by ed.

\(..\) Match substrings, save matching characters, such as s/\(love\)able/\1rs, loveable is replaced with lovers. & Save search characters to replace other characters, such as s/love/** * *

\

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

/Matches lines containing words ending in love.

x\{m\} repeats the character x m times, e.g./0\{5\}/matches a line containing 5 zeros.

x\{m,\} repeats the character x at least m times, e.g./0\{5,\}/matches a line with at least 5 zeros.

x\{m,n\} Repeat the character x at least m times and no more than n times, such as: /0\{5 ~ 10\}/matching 5~10 lines of zeros.

Example 1: Print out the tenth line of 1.txt

p stands for print line

sed -n '10'p 1.txt

Without the-n parameter, it prints to line 10, and the 10th line is printed again.

sed '10'p 1.txt

Example 2. Print lines 1-10 of 1.txt

sed -n '1,10'p 1.txt

$End of matching row

sed -n '30,$' p 1.txt

Example 4: Print out the sd line in 1.txt

sed -n '/sd/'p 1.txt (keywords must be marked with//)

Example 5. Print any character in 1.txt that matches a (sd) non-newline character

. Matches an arbitrary character other than newline

sed -n '/s.d/'p 1.txt

Example 6. Print 0 or more characters matching (sd) in 1.txt

* Matches 0 or more characters

sed -n '/s*d/'p 1.txt

Example 7. Print out 1.txt matching 0 or one (sd) character

sed -n '/s\? d/' 1.txt

Example 8. Print out 1.txt matching 1 or more (sd) characters

sed -n '/s\+d/'p 1.txt

Example 9. Print out 1.txt matching s| D's writing.

sed -n 's\|d'p 1.txt

Example 10. Print out one or more pairs of 1.txt matching (ss)

\(..\) Match substrings, save matching characters

sed -n '/\(ss\)\+/p'1.txt

Note: grep can be used expression, sed can be used

Example 11. Print out the lines matching [a-z] in 1.txt

[] Matches a specified range of characters

sed -n -r '/[a-z]/'p 1.txt

Example 11. Print out lines matching [0-9] in 1.txt

sed -n -r '/[0-9]/'p 1.txt

Example 11. Print out the lines matching [3,s] in 1.txt

Write 1 sed -n -r '/[3,s]/'p 1.txt

Write 2 sed -n -r '/[3\*s]/'p 1.txt

Example 12. Delete blank lines in 1.txt

d Delete, delete the selected row.

sed '/^$/'d 1.txt

Example 13. Replace the s in lines 1-20 with w in 1.txt

s replaces the specified character h copies the contents of the template block into a buffer in memory

g Gets the contents of the memory buffer and replaces the text in the current template block.

sed '1,20s/d/w/g' 1.txt

Example 14. How to interchange the first and last paragraphs of 2.txt

sed -r 's#(^[a-z]+)(:.*:) (.*$)#\ 3\2\1#g' 2.txt (^[a-z]+ + first paragraph)(:.*:) Second paragraph (... *$) Paragraph 3,\3\2\1 Fixed substitution

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

Wechat

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

12
Report