Use of sed tool

Source: Internet
Author: User
The grep tool is not powerful enough. to put it bluntly, grep only implements the search function, but it cannot replace the search content. Previously, vim can be searched or replaced, but it is limited to operations within the text and cannot be output to the screen. Sed tool...
The grep tool is not powerful enough. to put it bluntly, grep only implements the search function, but it does not
You can replace the search content. Previously, vim can be searched or replaced, but it is limited
This operation is performed internally and cannot be output to the screen. Sed tool and the awk tool mentioned below can replace
Text is output to the screen, and there are other more functions. Both sed and awk are stream editors, which are
This operation is performed on the line of the document. Www.2cto.com
A. print the number of the sed-nnpfilename single quotation mark (n ).


B. print multiple lines to print the entire document. use-n1, p

C. print the rows containing a string

The special characters such as ^,., and * used in grep can also be used in sed.

D.-e can implement multiple actions

E. delete a row or multiple rows.

D is the delete action. it not only deletes the specified single row and multiple rows, but also deletes the matching.
In addition, you can delete a line from a line to the end of the document. Www.2cto.com

F. replace the character or string

In the above example, s is the replacement command, and g is the global replacement in this line. if g is not added, only the first one in this line appears.
In addition to/, you can also use other special characters such as # or.

Now let's think about how to delete all the numbers or letters in the document?

Interestingly, [0-9] indicates any number. You can also write [a-zA-Z] or [0-9a-zA-Z]

G. replace the positions of two strings

This should be explained. in the above example, we use () to enclose the characters to be replaced into a whole, because the brackets
Sed is a special symbol. Therefore, you need to add the escape character \ in front of it, and write it as the form of \ 1, \ 2, \ 3 when replacing it.
In addition to changing the positions of two strings, I often use adding specified content before or after a line.

H. directly modify the file content
Sed-is/: // #/g test.txt, you can directly change the content in the test.txt file. This command can
Directly modify the file. Therefore, you are advised to copy the file before modification to avoid errors.
This is what sed uses frequently. you can familiarize yourself with it as long as you practice more. To make you more secure
Master the application of sed, and I will leave you some exercise questions. I hope you can finish them carefully.
1. Copy/etc/passwd to/root/test.txt and print all rows with sed;
2.print 3 to 10 lines of test.txt;
3.print the test.txt line containing the root;
4.delete 15 rows of test.txt and all subsequent rows;
5.delete the line that contains bash from test.txt;
6.for test.txt, the root is toor;
7.replace/sbin/nologin in test.txt with/bin/login
8.delete all numbers in rows 5 to 10 from test.txt;
9.delete all special characters (except numbers and uppercase/lowercase letters) in test.txt );
10.change the positions of the first and last words in test.txt;
11.replace the first number and the last word in test.txt;
12.move the first digit in test.txt to the end of the row;
13.add aaa at the beginning of test.txt 20 and the last line :;
Now, I will give you the answers to the above exercise questions. if you really don't know how to do this, please take a look at the answers.
. Www.2cto.com
1./bin/cp/etc/passwd/root/test.txt; sed-n'1, $ 'p test.txt
2. sed-n'3, 10' p test.txt
3. sed-n'/root/'p test.txt
4. sed '15, $ 'd test.txt
5. sed '/bash/'d test.txt
6. sed's/root/toor/g'test.txt
7. sed's # sbin/nologin # bin/login # G' test.txt
8. sed '5, 10 s/[0-9] // g'test.txt
9. sed's/[^ 0-9a-zA-Z] // g'test.txt
10. sed's/\ (^ [a-zA-Z] [a-zA-Z] * \) \ ([^ a-zA-Z]. * \) \ ([^ a-zA-Z] \) \ ([a-zA-Z] [a-zA-Z] * $ \) /\ 4 \ 2 \ 3 \ 1 /'
Test.txt
11. sed
'S # \ ([^ 0-9] [^ 0-9] * \) \ ([0-9] [0-9] * \) \ ([^ 0-9]. * \) \ ([^ a-zA-Z] \) \ ([a-zA-Z] [a-zA-Z] * $ \) # \ 1 \ 5 \ 3 \ 4 \ 2 #'
Test.txt
12. sed's # \ ([^ 0-9] [^ 0-9] * \) \ ([0-9] [0-9] * \) \ ([^ 0-9]. * $ \) # \ 1 \ 3 \ 2 # 'test.txt
13. sed's/^. * $/& aaa/'test.txt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.