Linux sed command learning experience

  

sed utility reads files into memory line by line in order. It then performs all the operations specified for the row and puts the row back into memory after completing the requested modification to dump it to the terminal. After completing all the operations on this line, it reads the next line of the file and then repeats the process until it completes the file. Sed reads the entire file by default and modifies each of the lines, but can limit the operation to the specified line as needed. Two things are important: the source file (by default) remains unmodified; the output can be redirected to another file to save changes. The usage syntax on the Internet is like this: sed [options] \\'{command}\\' [filename] [ > output_file]

But when I test locally, the \\ prompt of the command is reported. It would be fine to change \\'{command}\\' to '{command}', so the following commands removed \\.

To make a simple edit: ‘s/old value/new value/’[root@localhost etc]# echo 1 2 3 |  Sed 's/2/3/'

1 3 3

If you need to make multiple changes to the same line, the recommended method is: [root@localhost etc]# echo 1 2 3 4 5 |  Sed '> s/2/3/> s/4/5/> '

1 3 3 5 5

Split the command into multiple lines, each line is /The flag ends and finally 'closes all commands.

To make global changes: sed continues to process the next line after finding a project to modify and making changes, without continuing to read the rest of the line. When the modification target is the entire line, you need to use the global modification: [root@localhost etc]# echo 1 2 3 2 4 5 |  Sed 's/2/3/'

1 3 3 2 4 5

[root@localhost etc]# echo 1 2 3 2 4 5 |  Sed 's/2/3/g'

1 3 3 3 4 5

Change conditionally:

The following file is available [root@localhost root] # cat sample

one 1two 1three 1one 1two 1two 1three 1

To change 1 after two to 2, change 1 after three to 3 :[root@localhost root]# sed ' > /two/s/1/2/> /three/s/1/3/> ' sample

one 1two 2three 3one 1two 2two 2three 3

will output the changes to New file save: [root@localhost root]# sed '/two/s/1/2/; /three/s/1/3/' sample >sample_one[root@localhost root]# cat sample_one one 1two 2three 3one 1two 2two 2three 3

Use a pre-written script to process the specified file: [root@localhost root]# cat sedlist /two/s/1/2//three/s/1/3/[ ,null,null,3],Root@localhost root]# sed -f sedlist sampleone 1two 2three 3one 1two 2two 2three 3

It should be noted that neither the sedlist file nor the sed command has a '' pair of single quotes .

Processing the specified line:

Replace only 1 of 5 and 6 lines [root@localhost root]# sed '5,6 s/1/2/' sampleone 1two 1three 1one 1two 2two 2three 1

sed defaults to printing each line of the file to the screen, while sed -n overrides all displays and does not output to the screen. But the combination of -n and -p results in only the modified line: [root@localhost root]# cat sedlist /two/s/1/2/p/three/s/1/3/p[root @localhost root]# sed -n -f sedlist sampletwo 2three 3two 2two 2three 3

As you can see from above, “one 1” is not displayed because there is no modification.

Another usage is to display only a certain number of lines, such as 2~6 lines: [root@localhost root]# sed -n '2,6p' sampletwo 1three 1one 1two 1two 1

putting the file with two Delete the line: [root@localhost root]# sed '/two/d' sampleone 1three 1one 1three 1

Delete all lines except two: [root@localhost root]# sed '/two/! d' sampletwo 1two 1two 1

Delete the first three lines of the file: [root@localhost root]# sed '1,3 d' sampleone 1two 1two 1three 1

The special part of the deletion:

Delete lines starting with two: sed '/^two/d' sample_one

Delete lines ending in two: sed '/two$/d' sample_one

Delete empty lines in the file: sed '/^$/d' sample

To delete the title in an article, use the following syntax: sed '1, /^$/d' filename

Add and insert text:

Use sed and a, $ option to add content at the end of the article: [root@localhost root]# Sed '$a 123123123123' sampleone 1two 1three 1one 1two 1two 1three 1123123123123

If you want to add content to the third line, the method is: [root@localhost root]# sed '3a cccccccc' sampleone 1two 1three 1ccccccccone 1two 1two 1three 1

In addition to adding this method, there is also the option to insert (-i). For example, insert the content into the third line: [root@localhost root]# sed '3i cccccccc' sampleone 1two 1ccccccccthree 1one 1two 1two 1three 1

In the process of modifying the file, you can also read and write simultaneously. One file: [root@localhost root]# sed '/two/s/1/2/; /three/s/1/3/; 1,3 w sample_two' sampleone 1two 2three 3one 1two 2two 2three 3[root@localhost Root]# cat sample_two one 1two 2three 3

The above command outputs the modified 1~3 lines to the sample_two while replacing the two and three corresponding 1s.

Sed has a modification function (-c) in addition to the replacement function. The replacement is for a single project, and the modification is for the entire row. [root@localhost root]# echo two two |  Sed '/two/c dddddddd'dddddddd[root@localhost root]# echo two two |  Sed 's/two/dddddddd/'dddddddd two[root@localhost root]# echo two two |  Sed 's/two/dddddddd/g'dddddddd dddddddd

The above three commands explain their differences very well.

Exit early: sed does not have to exit after scanning all files, for example, only the first 5 lines: [root@localhost root]# sed '5q' sampleone 1two 1three 1one 1two 1

or exit after satisfying certain conditions (match to three and exit): [root@localhost root]# sed '/three/q' sampleone 1two 1three 1

There are some special cases, such as sample Replace two in the file with three, three replaced with four:[root@localhost root]# sed 's/two/three/; s/three/four/' sampleone 1four 1four 1one 1four 1four 1four 1

if According to the above command, you will find that all two and three have become four, which does not meet our requirements. [root@localhost root]# sed 's/three/four/; s/two/three/' sampleone 1three 1four 1one 1three 1three 1four 1

But after the two lines are reversed, the requirements are met. When performing this kind of operation, you must pay great attention to the way you specify the operations and arrange them in a certain order so that the operations do not affect each other.

Copyright © Windows knowledge All Rights Reserved