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

Three Musketeers of regular expressions in Shell programming-- sed tool

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

Share

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

Overview of sed tools

Good at processing data rows, sed is a stream editor that stores the currently processed rows in a temporary buffer, called "pattern space", and then uses the sed command to process the contents of the buffer. When the processing is complete, 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. Using the sed command, you can replace, delete, add, select and other specific tasks of data rows.

Common usage of sed commands sed [options] 'Operation' parameters sed [options]-the common sed command options for the f scriptfile parameter mainly include the following

-e or-- expression=: means to process the input text file with a specified command or script.

-f or-- file=: means to process the input text file with the specified script file.

-h or-- help: displays help.

-n,-- quiet, or silent: indicates that only the processed results are displayed.

-I: edit the text file directly.

"Action" is used to specify the action behavior of the operation on the file, that is, the command of sed. The format of the "[N1 [, N2]]" operation parameter is usually used. N1 and N2 are optional and do not necessarily exist, representing the number of rows selected for operation

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

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

D: delete, delete the selected row.

I: insert, inserting 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 don't specify a line, it prints everything; if there are non-printed characters, it is output in ASCII code. It is usually used with the "- n" option.

S: replace, replace the specified character.

Y: character conversion.

Use example 1, output qualified text (p represents normal output)

Sed-n 'p' test.txt outputs everything

[root@localhost ~] # sed-n 'p' test.txt the footballyou are best boyPI=3.1415926535897a wood crosshello.the boy

Sed-n '2p' test.txt output the contents of line 2

Sed-n'2 5p 'test.txt output 2 to 5 lines

Sed-n'ptern 'test.txt outputs the contents of all odd lines, and n means to read the next line of data

Sed-n'nposition p' test.txt outputs the contents of all even lines

[root@localhost] # sed-n'ptern 'test.txtthe footballPI=3.1415926535897the boy [root@localhost ~] # sed-n'nten p'test.txt you are best boya wood crosshello.

Sed-n'2 test.txt 5 {pshare n} 'output odd lines from lines 2 to 5

At this time, the odd row is the original second row, which is determined according to the odd and even number of the selected rows.

[root@localhost ~] # sed-n'2 test.txt you are best boya wood cross 5 {pten n}'

These are the basic uses of the sed command. When the sed command is combined with a regular expression, the format is slightly different, which is surrounded by "/". For example, the following is an example of the sed command used in conjunction with a regular expression.

[root@localhost ~] # sed-n'/ the/p' test.txt output line the footballthe boy containing the

Sed-n'4 the test.txt output from the fourth line to the line containing the first the

[root@localhost] # sed-n'4 go back to the pound p 'test.txta wood crosshello.the boy

The sed-n'/ the/=' test.txt output contains the line number of the the

[root@localhost ~] # sed-n'/ the/=' test.txt17

Sed-n'/ ^ PI/p' test.txt outputs lines that start with PI

Sed-n'/ [0-9] $/ p 'test.txt outputs lines ending with numbers

Sed-n'/\ / p 'test.txt output the line containing the word wood

[root@localhost ~] # sed-n'/ ^ PI/p' test.txtPI=3.1415926535897 [root@localhost ~] # sed-n'/ [0-9] $/ p 'test.txtPI=3.1415926535897 [root@localhost ~] # sed-n' /\ / p 'test.txta wood cross2, delete the nl command under the eligible text (d) to calculate the number of lines of the file, combined with this command to see the result of the command execution more intuitively

Nl test.txt | sed '3d' delete the third line

Nl test.txt | sed '3pr 5d' delete 3 to 5 lines

Nl test.txt | sed'/ cross/d' delete the line with cross does not include use! Take reverse

[root@localhost ~] # nl test.txt | sed '3d' 1 the football 2 you are best boy 4 a wood cross 5 hello. 6 the boy [root@localhost ~] # nl test.txt | sed'3 the football 5d'1 you are best boy 5 hello. 6 the boy [root@localhost ~] # nl test.txt | sed'/\ / d' 1 the football 2 you are best boy 3 PI=3.1415926535897 4 a wood cross 6 the boy

Sed'/ ^ [a-zA-Z] / d'test.txt deletes lines that begin with a letter

Sed'/. $/ d' test.txt is deleted to. The line at the end

Sed'/ ^ $/ d'test.txt deletes the blank line

[root@localhost ~] # sed'/ ^ [a-zA-Z] / d'test.txt [root@localhost ~] # sed'/\. $/ d' test.txtthe footballyou are best boyPI=3.1415926535897a wood crossthe boy [root@localhost ~] # sed'/ ^ $/ d' test.txtthe footballyou are best boyPI=3.1415926535897a wood crosshello.the boy

Note: if you delete duplicate blank lines, that is, only one consecutive blank line is left, you can do so by executing the command "sed-e'/ ^ $/ {n X / ^ $/ d} 'test.txt". The effect is the same as that of "cat-s test.txt". N means to read the next line of data.

3, replace the eligible text

S (string substitution)

C (whole line / block replacement)

The common usage of the y (character conversion) command option is as follows.

4. Migrate eligible text

H, copy to the clipboard

G, G, overwrite / append the data from the clipboard to the specified row

W, save as a file

R, read the specified file

A, append the specified content.

5, use script to edit the file

Using the sed script, multiple editing instructions are stored in a file (one for each line) and called through the "- f" option.

For example: sed'1 vi opt.list 5 {Htterd}; 17G 'test.txt / / after transferring the contents of line 1 to line 5 to line 17, the above operations are changed to script files [root@localhost ~] # H1Magol 5d17G [root@localhost ~] # sed-f opt.list test.txt6,sed direct operation file example

Write a script to adjust the vsftpd service configuration: anonymous users are prohibited, but local users are allowed (and writes are also allowed).

Thank you for reading!

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