The content of this article is more concise, suitable for Linux a bit basic novice ...
After the introduction of the function is easy to understand, I hope that we can play a role in helping
Sed is an online editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, to simplify the repeated operation of the file, to write the conversion program and so on .
1, num2 num1 line to Span style= "font-family: ' Times New Roman ';" >num2 num
EG< Span style= "font-family: ' The song Body '; >: [[email protected] mnt]# Sed ' 1, 3d ' test to 3 Span style= "font-family: ' The song Body '; > line
, word1word2 : From first match to character word1 to first match to character word2 all rows between
, : from m after the beginning of the line. n line
EG< Span style= "font-family: ' The song Body '; >: [[email protected] mnt]# Sed ' 1 +3d ' test line and back 3 line
4. num a \word : add character word after num lines
5. num i \word : add character before num line word
EG:
[[Email protected] mnt]# sed ' 2a \hello ' test
Hello, like
Hello
Hello, Love
[[Email protected] mnt]# sed ' 2i \hello ' test
Hello
Hello, like
Hello, Love
If you want to add another line:
[[email protected] mnt]# sed ' 2a \hello\n\world ' test (\ n is a line break)
Hello, like
Hello
World
Hello, Love
6. r file: Add content from file after the specified line
EG: sed ' 2r/etc/passwd ' fstab # # Add the contents of the /etc/passwd file after the second line
7. W file: Save the contents of the specified range to file
EG:
[Email protected] mnt]# sed-n ' 1,3w/mnt/test1 '/etc/passwd
[email protected] mnt]# cat Test1
Root:x:0:0:root:/root:/bin/bash
Bin:x:1:1:bin:/bin:/sbin/nologin
Daemon:x:2:2:daemon:/sbin:/sbin/nologin
8. & * * *: After thematch to the content plus * * *
9. l***: Subtract * * * (lowercase l) after matched content
EG:
[email protected] mnt]# cat test
Hello, like
Hello, Love
[Email protected] mnt]# sed ' s/l. e/&r/' Test
Hello, Liker.
Hello, lover.
[[email protected] mnt]# sed ' s/l. r/lr/' Test
Hello, like
Hello, Love
Sed feature options:
-N: Display only content that is matched to, not in the display mode space
-I: change to source file
sed+ The extension of the regular expression:
# # Test file is tested
1.Delete the blank line of the file: sed '/^$/d ' test
2. Delete the blank characters from the beginning of the file: sed ' s/^[[:space:]]//' test
3. Delete the #at thebeginning of the file , but the following must be blank: sed ' s/^#[[:space:]]//' test
This article is from the "11944248" blog, please be sure to keep this source http://11954248.blog.51cto.com/11944248/1963794
Basic usage of <linux small white advanced > SED instruction