Sed syntax
Author: laruence
Parameters:
-N: silent mode. In general sed usage, all data from stdin
The data is usually listed on the screen. However, if the-n parameter is added
The row (or action) specially processed by SED will be listed.
-E: directly edit the SED action in the Command column mode;
-F: Write the SED action directly in a file.-f filename can execute
Sed action;
-R: sed supports the syntax of extended regular expressions. (The default is the basic regular expression syntax)
-I: directly modify the read file content, rather than the screen output.
Action Description: [N1 [, N2] Function
N1, N2: does not necessarily exist. It generally indicates "number of rows selected for the action". For example, if my action
If it is between 10 and 20 rows, then "10, 20 [Action Behavior]"
Function has the following functions:
A: new. A can be followed by strings. These strings will appear in the new row (the next row currently )~
C: replace. C can be followed by strings. These strings can replace rows between N1 and N2!
D: delete. Because it is deleted, D is usually followed by no comment;
I: insert, I can be followed by strings, and these strings will appear in the new line (the previous line currently );
P: print the selected data. Usually P will work with the sed-N parameter ~
S: replace, you can directly replace the work! Generally, this s action can be used together.
Regular Expression! For example, 1, 20 s/old/new/g!
Example:
Example 1: List the content of/etc/passwd and print the row number ~ Delete 5 rows!
[Root @ Linux ~] # 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/Shutdown
... (Omitted later ).....
# Have you seen it? Because 2-5 rows are deleted from the table, NO 2-5 rows are displayed ~
# In addition, note that sed-E should have been issued, and no-E is required!
# At the same time, note that the action followed by sed must be enclosed in two single quotes!
# If you only need to delete 2nd rows, you can use NL/etc/passwd | sed '2d,
# For the last line from 3rd to, NL/etc/passwd | sed '3, $ d!
Example 2: Answer the question,Add "drink tea?" to the second row (that is, to the third row ?』 Words!
[Root @ Linux ~] # NL/etc/passwd | sed '2ADrink 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/nologin
# Hey hey! The string added after line A will already appear after line 2! What if it is before the second line?
# NL/etc/passwd | sed '2i drink tea 'is right!
Example 3: Add two lines after the second line, for example, "Drink tea or..." Drink beer ?』
[Root @ Linux ~] # NL/etc/passwd | sed '2ADrink 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/nologin
# The focus of this example is that we can add more than one line! Several lines can be added ~
# However, each row must be added with a backslash! Therefore, in the above example,
# We can find that \ exists at the end of the first line! That must be done!
Example 4: I want to replace the content in line 2-5 with "NO 2-5 number?
[Root @ Linux ~] # NL/etc/passwd | sed '2, 5CNo 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
# No rows 2-5, hey! The data we need will show up!
Example 5: only 5th-7 rows are listed.
[Root @ Linux ~] # 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/Shutdown
# Why do we need to add the-n parameter? You can issue sed '5, 7p' on your own! (Repeated output in rows 5-7)
# When the-n parameter is added, the output data is much worse!
Example 6: We can use ifconfig to list IP addresses?
[Root @ Linux ~] # Ifconfig eth0
Eth0 link encap: Ethernet hwaddr 00: 51: FD: 52: 9A: CA
Inet ADDR: 192.168.1.12 bcast: 192.168.1.255 mask: 255.255.255.0
Inet6 ADDR: fe80: 250: fcff: fe22: 9acb/64 scope: Link
Up broadcast running Multicast MTU: 1500 Metric: 1
... (Omitted below ).....
# Actually, all we need is the inet ADDR:... line. So, use grep and sed to catch it.
[Root @ Oracle ~] # Ifconfig eth0 | grep 'inet '| SED's/^. * ADDR: // G' | SED's/bcast. * $ // G'
192.168.0.189
# You can run the process of each pipeline (|) separately to find out the cause!
# After we start and end, we will get the IP address we need, that is, 192.168.1.12 ~
Sed S is used for replacement.
Example 7: extract the man settings in the/etc/man. config file, but do not describe the content.
[Root @ Linux ~] # Cat/etc/man. config | grep 'man '| SED's/#. * $ // G' | \
> SED '/^ $/d'
# If there is # In each row, it indicates the behavior annotation, but note that sometimes,
# Annotation is not written in the first character, that is, after a command, as shown below:
# "Shutdown-H now # This is the command to shut down", and the comment # is behind the command.
# Therefore, we will use the regular expression #. * $!
Example 8: Use sed to directly ~ /Add "# This is a test" to the last line of bashrc 』
[Root @ Linux ~] # Sed-I '$ A # This is a test '~ /. Bashrc
# The-I parameter in the header allows your sed to directly modify the file content next to it! Instead of screen output.
# The $ A indicates that the last row is added.