Linux sed commands

Source: Internet
Author: User
Introduction

Sed is an online editor that processes a row of content at a time. During processing, the currently processed rows are stored in the temporary buffer, called the pattern space. Then, the sed command is used to process the content in the buffer, send the buffer content to the screen. Next, process the next row, and repeat until the end of the file. The file content is not changed unless you use the redirection storage output. Sed is mainly used to automatically edit one or more files, simplify repeated operations on files, and write conversion programs.

 

Sed Parameters
[Root @ www ~] # Sed [-nefr] [action] Option and parameter:-n: Quiet Mode. In general sed usage, all data from STDIN is usually listed on the terminal. However, if the-n parameter is added, only the row (or action) that has been specially processed by sed will be listed. -E: directly edit the sed action in the Command column mode.-f: Write the sed action in a file.-f filename can run the sed action in filename; -r: sed supports the syntax of extended regular notation. (The default is the basic regular expression syntax)-I: directly modify the content of the file to be read, rather than output to the terminal. Action Description: [n1 [, n2] functionn1, n2: does not necessarily exist. Generally, it indicates "number of rows selected for action". For example, if my actions need to be performed between 10 and 20 rows, then "10, 20 [Action Behavior]" function: a: new, and a can be followed by a string, these strings will appear in the new line (the current next line )~ C: replace. c can be followed by strings. These strings can replace rows between n1 and n2! D: delete, because it is delete, so d is usually not followed by any comment; I: insert, I can be followed by a string, these strings will appear in the new line (the previous line currently); p: print, or print the selected data. Generally, p runs with the sed-n parameter ~ S: replace, you can directly replace the work! Generally, this s action can be combined with regular notation! For example, 1, 20 s/old/new/g!

 

Add/delete in behavior Unit

List the content of/etc/passwd and print the row number ~ Delete 5 rows!

[Root @ www ~] # Nl/etc/passwd | sed '2, 5d '1 root: x: 0: 0: root:/bin/bash6 sync: x: 5: 0: sync:/sbin:/bin/sync7 shutdown: x: 6: 0: shutdown:/sbin/shutdown ..... (Omitted later ).....

The sed action is '2, 5d ', and the d is deleted! Because 2-5 rows are deleted, NO 2-5 rows are displayed ~ In addition, note that sed-e should have been issued, and it would have been okay if-e was not used! At the same time, it should be noted that the action following sed must be enclosed in two single quotation marks!

Delete only 2nd rows

nl /etc/passwd | sed '2d' 

 

Delete 3rd to the last row

 nl /etc/passwd | sed '3,$d' 

 

Add "drink tea?" to the second row (that is, to the third row ?』 Words!

[Root @ www ~] # Nl/etc/passwd | sed '2a drink tea '1 root: x: 0: 0: root:/bin/bash2 bin: x: 1: 1: bin:/sbin/nologindrink tea3 daemon: x: 2: 2: daemon:/sbin/nologin ..... (Omitted later ).....

 

If it is before the second row

 nl /etc/passwd | sed '2i drink tea' 

 

If you want to add more than two lines, add two lines after the second line, for example, "Drink tea or..." and "drink beer ?』

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

Each line must be added with a backslash! Therefore, in the above example, we can find that \ exists at the end of the first line.

Replace and display with behavior units

What if I replace the content in line 2-5 with "No 2-5 number?

[Root @ www ~] # 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 can replace the whole row of Data!

 

Only 5th-7 lines in the/etc/passwd file are listed

[root@www ~]# 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

You can use this sed display function to display certain row numbers in a file.

  Search and display data

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

Nl/etc/passwd | sed '/root/P' 1 root: x: 0: 0: root:/bin/bash1 root: x: 0: 0: 0: 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 sync: x: 4: 65534: sync:/bin: /bin/sync .... ignore below

If the root node finds the output, all rows are matched.

 

When-n is used, only the rows containing the template are printed.

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

 

Search and delete data

Delete all rows containing root in/etc/passwd and output other rows.

Nl/etc/passwd | sed '/root/d' 2 daemon: x: 1: 1: daemon:/usr/sbin:/bin/sh3 bin: x: 2: 2: bin:/bin/sh .... ignore below # matching root in the first line has been deleted

 

 

 

Search for and execute commands for Data

After finding the row that matches the eastern pattern,

Search for/etc/passwd, find the line corresponding to the root, execute a group of commands in the brackets, and separate each command with a semicolon. Here we replace bash with blueshell, and then output this line:

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

If you only Replace the first bash keyword of/etc/passwd with 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 to exit.

 

Search and replace Data

In addition to the full row processing mode, sed can also use behavior units to search and replace part of data. Basically, sed search and substitution are similar to vi! He is a bit like this:

Sed's/the string to be replaced/New String/G'

 

First, observe the original information and use/sbin/ifconfig to Query IP addresses.

[Root @ www ~] #/Sbin/ifconfig eth0eth0 Link encap: Ethernet HWaddr 00: 90: CC: A6: 34: 84 inet addr: 192.168.1.100 Bcast: 192.168.1.255 Mask: 255.255.0inet6 addr: fe80: 290: ccff: fea6: 3484/64 Scope: LinkUP broadcast running multicast mtu: 1500 Metric: 1 ..... (omitted below ).....

The ip address of the local machine is 192.168.1.100.

 

Delete the previous part of the IP address.

[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

Next, delete the subsequent parts, that is, 192.168.1.100 Bcast: 192.168.1.255 Mask: 255.255.255.0

Delete the part after the IP address

[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 to delete the data from the third row to the end of/etc/passwd and replace 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 Indicates multi-point editing. The first Editing Command deletes the data from the third row to the end of/etc/passwd, and the second command searches for bash with blueshell.

 

 

Directly modify the file content (dangerous action)

Sed can directly modify the file content without using pipeline commands or data stream redirection! However, this action will directly change to the original file, so please do not test it with system configuration! Let's test it by using the downloaded regular_express.txt file!

Use sed to change the end of each line in regular_express.txt!

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

 

Use sed to add "# This is a test" to the last line of regular_express.txt 』

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

As $ represents the last row, and a action is added, This file adds "# This is a test 』!

Sed's "-I" option can directly modify the file content, which is very helpful! For example, if you have a 1 million-line file and you want to add some text to the 100th-line file, using vim may go crazy! The file is too large! What should we do? Use sed! Using sed to directly modify/Replace the function, you do not even need to use vim to modify it!

 

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.