Linux sed command

Source: Internet
Author: User

[Root @ Linux ~] # Sed [-nefr] [action]
Parameters:
-N: Use silent mode. In general SED
All

Will be 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 be executed.
In filename

Sed action;
-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 by the screen.

Action Description:
[N1 [, N2] Function
N1, N2: does not necessarily exist. Generally, it indicates "select the number of rows 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:
A: add,
Can be followed by strings, and these strings will appear in the new line (current next line )~
C: replace. C can be followed by strings.
Line between N1 and N2!
D: delete, because it is deleted, so d
Generally, no things are followed;
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 corresponds to the parameter
Sed-N works together ~
S: replace, you can directly replace the work! Usually this s
Can be used together

Regular representation! 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 lines 2-5 have been deleted from him, there is no 2-5 in the information displayed.
Cotton ~
# 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: Add "drink tea?" To the question behind the second line (that is, to the third line ?』 Words!
[Root @ Linux ~] # 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/nologin
# Hey hey! In
The string added after the second line will appear behind the cotton! 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 '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/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 there is/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, 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
# No rows 2-5, hey! The materials we need will show up!

Example 5: list only 5th-7 rows
[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' by yourself'
That's it! (Repeated output in rows 5-7)
# When the-n parameter is added, the output information 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 ).....
# In fact, we only need the inet ADDR:... line, so cotton, using grep and
Sed to catch

[Root @ Linux ~] # Ifconfig eth0 | grep 'inet '| SED
'S/^. * ADDR: // G' | SED's/bcast. * $ // G'
[Root @ Linux ~] # Ifconfig eth0 | grep 'inet '| SED
'S/^. * ADDR: // G' | SED's/bcast. * $ // G'
# These two lines of commands are similar, but the second line has a space before bacst. Note that even spaces are excluded. You can use
The two commands are directed to the two hosts files, for example, 1.txt 2.txt.
# You can run the process of each pipeline (|) separately to find out the cause of Cotton!
# After removing the head and tail, we will get the IP address we need, that is, 192.168.1.12 cotton ~

Example 7: extract the content in the/etc/man. config file with man settings, 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,
# The 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", note # is behind the command.
# Therefore, we will use the regular notation #. * $!

Example 8: Use sed directly in ~ /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 upper header allows your sed
Directly modify the following file content! Instead of being output by the screen.
# The $ A indicates that the last row is added.

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.