"Linux" basic daily life sed command

Source: Internet
Author: User

The SED command uses script to process files

SED can process and edit text files according to script instructions.
SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program, etc.

Grammar:
sed [-hnv][-e<script>][-f<script file >][text file]

Parameter description:
-e<script> or--expression=<script> processes the input text file with the script specified in the options.
-f<script file > or--file=<script file > process the input text file with the script file specified in the options.
-h or--help display Help.
-N or--quiet or--silent only show results after script processing.
-V or--version displays version information.

Action Description:
A: New, a can be followed by a string, and these strings will appear in a new line (the current next line) ~
C: Replace, 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 boom;
I: Insert, I can be followed by the string, and these strings will appear on a new line (the current line);
P: Printing, that is, printing a selected data. Normally p will run with the 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!

Instance:
1. Add a row after line Fourth of the Testfile file and output the result to standard output
SED-E 4a\new testfile

A. First look at the contents of Testfile in the following (walkthrough comparison)
Cat Testfile
HELLO linux!
Linux is a free unix-type opterating system.
This is a Linux testfile!
Linux Test

B. After using the SED command, the output is as follows
SED-E 4a\new testfile
HELLO linux!
Linux is a free unix-type opterating system.
This is a Linux testfile!
Linux Test
New

2. Additions/deletions to behavioral units
List the contents of the/etc/passwd and print the line number, and at the same time, delete the 2nd to 5th line!
nl/etc/passwd | Sed ' 2,5d ' (d delete)

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
nl/etc/passwd | Sed ' 2d '

To delete 3rd to last row
nl/etc/passwd | Sed ' 2, $d '

Add "drink Tea" after the second line (i.e. the third line) Words!
nl/etc/passwd | Sed ' 2a drink tea '

So if it's going to be before the second line,
nl/etc/passwd | Sed ' 2i drink tea '

If you want to add more than two lines, add two lines after the second line, such as "Drink tea or ..." and "Drink beer?"
nl/etc/passwd | Sed ' 2a Drink tea or ... \ Drink beer '

Note: There must be a backslash "\" To add a new line between each line! So, in the example above, we can see that there is a \ presence on the last side of the first line.

3. Substitution and display of behavior units
Replace the contents of the 2–5 line as "No 2-5 number"?
nl/etc/passwd | Sed ' 2,5c No 2-5 number ' (c replacement)

List only 第5-7 lines within a/etc/passwd file
nl/etc/passwd | Sed-n ' 5,7p ' (p printing, typically used with-N)

It is possible to select certain line numbers within a file to display through the display function of the SED in the behavior unit.
nl/etc/passwd | Sed '/root/p ' (keyword search)
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.

4. Search for and delete data
Delete/etc/passwd all rows containing root, other rows output
nl/etc/passwd | Sed '/root/d ' (d delete)

5. Search for data and execute commands
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
nl/etc/passwd | Sed-n '/root/p ' | ' s/bash/blueshell/g ' (feasible)

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

6. 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
Sed ' s/to be substituted string/new string/g '

First look at the original information, using/sbin/ifconfig query IP
[Email protected] ~]#/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

The IP of this machine is 192.168.1.100.
Delete the previous part of IP
/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
/sbin/inconfig eth0 | grep ' inet addr ' | Sed ' s/^. addr://g ' | sed ' s/bcast. $//g '

7. 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/g '

Note:-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.

8. Directly modify the contents of the file (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!
Sed ' s/.*$/!/g ' regular_express.txt

Use sed to add "# is a test" directly to the last line of Regular_express.txt
Sed-i ' $a ' # is a Test "' Regular_express.txt (-I directly modify the file, this method can save with vim added)

Learn to organize http://www.runoob.com/linux/linux-command-manual.html

"Linux" basic daily life sed command

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.