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 sed

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article is to share with you about how to use sed. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

For example, after multiple systems and applications are installed, we often need to modify many configuration files. Using a vi editor means time-consuming and repetitive work, while sed frees us from heavy repetitive work.

How to call sed:

1. Sed [sed option] 'sed command' file to be modified

2. Sed [sed option]-the file to be modified by f sed script

3. Sed script [sed option] File to be modified

Here is only the first way that is most commonly used.

Sed option: list only commonly used

-I: modify the source file directly (it cannot be modified directly without this option, it must be redirected to the new file, and this option is not needed if it is only used as an output test), and you can back up the source file before modification

Sed-i.bak 's test.txt 123 test.txt backup test.txt to test.txt.bak, and then replace the first occurrence of "123in the file" with "234"

-e: edit multiple times, for example, replace all 123 with 234, and then precede line 7 with a # comment

Sed-I-e's test.txt 123GI-e'e '7s / ^ / # /'

-f: specify the sed script file name

-n: cancel the default output (do not print). If you use sed without any option, you will see all the content output, not in accordance with the conditions restricted by the sed command. Then with the "p" command, you can filter out lines that do not meet the criteria and display lines that meet the criteria, such as

Sed-n 'find all lines containing character 123' p test.txt or sed-n 'find all lines containing character 123 p' test.txt

Sed command: regular expressions can be used here. If you encounter a special character, you need to mask its special meaning with "\". For example, "\ $" represents the ordinary character $.

X x is a line number

XQuery y, for example, 2, 5, from line 2 to line 5

/ val/ query lines that contain the character "val"

/ val1/val2/ query contains rows of two schemas

Val/,x queries the line containing "val" on the line with a given line number

XJM / rows that match by line number and pattern query

X,y! A line that does not contain the specified line number xQuery y

P print matching lines

= display line number

A\ append content after the positioned line

I\ insert content after the positioned line

D delete the positioned line, for example: 2d means delete line 2

C\ replace the text of the positioning line with the new text

S replacement, in the form:'s / what is found / what is replaced /'

G is replaced globally, and if you do not use this option, only the first result to which the row matches will be replaced, and the next match in the row will not be processed.

... The rest will not be introduced. Let's search Baidu.

Case study:

The content of test.txt file is

The honeysuckle band played all night long for only $90

It was an evening of splendid music and company

Too bad the disco floor fell through at 23:10

The local nurse Miss P.Neave was in attendance

1. Display the contents of line 2

Sed-n '2p' test.txt

2. Display the contents of lines 1 to 3

Sed-n'1P test.txt

3. Show only lines that contain "disco"

Sed-n'/ disco/'p test.txt

4. Display lines that contain the "$" character

Sed-n'/\ $/'p test.txt

5. Displays the line ending with a number. [0-9] is a regular expression representing the numbers 0 to 9; "$" indicates the end of the line ("^" represents the beginning of the line).

Sed-n'/ [0-9] $/ 'p test.txt

The display results are as follows:

The honeysuckle band played all night long for only $90

Too bad the disco floor fell through at 23:10

6. Display lines that end with numbers and lines that begin with an uppercase T.

Sed-n-e'/ ^ T test.txt p-e'/ [0-9] $/'p

7. Lines that match any letter, followed by multiple repetitions of any letter, and end with "ing"

Sed-n'/. * ing/'p test.txt

8. The first and last lines

Sed-n '1p' test.txt

Sed-n'$p 'test.txt

9. Change "nurse" to "little nurse", and the "&" command is used to re-invoke the replaced content.

Sed-n 's/nurse/little & / p' test.txt

10. Replace all 123 with 234, and then precede line 7 with a # note

Sed-I-e's test.txt 123GI-e'e '7s / ^ / # /'

Delete the "-", delete the blank line, delete the first and last lines, and print the first column

Contents of the file:

Database Size (MB) Date Created

-

Mysql 2244 12-11-08

Test 5632 12-11-08

(2 rows affected)

Command:

Cat test.txt | sed's print print Universe G' | sed'/ ^ $/ d' | sed'$d' | sed '1d' | awk' {awk $1}'

Display the results:

Mysql

Test

Description:

Delete the horizontal line using sqqure Mustang Uniplank g-

Use / ^ $/ d to delete blank lines

Delete the last line using $d

Delete the first line using 1d

Awk {print $1} print the first column

12. Some miscellaneous examples

^ [0-9] indicates that the first character of the line is an arbitrary number, such as "1asdf"

^ [0-9] * indicates that the beginning of a line contains any number of numbers, such as "1818asdf"

[0-9] [0-9] * $indicates that the end of the line contains at least 2 digits, such as "asdfasdf18" and "asdf1818"

Sed-I's / ^ [0-9] * / / g 'test.txt deletes any number at the beginning of a line

Sed-I-e's / ^ [0-9] * / / g'- I-e's passed/' test.txt delete any number at the beginning of the line and add "passed" to the end of each line

Sed-I-e's / ^ / # & 'test.txt annotates each line at the beginning of each line

S /\. $/ / g Delete the period of a line ending with a period

S / ^ [] [] * / / Delete any space at the beginning of the line

S / ^. / / delete the first character at the beginning of the line

S / ^\ / / delete the "/" character at the beginning of the line

S/SP\ (..\) / / g remove the character "SP" and the two arbitrary characters immediately following it, "SPLLY"-- > "Y"

Thank you for reading! This is the end of the article on "how to use sed". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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