Everyday One Linux command -18 (SED) __linux

Source: Internet
Author: User
Linux sed command detailed Brief Introduction

Sed is an online editor that handles a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer, known as pattern spaces, followed by the SED command to handle the contents of the buffer, and the contents of the buffer are sent to the screen after processing is complete. The next line is processed so that it repeats until the end of the file. The contents of the file do not change unless you use redirected storage output. SED is used to automatically edit one or more files, to simplify the repetitive operation of files, to write conversion programs, and so on.

sed Using Parameters

[Root@www ~]# sed [-NEFR] [action]
options and Parameters:-
N: Use Quiet (silent) mode. In general sed usage, all data from STDIN is generally listed on the terminal. However, if you add the-n parameter, only the line (or action) that is specifically handled by SED will be listed. -
e: SED action edits directly on command-line mode;-
f: Writes the SED action directly in a file, and-f filename can run the SED action within filename;
-r:sed action supports extended formal The syntax of the notation. (Default is the basic formal representation of French law)
-I: Directly modify the contents of the read file, rather than output to the terminal.

Action Description: [n1[,n2]]function
N1, N2: Does not necessarily exist, the general representative of "Select the line of action", for example, if my action is required between 10 to 20 lines, then "10,20[Action behavior]"

Function:
A: New, a can be followed by a string, and these strings will appear on the new line (the next line) ~
C: Replaced, C can be followed by strings, these strings can replace the line between n1,n2.
d: Delete, because it is deleted ah, so d usually do not pick up any of the following,
I: Insert, I can be followed by strings, and these strings will appear in a new line (currently the previous line),
p: Print, that is, a selection of data printed. Normally p will run with the parameter Sed-n ~
s: Instead, it can be directly substituted for the work mile. Usually this s action can be paired with the formal notation. For example, 1,20s/old/new/g is.

Add/Remove by unit of action


List the contents of the/etc/passwd and print the line number, and delete line 2nd to 5th.

[Root@www ~]# nl/etc/passwd | Sed ' 2,5d '
1 root:x:0:0:root:/root:/bin/bash
6 sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x:6:0: Shutdown:/sbin:/sbin/shutdown .....
(Omitted later) ...


The SED action is ' 2,5d ', and that D is the deletion. Because 2-5 lines to him deleted, so the data show no 2-5 line Luo ~ In addition, note that the original should be issued sed-e just right, no-e also line. Also note that the actions followed by SED must be enclosed in ' two single quotes.

Just delete line 2nd

To delete the 3rd to the last line

After the second line (that is, on the third line) plus "drink tea?" Words.

[Root@www ~]# nl/etc/passwd | Sed ' 2a drink tea '
1 root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
drink Tea
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin .....
(Omitted later) ...

So if it's going to be in 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?"

[Root@www ~]# nl/etc/passwd | Sed ' 2a Drink tea or ... \
> Drink beer? '
1 Root:x:0:0:root:/root:/bin/bash
2 bin:x:1:1:bin:/bin:/sbin/nologin
Drink tea or ...
Drink beer?
3 Daemon:x:2:2:daemon:/sbin:/sbin/nologin ...
(Omitted later) ...

You must add a new line with a backslash "\" between each line. So, in the example above, we can see that there is a \ presence at the end of the first line.
by the substitution and display of behavioral units


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

[Root@www ~]# nl/etc/passwd | Sed ' 2,5c no 2-5 number '
1 root:x:0:0:root:/root:/bin/bash
no 2-5 number
6 sync:x:5:0:sync:/sbin:/bin/ Sync .....
(Omitted later) ...


In this way we are able to replace the entire row of data.

List only 第5-7 lines in/etc/passwd files

[Root@www ~]# nl/etc/passwd | Sed-n ' 5,7p '
5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
6 Sync:x:5:0:sync:/sbin:/bin/sync
7 shutdown:x : 6:0:shutdown:/sbin:/sbin/shutdown

You can choose to display some of the line numbers within a file by using the display function of the SED in the behavior unit. Search for data and display

Search for lines with the root keyword in/etc/passwd

nl/etc/passwd | Sed '/root/p '
1  root:x:0:0:root:/root:/bin/bash
1  root:x:0:0:root:/root:/bin/bash
2  Daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3  bin:x:2:2:bin:/bin:/bin/sh
4  sys:x:3:3:sys:/dev:/ Bin/sh
5  Sync:x:4:65534:sync:/bin:/bin/sync

If Root is found, a matching row is 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


Output the specified number of rows (output 2-5 rows of data)

Sed-n ' 2,5p ' file
Search for and delete data

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

nl/etc/passwd | Sed  '/root/d '
2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh
3  bin:x:2:2:bin:/bin:/bin/sh
.... #第一行的匹配root已经删除了 ignored below

Search for data and execute commands

After the row that matches the pattern eastern is found,

Search for/etc/passwd, locate the row for root, perform a set of commands in the following curly braces, separate each command with a semicolon, replace bash with Blueshell, and then export the line:

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

If 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 exit.

search for and replace data

In addition to the whole line of processing mode, SED can also use the behavior unit to search and replace part of the data. Basically, the SED search is similar to the alternative to VI. He's kind of like this:

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

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

[Root@www ~]#/sbin/ifconfig eth0
eth0 Link encap:ethernet hwaddr 00:90:cc:a6:34:84 inet addr:192.168.1.100
bcast:192.168.1.255 mask:255.255.255.0
inet6 addr:fe80::290:ccff:fea6:3484/64 scope:link up
BROADCAST RUNNING multicast mtu:1500 metric:1 ...
(Omitted below) ...


The IP of this machine is 192.168.1.100.

Remove the front portion of the IP

[root@www ~]#/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 section: 192.168.1.100 bcast:192.168.1.255 mask:255.255.255.0

Remove the part behind the IP

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

Multi-point editing

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

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

-e means multiple edits, the first edit command deletes data from the third line to the end of/etc/passwd, and the second command searches for bash to replace Blueshell.

directly modify the contents of the file (dangerous action)


Sed can directly modify the contents of a file without having to use a pipe command or data stream redirection. However, since this action will be directly modified to the original file, so please do not randomly take the system configuration to test. Let's use the downloaded Regular_express.txt file to test and see.

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

[Root@www ~]# sed-i ' s/\.$/\!/g ' regular_express.txt

Use sed to add "# This is a test" directly on Regular_express.txt last line

[Root@www ~]# sed-i ' $a # This is a test ' regular_express.txt

Since $ represents the last row, and the action of A is new, the file finally adds "# This is a test".

The "-i" option of SED can directly modify the contents of the file, which is very helpful. For example, if you have a 1 million-line file that you want to add some text to on line 100th, using Vim may go crazy. Because the file is too big. What do you do? just use sed. You don't even need to use VIM to revise and replace functions directly through 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

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.