Little Q: There is a kind of forbearance is the greatest strength, there is a silence is the most powerful confession.
=============================================================
A: Introduction
Sed is a stream editor, which is a very useful tool in text processing and can be used perfectly in conjunction with regular expressions. 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.
Two: Command format
sed [options] ' command ' file (s) #直接调用选项和命令
sed [options]-f filename File (s) #调用文件中的语句
Three: option [options]+ command
Common options:
- N: Use Quiet (silent) mode. Plus-N, only the line (or action) that has been specially processed by SED is listed.
- E: The Action of SED is edited directly on the instruction list mode;
- F: The action of SED is written directly in a file, and-f filename can perform the SED action within filename;
- R:The action of SED supports the syntax of extended formal notation. (Presupposition is the basic formal notation of French law)
-I : directly modify the contents of the read file instead of the screen output.
Common parameters: (+ not used)
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: print, that is, print out a selected material. Normally p will work with parameter Sed-n ~
s: Replace, can be directly replaced by work! Usually this s action can be paired with regular notation
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7D/40/wKioL1bjxoCCdg5pAABJwh2R5H8773.png "title=" Dfvdfbvdfbfb.png "alt=" Wkiol1bjxoccdg5paabjwh2r5h8773.png "/>
Four: Getting Started instancePrint Delete Replace lookup (n P d s g)
Sed-n ' 1p ' file #显示第一行
Sed-n ' $p ' file #显示最后一行
Sed-n ' 1,2p ' file #显示第一行到第二行
Sed-n ' 2, $p ' file #显示第二行到最后一行
Sed '/^$/' file #删除file文件的空白行
Sed ' 1d ' file #删除第一行
Sed ' $d ' file #删除最后一行
Sed ' 2, $d ' file #删除第二行到最后一行
Sed ' s/book/books/' file #替换单独的字符串
Sed-n ' s/test/test/p ' file #-n print only replaced rows
Sed ' s/book/books/g ' file #替换每一行中所有匹配
echo Sksksksksksk | Sed ' s/sk/sk/3g ' sksksksksksk #从第三处开始替换
Sed-n '/ruby/p ' file #查询包括关键字ruby所在所有行
Sed-n '/\$/p ' file #查询包括关键字 $ where all rows, \ non-righteous characters
Of course, these command operations just want to print out, will not really change the contents of the file, want to really change the content, need to add-I option
Five: An explanation of the order
Multi-point editing: E command
The-e option allows multiple commands to be executed on the same line:
Sed-e ' 1,5d '-e ' s/test/check/' file
#上面sed表达式的第一条命令删除1至5行, the second command replaces test with a check. The order in which the commands are executed has an effect on the results. If all two commands are replacement commands, the first substitution command affects the result of the second replacement command.
And-e equivalent commands are--expression:
Sed--expression= ' s/test/check/'--expression= '/love/d ' file
read from File: R command
The contents of file are read in and displayed after the line matching the test, and if multiple rows are matched, the contents of file are displayed below all matching rows:
Sed '/test/r file ' filename
Write file: w command
All rows containing test in example are written to file:
Sed-n '/test/w file ' example
Append (line): A\ command
Append this was a test line to the row that begins with test:
Sed '/^test/a\this is a test line ' file in test.conf
After the 2nd line of the file, insert this is a test lines:
Sed-i ' 2a\this is a test line ' test.conf
insert (on line): I\ command
Append this was a test line to the front of the row that begins with test:
Sed '/^test/i\this is a test line ' file
Before the 5th line of the test.conf file, insert this is a test lines:
Sed-i ' 5i\this is a test line ' test.conf
Next: N Command
If test is matched, move to the next line of the matching line, replace the AA for this line, change to BB, and print the line, and then continue:
Sed '/test/{n; s/aa/bb/;} ' file
deform: Y command
Turn all ABCDE in 1~10 into uppercase, note that regular expression metacharacters cannot use this command:
Sed ' 1,10y/abcde/abcde/' file
exit: Q command
After you finish printing line 10th, exit SED
Sed ' 10q ' file
Hold and get: H command and G command
When the SED processes the file, each row is stored in a temporary buffer called the pattern space, unless the row is deleted or the output is canceled, otherwise all processed rows will be printed on the screen. The pattern space is then emptied and a new line is stored for processing.
Sed-e '/test/h '-e ' $G ' file
In this example, when a row matching test is found, it is stored in the pattern space, and the H command copies it into a special buffer called the hold cache. The second statement means that when the last line is reached, the G command takes out the row holding the buffer, then puts it back into the pattern space and appends it to the end of the line that already exists in the pattern space. In this example, it is appended to the last line. Simply put, any line containing test is copied and appended to the end of the file.
Hold and Swap: H command and x command
Swaps the pattern space and preserves the contents of the buffer. That is, the line that contains test and check is swapped:
Sed-e '/test/h '-e '/check/x ' file
The script scriptfile sed script is a list of SED commands that boot the script file name with the-F option when starting sed. Sed is very picky about the commands entered in the script, and cannot have any whitespace or text at the end of the command, separated by semicolons if there are multiple commands in a row. The behavior that begins with # comments lines, and cannot span rows.
sed [options]-F scriptfile file (s)
Print odd or even rows
Method 1:
Sed-n ' p;n ' test.txt #奇数行
Sed-n ' n;p ' test.txt #偶数行
Method 2:
Sed-n ' 1~2p ' test.txt #奇数行
Sed-n ' 2~2p ' test.txt #偶数行
Print the next line matching a string
Grep-a 1 SCC urfile sed-n '/scc/{n;p} ' urfile awk '/scc/{getline; print} ' Urfile
Delimiter
The characters in the above command are used as delimiters in sed or any delimiter can be used: sed ' s:test:text:g ' sed ' s|test| Text|g '
When delimiters appear inside a style, they need to be escaped:
Sed ' s/\/bin/\/usr\/local\/bin/g '
Matched string Markers &
The regular expression \w\+ matches each word, using [&] to replace it,& corresponds to the previously matched word:
Echo this was a Test line | Sed ' s/\w\+/[&]/g '
[This] [IS] [A] [test] [line]
All lines starting with 192.168.0.1 will be replaced with a self-added localhost:
Sed ' s/^192.168.0.1/&localhost/' file
192.168.0.1localhost
SUBSTRING matching tag \1
Matches part of a given style:
echo this was digit 7 in a number | Sed ' s/digit \ ([0-9]\)/\1/'
This was 7 in a number
The command digit 7, which was replaced by 7. The substring to which the style matches is 7,\ (.. \) is used to match substrings, the first substring that matches to is marked as \1, and so on the second result that matches to \2,
Example: Echo AAA BBB | Sed ' s/\ ([a-z]\+\) \ ([a-z]\+\)/\2 \1/' BBB aaa Love
Marked as 1, all loveable will be replaced with lovers and printed out:
Sed-n ' s/\ (love\) able/\1rs/p ' file
Combining multiple expressions
Sed ' expression ' | Sed ' expression ' is equivalent to: sed ' expression; An expression '
A reference sed expression can be quoted using single quotation marks, but you need to use double quotes if the expression contains variable strings inside. Test=hello
echo Hello World | Sed "s/$test/hello" HELLO World
Range of selected rows: (comma) All lines in the range determined by the template test and check are printed:
Sed-n '/test/,/check/p ' file
Prints all lines starting from line 5th to the first one that contains the line starting with test:
Sed-n ' 5,/^test/p ' file
For the row between the template test and West, the end of each line is replaced with the string AAA BBB:
Sed '/test/,/west/s/$/aaa bbb/' file
----------------------------------------------------------------------------------------------------
Recommended reading: Http://blog.chinaunix.net/uid-25324849-id-3190054.html
http://blog.csdn.net/yiqingnian28/article/details/23133043
Stream Editing-sed