Shell Script (v)
4. Sed and awk
(1) SED
A, common parameters
#文件编辑工具, text files and standard input can be edited, the standard input is keyboard input,
#文件重定向, string, variable, and pipe text.
#-n: Do not print all rows to standard output;-e: Resolves the next string to the sed edit command
#-f: Calling sed script file
B. Positioning text
#x: Specify the line number; x, y: Specifies the range from X line to Y line;
#/pattern/: Query for rows that contain patterns;
#/pattern/pattern/: Query for rows containing two patterns;
#/pattern/,x: line from match pattern to row to X row
#x,/pattern/: Rows from x rows to matching rows with/pattern/
#x, y!: The query does not include rows with x and Y lines;
C, edit command
#p: print matching lines; =: Print the file line number; A\: Append text after locating the line;
#i \: Append text before locating the line;
#d: Delete the anchor row; C\: replace the positioned text with the new text; s: replace the pattern with the replacement mode
#r: Reads text from another file; W: Writes the file to a file; y: transforms the character;
#q: Exits after the first pattern is matched;
#1: Displays the equivalent control character of the octal ASCII code; {}: The command group executed on the anchor line;
#n: Reads the next input line and processes the new row with the next command
d. Examples
#打印第1行, and print/not print all lines of the file
Sed ' 1p ' a.txt
Sed-n ' 1p ' a.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/46/wKiom1V33VTRKyADAADqfpYLAag962.jpg "/>
#打印第2行到第4行
Sed-n ' 2,4p ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/42/wKioL1V33vniNyHeAAEN19zWcN8690.jpg "/>
#打印不在第2到第4行的行
Sed-n ' 2,4!p ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/46/wKiom1V33VTB7RgaAAEoB3ZE1q8215.jpg "/>
#定位行前1行追加文本
Sed '/name5/i\add_string ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/42/wKioL1V33vrSXR5oAADTsThS5Ms598.jpg "/>
#定位行后1行追加文本
Sed '/name5/a\add_string ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/46/wKiom1V33VXiPupYAACu9JMJjbY957.jpg "/>
#使用正则表达式需要使用s选项
#同1行定位字符串前面追加文本
Sed ' s/name5/addstring&/' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/42/wKioL1V33vvi_uWrAACkSx90f2Y614.jpg "/>
#同1行定位字符串后面追加文本
Sed ' s/name5/&addstring/' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/42/wKioL1V33vugZJrRAAChIsOeKVs817.jpg "/>
#修改定位文本
#将NAME5替换成rp_string
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/46/wKiom1V33VbyII_0AACDp3luL0I277.jpg "/>
#将NAME5定位行整行替换成rp_string
Sed '/name5/c\rp_string ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/42/wKioL1V33vzAe7iDAAB-yg0aoXE041.jpg "/>
#删除文本, delete line 6th, line 2nd to 5th, respectively
Sed ' 6d ' bkname.txt
Sed ' 2,5d ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/42/wKioL1V33vyhOuWbAADBCxhuWDs254.jpg "/>
#删除不在第2到第9行的行, it's actually showing lines 2nd through 9th.
Sed ' 2,9!d ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/46/wKiom1V33VfRepTmAACbVw4Mk0M151.jpg "/>
#参数p和g区别, P: The ball replaces only the 1th occurrence of matching text;
#g: Represents replacing all occurrences of the matched text
#sed是按行来处理的, so to delete the matching text in 1 rows more than once, you need to use G, notice the difference between the two
Sed-n ' s/name/*/p ' bkname.txt
Sed-n ' s/name/*/g ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/42/wKioL1V33vygtmLtAADMp-Oz4S0032.jpg "/>
#字符变换, each of which corresponds to a replacement
#a替换成E, b Replace with f,c Replace with g,d Replace with H
Sed ' y/abcd/efgh/' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/46/wKiom1V33VjD_l3ZAABdEXOG6_k671.jpg "/>
#元字符匹配, navigate to the last 1 rows
Sed-n ' $p ' bkname.txt
Sed-n ' $ ' p bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/46/wKiom1V33VjBspAtAABTHX0Fg8U363.jpg "/>
#定位以test结尾的行
Sed-n '/test$/p ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/42/wKioL1V33v3zIQm2AABPHrKKYc4624.jpg "/>
#定位行到指定行, if you specify a row value > position row value, it will print
#指定行到最后1行, such as the specified row 8>4 (name4 row value)
Sed-n ' 3,/name4/p ' bkname.txt
Sed-n ' 8,/name4/p ' bkname.txt
#等价于
Sed-n ' 8, $p ' Bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/42/wKioL1V33v7AedRVAAEOf7h07YE981.jpg "/>
#定位行到指定行, if you specify a row value > anchor row value, only the anchor row is printed
Sed-n '/name4/,6p ' bkname.txt
Sed-n '/name4/,2p ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/46/wKiom1V33VmDhZxQAACbUSf1xMs763.jpg "/>
#"!" Exclamation mark: Indicates non, shows rows not in line 2nd to 9th
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/42/wKioL1V33v7y0sGoAABxxat2olg910.jpg "/>
#=: Print line numbers only
Sed-n '/name4/= ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/46/wKiom1V33VnhKL3rAAB6XXXUQKc687.jpg "/>
#l (lowercase L): Display control
Sed-n ' 1, $l ' Bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/46/wKiom1V33VnTMQ3FAADXy3UPRyE639.jpg "/>
#-e: Using multiple Edit commands
Sed-n '/name4/{p;=} ' bkname.txt
Sed-n-E '/name4/p '-e '/name4/= ' bkname.txt650) this.width=650; "Src=" http://s3.51cto.com/wyfs02/M02/6E/42/ Wkiol1v33v6jn9khaacrbx76xkc695.jpg "/>
#n: Handles the next row of the anchor row, replacing "00" with "*" for the next 1 rows of the anchor row
Sed '/name1/{n;s/00/*/g;} ' Bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/46/wKiom1V33VniibCXAADSHdr9f6M513.jpg "/>
#打印定位行的下一行
Sed-n '/name2/{n;p} ' bkname.txt
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6E/42/wKioL1V33v6TSAzJAABSoPwGKDE335.jpg "/>
# To execute a command group on the anchor row, note that the anchor row operation
Sed '/name1/{s/100/*/g;s/name/ttt/g;} ' Bkname.txt
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6E/43/wKioL1V34cDBnX9SAAC39XNVfpE680.jpg "title=" Shell.png "alt=" Wkiol1v34cdbnx9saac39xnvfpe680.jpg "/>
This article is from the "Love On Action" blog, please be sure to keep this source http://1055745601.blog.51cto.com/5003160/1660411
Shell Script (v)