April 26

9.4-9.5 sed

In fact , the function of the grep tool is not powerful enough. It only implements the search function, but cannot replace the searched content. When using vim to operate the document before, it can search and replace, but it is only limited to the operation inside the text, not output to On the screen, the sed tool can output the replaced text to the screen , and there are other richer functions. sed is a streaming editor that operates on lines of a document.

Common options: 

-n : output the lines processed by the sed command to the screen 

-e : Allow multiple sed command edits to be applied to the input data 

-i : directly modify the file that reads the data with the modification result of sed , instead of outputting it on the screen 

-a : append, add one or more lines after the current line 

-c : line replacement, replace the original data line with the string after c 

-i : insert, insert one or more rows before the current row 

-d : delete, delete the specified line 

-p : print, enter the specified line 

-s : String substitution , replace one string with another. The format is " line range s/ old string / new string /g"

 

1.1 print a line 

 Command format: sed -n 'n'  p filesname 

Explanation: The n inside the single quotation marks is a number, which indicates the number of lines. The function of the -n option is to display only the line we want to print, and the irrelevant content is not displayed.

blob.png 

1.1.2 Print all line commands: sed -n ' 1,$ 'p passwd 

blob.png 

1.1.3 Specify an interval print command: sed -n ' 1,3 'p passwd

blob.png 

2.1 Print lines containing a certain string

Command: sed -n '/root/'p passwd

blob.png 

Similar to grep , special characters used in grep (such as ^ , $ , . , * etc.) can also be used in sed

2.1.1 Print the line command starting with 1 : sed -n '/^1/'p passwd   

blob.png 

2.1.2 Print lines ending with in : sed -n '/in$/'p passwd

blob.png 

2.1.3 Print a line command with two arbitrary characters before r and o : sed -n  '/r..o /'p passwd

blob.png 

2.1.4 Print a line command with zero or more ooos : sed -n '/ooo*/'p passwd

blob.png 

3. -e can implement multiple behaviors 

Command: sed -e '1'p -e '/111/'p -n passwd or 

            sed -e '1p;/111/p' -n passwd 

blob.png 

4. Delete a certain line or multiple lines  ( the parameter d here represents the action of deletion, it can not only delete the specified single line and multiple lines, but also delete the line matching a certain character, and it can also delete from the beginning of a certain line to the last line of the document All lines. However, this operation just does not display these lines on the monitor screen, the document itself has not done the delete action , don't worry. )

Command: sed '1'd passwd or sed '1,3'd passwd

blob.png 

 

blob.png 

5. Replace characters or strings 

Command: sed '1,2 s /ot/to/ g ' passwd \\ replace the first and second lines of ot with to

 ('s' is the replacement command, 'g' is the global replacement in the line, if you don't add 'g' , only the first one that appears in the line will be replaced )

blob.png 

5.1 In addition to '/' as a separator, other special characters can also be used

For example , '#' or '@' are acceptable, command: sed '1,2s#ot#to#g' passwd

blob.png 

5.2 文档中所有字母或者数字替换成空 删除文档中所有的数字或字母

解释:[0-9]表示任意的数字,[a-zA-Z]表示所有的大小写字母或者[0-9a-zA-z]所有数字和字母

命令:sed 's/[a-zA-Z]//g' passwd 或者sed 's/[0-9]//g' passwd

blob.png 

 

blob.png 

6.调换两个字符串的位置 

命令:sed 's/\(rot\)\(.*\)\(bash\)/\3\2\1/'

小括号sed中属于特殊符号,必须在前面加转义字符\,替换时则写成类似\1\2、或\3的形式。上例中用()把想要替换的字符打包成了一个整体。

有这个转义字符\,会让这个表达式看起来乱槽槽的,可以换一种方式省略它,如下所示

Command: sed -r  's/(rot)(.*)(bash)/\3\2\1/' passwd ( you can omit the escape character \)

blob.png 

It is to add this -r option to make this expression clearer

blob.png 

6.1 In addition to swapping the positions of two strings, you can also use sed to add specified content before and after a line , as shown below:

Command: sed 's /^.*$ / 123& /' passwd

blob.png 

7. Modify the content of the file directly (make a backup in advance ) 

命令:sed -i 's/ot/to/g' passwd

blob.png 

In this way, the file can be modified directly , so make a backup before modifying it. It is best to copy the file first to avoid mistakes.

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324976173&siteId=291194637