Linux Shell script programming-sed command details

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 matching rows. 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 is.
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:/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:/root:/bin/bash2 bin: X: 1: 1: bin:/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:/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:/root:/bin/bash1 root: X: 0: 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 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!

 

Related Article

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.