The use of SED under Linux + practice __linux

Source: Internet
Author: User

Sed is a non-interactive editor that is not destructive and does not modify files unless you use redirects to save the output, and the default
case, all output lines are printed on the screen.
Sed saves the row currently being processed in a temporary buffer called the pattern space or the temporary buffer. SED processing
After the line in the mode space, the line is sent to the screen. Remove it from the mode space after each row is processed, and then the next
Rows are read into space, processed and displayed. SED has a temporary buffer in each row and edits the copy, so it doesn't
Modify or destroy the original file.
Below are the commands and options for SED.
A\ add one or more rows after the current line
C\ (replaces) text in the current line with new text
D Delete Line
I\ inserts text before the current line
H copies the contents of the pattern space to the staging buffer
H Append the contents of the pattern space to the staging buffer
G Take out the contents of the staging buffer, copy it to the mode space, and overwrite the original contents of the register.
G Remove the contents of the staging buffer, copy it to the mode space, append to the original content
L List nonprinting characters
P Print Line
N reads the next input line and starts processing it from the next command rather than the first command
Q End or exit sed
R reading input rows from a file
! Apply a command from all rows except the selected line
S replaces another with a string
Replace flag
G Global substitution within a row
P Print Line
W writes rows to file
The contents of the X-Exchange staging buffer and mode space
Y converts a character to another character

Options
-E allows multiple edits
-f Specifies the SED script file name
-N Cancels default output
If you need to use more than one command, or if you need to embed an address within an address range, you must enclose the command in curly braces, each row
Write only one command, or separate multiple commands in the same row with semicolons.

The character immediately following the S command is the separator between the lookup string and the replacement string. Delimited Fummer think forward slashes, but can be changed. No matter
What characters (except for line breaks, backslashes), as long as the S command is followed by a new string separator. This method in the lookup contains the positive
The pattern of a slash is useful, such as finding a path name or a birthday.

Meta characters supported by SED
^ Line Start locator/^love/matches all love-Beginning lines
$ end-of-line Locator/love$/matches all lines ending in love
. Match except line breaks
The single character/L.. e/match contains L, followed by two arbitrary characters, and then
The line with the letter E
* Match 0 or more front characters/*love/match in 0 or more spaces followed by pattern love line
[] matches the specified character group in the
Any of the characters [ll]ove/matches lines that contain love and love
[^] matches no longer specify character groups
/[^a-km-z]ove/matches any character that contains ove but precedes the characters not in A to K or
Rows between M and Z
\(.. \) Save the matched character s/\ (love\) able/\1er/loveable replaced with lover
& Save the search string to
Replace a reference in a string s/love/**&**/love becomes **love**.
\< the first locator/\<love/matches a line that contains a word that begins with love
\> final Locator/love\>/matches a line that contains a word ending with love
x\{m\} continuous M x/o\{5\}/
X\{m,\} at least m a x/o\{5,\}/
x\{m,n\} at least m, but not more than n/o\{5,10\}/


Practice Review
Sed-n '/sentimental/p ' Filex print all sentimental-containing lines on the screen in the file Filex
Filex content will not be changed if the-N option is not available, all contains
Sentimental's line will be printed two times
Sed ' 1,3d ' filex > Newfilex deletes the first 3 lines of the file Filex and saves the modified results in the Newfilex file
Sed '/[dd]aniel/d ' Filex delete the line containing Daniel or Daniel
Sed-n ' 15,20p ' Filex only print line 15th to 20th
Sed ' 1,10s/montana/mt/g ' Filex replaces all Montana of 1~10 rows with MT (if no G replacement option
will only replace the first Montana in each row)
Sed '/march/!d ' filex (SH)
Sed '/march/\!d ' Filex (CSH) deletes all rows that do not contain March (only in CSH to be escaped with backslashes)
Sed '/REPORT/S/5/8 ' Filex the first 5 in the row containing the show to 8.
Sed ' s/...$//' Filex deletes the following 3 characters per line
Sed ' s/...//' Filex deletes the first 3 characters per line
The sed '/east/,/west/s/north/south/' Filex replaces the north of all rows from east to west with South
Sed-n '/time off/w timefile ' Filex writes all rows containing time off to the Timefile file
Sed ' s/\ ([oo]ccur\) ence/\1rence/' file replaces all occurence with occurence,occurence
Sed-n ' | ' Filex print all \nn display nonprinting characters, TAB key (tab) is displayed as a > line

[Root@linux ~]# sed [-NEFR] [action]

Practice

Example one: List the contents of the/etc/passwd, and I need to print line numbers, and delete line 2nd to 5th.
[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:/sbin/shutdown
..... (Omitted later) ...
# See it. Because 2-5 lines to him deleted, so the information displayed, there is no 2-5 lines of cotton ~
# In addition, note that the original should be issued SED-E, no-e also line.
# also note that the actions followed by SED must be enclosed in ' two single quotes.
# and, if you just delete line 2nd, you can use NL/ETC/PASSWD | Sed ' 2d ' to reach,
# As for the 3rd to the last line, it's NL/ETC/PASSWD | Sed ' 3, $d '.

Example two: On the title, after the second line (that is, in the third line) plus "drink tea?" 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:/sbin/nologin
Hey The string added after a will appear in the second line behind the cotton. What if it's going to be before the second line?
# NL/ETC/PASSWD | Sed ' 2i drink tea ' is right.

Example three: Add two lines after the second line, such as "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:/sbin/nologin
# The point of this paradigm is that we can add more than one line. can add several lines ~
# but each line has to be a backslash/to increase the number of new rows. So, in the example above,
# we can see that there is/exists at the end of the first line. That's for sure.

Example four: I want to replace the contents of the 2–5 line as "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 2-5 lines, hey hey. The information we need appears.

Example five: List only 第5-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:/sbin/shutdown
# Why do you want to add-n parameters? You can tell sed ' 5,7p ' on your own. (5-7 rows Repeat output)
# There is no parameter with-N, the output of the data is much worse oh.

Example SIX: We can use ifconfig to list IP, if only eth0 IP.
[root@linux ~]# ifconfig eth0
eth0 Link encap:ethernet hwaddr 00:51:fd:52:9a:ca
inet addr:192.168.1.12: 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, all we need is the inet addr:. That line, so cotton, using grep and sed to catch the
[root@linux ~]# ifconfig eth0 | grep ' inet "|sed ' s/^.*addr://g ' |sed ' ' s/bcast.*$//g '
[ro Ot@linux ~]# ifconfig eth0 | grep ' inet ' |sed ' s/^.*addr://g ' |sed ' s/bcast.*$//g '
#这两行命令差不多, but the second line has a space before the BACST, and be sure to note that the spaces are removed and can be used
The above two lines are redirected to two text files, such as 1.txt 2.txt. Carefully view the size of these two files
# you can separate the process of each pipeline (|) To see why Cotton is used.
# After going to the end of the head, we will get the IP that we need, which is 192.168.1.12 cotton ~

Example seven: Will/etc/man.config the contents of the file, there is a man's set out, but do not explain the content.
[Root@linux ~]# Cat/etc/man.config | grep ' Man ' | Sed ' s/#.*$//g ' | Sed '/^$/d '
# in each row, if # denotes the behavior annotation, it should be noted that sometimes,
# The annotation is not written in the first character, which is written in the rear of an instruction, as in the following shape:
# "Shutdown-h Now, this is the shutdown instruction", note # is in the rear of the instruction.
# Therefore, we will use the formal notation that will #.*$ this.

Example eight: Use sed to add "# This is a test" directly on ~/.BASHRC last line
[Root@linux ~]# sed-i ' $a # This is a test ' ~/.BASHRC
# The above-i parameter allows your sed to directly modify the contents of the file that follows. Rather than by the screen output.
# As for the $a, it means the last line.

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.