Linux sed command detailed

Source: Internet
Author: User

Introduction

Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on.

sed usage Parameters
[[Email protected] ~]# sed [-NEFR] [action] options and Parameters:-N: Use Quiet (silent) mode. In the usage of general sed, all data from STDIN is generally listed on the terminal. However, if you add the-n parameter, only the line (or action) that is specially processed by SED is listed. -E: The action edit of SED directly in the command-line mode;-F: The action of SED is written in a file directly, and-f filename can run the SED action within filename; The-r:sed action supports the syntax of the extended formal notation. (The default is the basic formal notation of French)-I: Directly modifies the contents of the read file, not the output to the terminal. Action Description: [N1[,n2]]functionn1, N2: Not necessarily exist, generally represents "select the number of lines of action", for example, if my action is required between 10 to 20 rows, then "10,20[Action Behavior" "FUNCTION:A: New, the back of a The strings can be connected, and the strings will appear on a new line (the current next line) ~c: Replace, C can be followed by a string, these strings can replace the line between N1,N2! D: Delete, because it is deleted, so d usually do not take any knock; I: Insert, I can be followed by a string, and these strings will appear on a new line (the current line); P: Print, that is, print out a selected data. Normally p will run with parameter sed-n ~s: Replace, can be directly replaced by work! Usually this s action can be paired with formal notation! For example 1,20s/old/new/g is!

Add/Remove as behavior unit


List the contents of the/etc/passwd and print the line number, and at the same time, delete the 2nd to 5th line!

[Email protected] ~]# NL/ETC/PASSWD | Sed ' 2,5d ' 1 root:x:0:0:root:/root:/bin/bash6 sync:x:5:0:sync:/sbin:/bin/sync7 shutdown:x:6:0:shutdown:/sbin:/sbin/ Shutdown ..... (omitted later) .....


Sed's action is ' 2,5d ', and that D is delete! Because 2-5 rows to him deleted, so the data displayed there is no 2-5 line Luo ~ In addition, note that the original should be issued SED-E only, no-e also line! Also note that the action behind the SED, be sure to enclose in ' two single quotes!

Just delete line 2nd

To delete 3rd to last row

Add "drink Tea" after the second line (i.e. the third line) Words!

[Email protected] ~]# NL/ETC/PASSWD | Sed ' 2a drink tea ' 1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bin:/bin:/sbin/nologindrink tea3 daemon:x:2:2:daemon:/ Sbin:/sbin/nologin ..... (omitted later) .....

So if it's going to be before the second line,

If you want to add more than two lines, add two lines after the second line, such as "Drink tea or ..." and "Drink beer?"

[Email protected] ~]# NL/ETC/PASSWD | Sed ' 2a Drink tea or ......> Drink beer? ' 1 root:x:0:0:root:/root:/bin/bash2 bin:x:1:1:bin:/bin:/sbin/nologindrink tea or ... drink beer? 3 Daemon:x:2:2: Daemon:/sbin:/sbin/nologin ..... (omitted later) .....

Each row must be a backslash "\" To add a new line Oh! So, in the example above, we can see that there is a \ presence on the last side of the first line.


Replace and display with the behavior unit


Replace the contents of the 2–5 line as "No 2-5 number"?

[Email protected] ~]# NL/ETC/PASSWD | Sed ' 2,5c No 2-5 number ' 1 Root:x:0:0:root:/root:/bin/bashno 2-5 number6 sync:x:5:0:sync:/sbin:/bin/sync ..... (omitted later) .....


In this way we are able to replace the entire line of data!

List only 第5-7 lines within a/etc/passwd file

[Email protected] ~]# NL/ETC/PASSWD | Sed-n ' 5,7p ' 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin6 sync:x:5:0:sync:/sbin:/bin/sync7 shutdown:x:6:0:shutdown:/ Sbin:/sbin/shutdown

It is possible to select certain line numbers within a file to display through the display function of the SED in the behavior unit.

Search and display of data

Search for a line with the root keyword/etc/passwd

nl/etc/passwd | Sed '/root/p ' 1  root:x:0:0:root:/root:/bin/bash1  root:x:0:0:root:/root:/bin/bash2  daemon:x:1:1: Daemon:/usr/sbin:/bin/sh3  bin:x:2:2:bin:/bin:/bin/sh4  sys:x:3:3:sys:/dev:/bin/sh5  

If Root is found, the matching rows are also output in addition to outputting all rows.

When you use-N, only the rows that contain the template are printed.

nl/etc/passwd | Sed-n '/root/p ' 1  root:x:0:0:root:/root:/bin/bash

Search for and delete data

Delete/etc/passwd all rows containing root, other rows output

nl/etc/passwd | Sed  '/root/d ' 2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh3  bin:x:2:2:bin:/bin:/bin/sh .... The following ignores # The first line of the match root has been deleted

Search for data and execute commands

After you find the line that matches the pattern eastern,

Search for/etc/passwd, locate the root row, execute a set of commands in parentheses, and separate each command with a semicolon, where bash is replaced with Blueshell, and then the line is output:

nl/etc/passwd | Sed-n '/root/{s/bash/blueshell/;p} '
1 Root:x:0:0:root:/root:/bin/blueshell

If you only replace/etc/passwd's first bash keyword as Blueshell, exit

nl/etc/passwd | Sed-n '/bash/{s/bash/blueshell/;p; q} '    1  Root:x:0:0:root:/root:/bin/blueshell

The last Q is the exit.

search for and replace data

In addition to the entire line of processing mode, SED can also be used to search and replace part of the data in the behavioral units. Basically, the search for SED is similar to the VI equivalent to the alternative! He's kind of like this:

Sed ' s/to be substituted string/new string/g '

First look at the original information, using/sbin/ifconfig query IP

[Email protected] ~]#/sbin/ifconfig eth0eth0 Link encap:ethernet HWaddr 00:90:cc:a6:34:84inet addr:192.168.1.100 Bcast : 192.168.1.255 mask:255.255.255.0inet6 addr:fe80::290:ccff:fea6:3484/64 scope:linkup broadcast RUNNING MULTICAST MTU : Metric:1 ..... (omitted below) .....


The IP of this machine is 192.168.1.100.

Delete the previous part of IP

[Email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' | Sed ' s/^.*addr://g ' 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0

The next step is to delete the following sections, namely: 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0

Remove the parts that follow the IP

[Email protected] ~]#/sbin/ifconfig eth0 | grep ' inet addr ' | Sed ' s/^.*addr://g ' | Sed ' s/bcast.*$//g ' 192.168.1.100

Multi-point editing

An sed command that removes data from the third line to the end of/etc/passwd and replaces bash with Blueshell

nl/etc/passwd | Sed-e ' 3, $d '-e ' s/bash/blueshell/' 1  root:x:0:0:root:/root:/bin/blueshell2  daemon:x:1:1:daemon:/usr/sbin :/bin/sh

-E for multi-point editing, the first edit command deletes the data from the third line to the end of the/etc/passwd, and the second command searches for bash instead of Blueshell.

Modify file contents directly (Dangerous action)


Sed can directly modify the contents of a file without using a pipe command or data flow redirection! However, because this action will be directly modified to the original file, so please do not take the system configuration to test! Let's use the downloaded Regular_express.txt file to test it out!

Use SED to end each line in the Regular_express.txt. Then replace it!

[Email protected] ~]# sed-i ' s/\.$/\!/g ' regular_express.txt

Use sed to add "# is a test" directly to the last line of Regular_express.txt

[[email protected] ~]# sed-i ' $a # This is a test ' regular_express.txt

Since $ represents the last line, and A's action is new, the file is finally added "# This is a test"!

Sed "-i" option can directly modify the file content, this feature is very helpful! For example, if you have a 1 million-line file that you want to add some text to in line 100th, using VIM at this point may go insane! Because the file is too big! What do you do? Just use sed! You don't even need to use VIM to revise the features directly modified/replaced by SED!

Reference http://vbird.dic.ksu.edu.tw/linux_basic/0330regularex_2.php#sed

Http://www.cnblogs.com/stephen-liu74/archive/2011/11/17/2245130.html

Http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2856901.html

Linux sed command explained (GO)

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.