1、grep
Content filtering required grep - I ignore case - O content to match the display, that is not visible - V exclusions - the number of statistical matching line c - n-rows and displaying the matched line number - E to extend the search mode (corresponding egrep) - after the match line a displays, and displays the line num - W word by filtration, equivalent \ B grep -A STU 10 b.txt filtered STU, the display 10 after the diverted line
2, but
sed stream editor, additions and deletions to achieve the change to the file search options: - n-shield default output - I directly modify the source file - E can specify multiple processing actions - R & lt support extended expressions {} may be a combination of a plurality of commands, separated by; - F sed script using addressing operators: P print line d deleted row replacement string s 1 of the first processing line 1 , 3 to 1 to 3 row 1, + 3 behind the first row row 3 1 1 - 2 of 1, 3 , 5 , 7 ...... row 1,$ For a row to the last row processing / aa /, / bb / on a line to the b-line processing / AAA /, 9 of a line to line 9, when the first nine rows is not displayed OK 9 OK after matching Zha: Sed -n ' 20 is , 30P' b.txt print line 20-30 Sed -n ' 3P; 6P ' a.txt print lines 3 and 6 Sed -n ' 3, + 6P ' a.txt print 3 after 6 rows and rows Sed -n ' / ^ bin / P ' a.txt bin print rows beginning Sed -n ' P; n- ' a.txt print odd-numbered lines, n represents read the next row (interlaced) Sed - n- ' n-; P 'a.txt print even-numbered lines, n represents read the next row (interlaced) Sed -n ' 8, $ {n-; P} ' a.txt 8 print lines to the end of all the even rows Sed -n ' $ = ' a.txt number of lines of the print file Sed -n ' / a / = {; P} ' b.txt line number Sed - NL aaa.jpg printed characters invisible remove Sed ' 3,5d ' a.txt removes the first row 3-5 sed ' / xml / d ' a.txt delete all lines containing the xml sed ' / xml /! d ' a.txt delete the line does not contain the xml sed '/ ^ xml / d' A.txt delete lines beginning xml Sed ' $ D ' a.txt delete the last row 1 Sed ' / ^ $ / D ' a.txt remove all blank lines Sed ' / ^ $ / {n-; / ^ $ / } D ' a.txt delete duplicate blank lines, two consecutive blank lines retain only a Sed -R & lt ' / a | b / D ' a.txt delete a row or b Sed ' 2, 2D ~ ' a.txt deleted row 2 to row 2 of the multiple change replacement line c s string replacement Sed ' 2cxx ' m.txt line 2 replaced XX Sed ' 3,6cxx 'm.txt 6, line 3 through line 1 is replaced xx Sed -n ' 2cxx \ NYY ' Alternatively m.txt line 2 and line xx to yy (\ n == newline mandatory line) Sed -e ' 3cxx ' - E ' 6Cxx ' m.txt the third row and the sixth row replaced XX Sed ' S # a # & S # G ' a.txt all a are replaced as, & Representative lookup string (similar to {}) Sed ' S # a # #G ' a.txt delete all rows a Sed ' 4,7s / ^ / # / ' a.txt. 4 to 7, line numbers # plus Sed ' 4,7s / ^ // # ' a.txt. 4 to 7, line # number removed Sed 's / a / b / ig' A.txt all replaced by a line B (case insensitive) by inserting before the i-th row is inserted after a line Sed ' 2ixx ' m.txt line xx inserted before the row 2 Sed ' 3,6ixx ' m.txt at 3- inserted xx preceding each line 6 Sed ' 2axx ' m.txt inserted after the line xx 2 Sed ' / ^ yy / Axx ' m.txt inserted in the beginning of the line xx yy after () the reference to source data stored up reused when replacing the Sed -R & lt ' S # (^.) # a \. 1 # ' a.txt first line a and outputs the added Sed -R & lt ' S # (^.) (. * ) (. $) # \ # 1 'a.txt output \ 1 data Sed -R & lt ' S # (^.) (. *) (. $) # \. 3 \ 2 \ 1 # ' a.txt \ . 3 \ 2 data \ 1
ls * .bak | sed -r "s # (. ^ *) (. bak $) # mv & \ 1.txt # g" into all the .bak .txt sed -nr ' S # ([^:] +) (: . *:) (/.*$) # \ 3 \ 2 \ 1 # GP ' / etc / the passwd (swap 1 and 3) (: at the beginning of a string of 1 or more) (from: to: all characters string) (in / end of the string) & saved when replacing the source string Sed -n ' S & # # # 20 is GP ' a.txt alternative text = & reserved source character) Sed ' S / the root / & ADMIN / ' A.txt replace the root rootadmin Sed' S / [0-9] / (&) / G ' a.txt digital () enclosed Sed -i.bak ' S & S # A # G # ' a.txt modify the source file and backup {} command key combination Sed -n ' 3 = {; P} ' b.txt output line 3 and the line number ( 3 = ; 3P) ! Inverted Sed ' l, 3! D ' a.txt not remove line 1-3 Sed ' / a /! D ' a.txt not delete a row Sed ' . 1 ~ 2D ' a.txt deleted odd rows (the pattern space enough 2 row, delete row 1) Sed ' 1 ~ 2! D ' a.txt deleted even rows n the current row is read to the next line of pattern space Sed -n ' P; n ' a.txt print odd-numbered lines, n represents read the next row (interlaced) Sed -n ' n; P ' a.txt print even-numbered lines, n represents read the next row (interlaced) file operations r read files --r operation should be combined - I only option is stored, or output only save to file w - w so as to cover the operation as a new file Sed -i ' R & lt a.txt ' b.txt put into a b (b.txt be present) Sed -i ' 2R a.txt ' b.txt put into a 2 b of the lower row Sed ' W C.txt ' Save as the b.txt b c (c do not exist) Sed '/ ^ x / W C.txt ' b.txt the line b is stored at the beginning of the x C -H pattern space holding space ---- --- added - copy -h pattern space covered --- --- - holding space - copy -G additional holding space ---- --- space mode - shear -g holding space covering ---- --- space mode - shear Sed ' IH; 2H; $ G ' b.txt rows 2 to copy the file to the front end of the document Sed ' IH; 1, 2D;; 2H $ G ' b.txt cut the front two rows of the file to the end of the file