Introduction to the use of SED tools for Linux

Source: Internet
Author: User

Introduction to the use of SED tools

In fact, the functionality of the grep tool is not strong enough, it is only to find the function, but not to replace the search, the previous use of Vim to manipulate the document, can be found can also be replaced, but only in the text inside the operation, and not output to the screen, the SED tool can be replaced with the text output to the screen, And there are other richer features. Sed is a streaming editor that operates on the lines of a document.

1. The format of the SED command to print a line is: Sed-n ' n ' p filename explanation: N in single quotation marks is a number representing the first line, and the-n option is to show only the rows we want to print, and the irrelevant content is not displayed.

Examples are as follows:

[[email protected] grep]# sed-n ' 2 ' p passwdbin:x:1:1:bin:/bin:/sbin/nologin

If you want to print all the rows, you can use the command sed-n ' 1,$ ' p filename, as shown in the following example:

[Email protected] grep]# sed-n ' 1,$ ' P test.txt rot:x:o:o:/rot:/bin/bashoperator:x:11:o:operator:/root:/sbin/ nologinoperator:x:11:o:operator:/rooot:/sbin/nologinroooot:x:o:o:/roooooot:/bin/ Bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

You can also specify an interval to print, as in the following example:

[Email protected] grep]# sed-n ' 1,$ ' P test.txt rot:x:o:o:/rot:/bin/bashoperator:x:11:o:operator:/root:/sbin/ nologinoperator:x:11:o:operator:/rooot:/sbin/nologinroooot:x:o:o:/roooooot:/bin/ Bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2. Print the line containing a string:

Examples are as follows:

[Email protected] sed]# sed-n '/root/' P test.txt root:x:0:0:root:/root:/bin/bashoperator:x:11:0:operator:/root:/ Sbin/nologin

[[email protected] sed]# sed -n  '/^1/' p /root/grep/ test.txt1111111111111111111111111111111[[email protected] sed]# sed -n  '/in$/' p  /root/grep/test.txtoperator:x:11:o:operator:/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/nologin[[ email protected] sed]# sed -n  '/R.. o/' P /root/grep/test.txtoperator:x:11:o:operator:/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin /nologinroooot:x:o:o:/roooooot:/bin/bash[[email protected] sed]# sed -n  '/ooo*/' p  /root/grep/test.txtoperator:x:11:o:operator:/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/ Nologinroooot:x:o:o:/roooooot:/bin/bash 

The SED command plus the-e option allows you to implement multiple behaviors, as follows:

[[email protected] sed]# sed-e ' 1 ' p-e '/111/' p-n/root/grep/test.txtrot:x:o:o:/rot:/bin/ bash1111111111111111111111111111111
3. Delete some rows

Examples are as follows:

[[email protected] sed]# sed  ' 1 ' d /root/grep/test.txt operator:x:11:o:operator :/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/nologinroooot:x:o:o:/roooooot:/bin/ Bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] sed]# sed   ' 1,3 ' d /root/grep/test.txt roooot:x:o:o:/roooooot:/bin/ Bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] sed]# sed   '/oot/' d /root/grep/test.txt rot:x:o:o:/rot:/bin/ BASH1111111111111111111111111111111AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 

Explanation: The parameter d here represents the deleted action, which can delete not only the specified single row and multiple rows, but also the rows that match a character, and delete all rows starting from one line to the last line of the document. However, this operation is only on the monitor screen does not display these lines, the document is still good, do not worry.

4. Replacing characters or strings

[[email protected] sed]# cat /root/grep/test.txt //Original File rot:x:o:o:/rot:/bin/ bashoperator:x:11:o:operator:/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/nologinroooot:x:o:o:/ roooooot:/bin/bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected]  sed]# sed  ' 1,2s/ot/to/g '  /root/grep/test.txt   //executes the command after the file, looking closely at the difference between the two. rto:x:o:o:/rto:/bin/bashoperator:x:11:o:operator:/roto:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/ NOLOGINROOOOT:X:O:O:/ROOOOOOT:/BIN/BASH1111111111111111111111111111111AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 

Explanation: The parameter S in the example above represents the action of substitution, the parameter G indicates the bank's global substitution, and if no g is replaced only the first one that we appear, this usage is actually the same as the one for each vim.

###### Extension Knowledge: In addition to using/as separators, we can also use other special characters, such as # and @, as follows:

[[email protected] sed]# cat /root/grep/test.txt //Original File rot:x:o:o:/rot:/bin/ bashoperator:x:11:o:operator:/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/nologinroooot:x:o:o:/ roooooot:/bin/bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected]  sed]# sed  ' 1,2s/ot/to/g '  /root/grep/test.txt   //executes the command after the file, looking closely at the difference between the two. rto:x:o:o:/rto:/bin/bashoperator:x:11:o:operator:/roto:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/ NOLOGINROOOOT:X:O:O:/ROOOOOOT:/BIN/BASH1111111111111111111111111111111AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 

So how do we think about deleting all the numbers or letters in a document? The example commands are as follows:

[[Email protected] sed]# sed ' s/[0-9]//g '/root/grep/test.txt rot:x:o:o:/rot:/bin/bashoperator:x::o:operator:/root:/ Sbin/nologinoperator:x::o:operator:/rooot:/sbin/nologinroooot:x:o:o:/roooooot:/bin/bash

Explanation: [0-9] means any number, and here you can also write [a-za-z] or [0-9a-za-z]. As shown below:

Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] sed]# sed ' s/[a-za-z]//g '/root/grep/test.txt::::/://::11:::/://: : 11:::/://::::/://1111111111111111111111111111111
5. Swap the position of two strings

[[email protected] sed]# cat /root/grep/test.txt //View Original File rot:x:o:o:/rot:/bin/ bashoperator:x:11:o:operator:/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/nologinroooot:x:o:o:/ roooooot:/bin/bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected]  sed]# sed  ' s/\ (rot\) \ (. *\) \ (bash\)/\3\2\1/'  /root/grep/test.txt bash:x:o:o:/rot:/bin/ rotoperator:x:11:o:operator:/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/nologinroooot:x:o:o:/ ROOOOOOT:/BIN/BASH1111111111111111111111111111111AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 

Explanation: The parentheses are special symbols in the SED and must be preceded by an escape character \, and replaced with a form similar to \1, \2, or \3. In the example above (), the characters you want to replace are packaged as a whole.

Having this escape character \ will make the expression look like a slot, you can omit it in a different way, as follows:

[Email protected] sed]# sed-r ' s/(Rot) (. *) (bash)/\3\2\1/'/root/grep/test.txt bash:x:o:o:/rot:/bin/rotoperator:x:11 : o:operator:/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/nologinroooot:x:o:o:/roooooot:/bin/ Bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

As you can see from here, this-r option makes the expression clearer. In addition to changing the position of two strings, you can also use SED to add the specified content around a line, as follows:

[[Email protected] sed]# sed ' s/^.*$/123&/'/root/grep/test.txt 123rot:x:o:o:/rot:/bin/bash123operator:x:11:o:o perator:/root:/sbin/nologin123operator:x:11:o:operator:/rooot:/sbin/nologin123roooot:x:o:o:/roooooot:/bin/ Bash1231111111111111111111111111111111123aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
6. Directly modify the contents of the file

[[email protected] sed]# cat /root/grep/test.txt rot:x:o:o:/rot:/bin/bashoperator:x : 11:o:operator:/root:/sbin/nologinoperator:x:11:o:operator:/rooot:/sbin/nologinroooot:x:o:o:/roooooot:/bin/ Bash1111111111111111111111111111111aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[[email protected] sed]# sed  -i  ' s/ot/to/g '  /root/grep/test.txt [[email protected] sed]# cat /root /grep/test.txt rto:x:o:o:/rto:/bin/bashoperator:x:11:o:operator:/roto:/sbin/nologinoperator:x:11:o:o perator:/rooto:/sbin/nologinroooto:x:o:o:/roooooto:/bin/ BASH1111111111111111111111111111111AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 

Explanation: This allows you to directly modify the contents of the Test.txt file, but it is important to remember to back up a file before you modify it to avoid errors.


This article is from the "Gary Blog" blog, please be sure to keep this source http://taoxie.blog.51cto.com/10245493/1983919

Introduction to the use of SED tools for Linux

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.