Sed

xiaoxiao2021-03-05  22

SED usage introduction (Hasseeded network) S e D is a non-interactive text stream editor. It edits the text copy of the file or standard input.

Quote: • Extract the domain. • Match regular expressions. • The comparison domain. • Increase, additional, replace. • Basic S e D commands and a line script. You can enter the S e d command on the command line, or you can write the command in a file, then call S e d, which is basically the same as A Wk. An important fact that uses S e d that needs to be remembered whether the command is, S E D does not deal with the initialization file, which is just a copy, and then all changes will be output to the screen if not redirected to a file. Because S e D is a non-interactive editor, you must specify the text row to change by the line number or regular expression. This article describes S e d usage and function. Most of this chapter write a line of orders and small scripts. Doing so can slowly deepen the understanding of S E D usage, and obtain valuable experience in order to eventually compose large complex S E D scripts. Like G R e P, like A W K, S e d is an important text filtering tool, or uses a line of commands or uses pipes to combine with G R E P. 1 Sed How to read data SED read data from a file of the file or read data from several formats of the standard, copy it to a first command to edit the buffer, read the command line or script, and use these Command lookup mode or positioning line number edit it. Repeat this process until the command ends. 2 Call SED Call S e D There are three ways: in the command line type command; insert the S e d command into the script file, then call S e d; insert the S e d command into the script file and make the S e d script can be executed. Use the S e D command line format:

Code: SED [Option] S e d Command Enter file. Remember when the command line uses the s e d command, the actual command should add single quotes. S e D also allows multiple quotes. Use the S e d script file, format:

Code: SED [Option] -f SED Script File Enter file To use the first row with the S e D script file with the S E D command interpreter, its format is:

Code: S e d Script File [Option] Enter the file whether you use the S H E L command line mode or script file mode, if no input file is specified, S e d accepts input from standard input, usually a keyboard or redirect result.

Quote: The S e d option is as follows: n is not printed; S e D does not write editing to standard output, default is to print all rows (editors and not edited). The P command can be used to print editing rows. c The next command is the editorial command. Add this option when using multiple edits. If only one S e D command is used, this option is useless, but it doesn't matter if it specifies it. f If the S e d script file is being called, use this option. This option notifies that a script file supports all S e d commands, such as: Sed -f myscript.sed input_file, where M Y S C R I P T. S E D is file that supports S e D command. 2.1 Save the SED Output Since the initialization file is not contacted, if you want to save the change content, you can simply redirect all output to a file. The following example redirects all output to file 'M y o U t f i l e' for all outputs of the S e d command, using this method when the results are satisfactory.

Code: $ SED 'SOME-SED-COMMANDS' INPUT-FILE> Myoutfile2.2 Using SED Search in the file SED NED Books Enter files, the default starts from the first line, there are two ways to locate text:

Quote: 1) Use the line number, which can be a simple number, or a line number range. 2) Use Regular Expression The following is some of the ways to use S e D positioning text. Code: X X is a row, such as 1 x, y indicates that the line number ranges from x to y, such as 2, 5 represents rows from line 2 to Row 5 / P T t e R n / Query contains the pattern. For example / d i s k / or / [a-z] / / p A t t e R n / p A t t e R n / query contains two modes. For example / d i s k / d i s k s / p A t t e R n /, x queries a row containing a mode on a given line number. Such as / R I b O n /, 3 x, / p A t t e R n / through the line number and mode query matching line. 3. / V d U / X, Y! The query does not contain rows of the specified line number x and y. 1, 2! 2.3 Basic SED Edit Commands

Code: SED Editing Command P Print Matching Right = Display File No. A / Additional New Text Information I / After the Line Routing Route After Plugging New Text Information D Delete Location Row C / Novel Text Replace Location Text S Replacing the corresponding mode R ROP from another file W Write text to a file Q The first mode match is completed after the completion or immediately launched the control character {} in the target character {} execute in the positioning line Group N reads the text from another file and attached to the next row G to paste the mode 2 to / pattern N / Y transfer character n Continuation to the next input line; allowing cross-line mode matching statement SED and regular expression SED identification Any basic regular expression and mode and its row matching rules. Remember that one of the rules is: If you want to locate a special character, you must use the following text file Q u t e. T x t using the following text file Q u.

Code: [sam @ Linux_chenwy sam] $ cat quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23:00 The local nurse Miss... P.neave Was in Attendance.1 Use P (RINT) to display the line only in the second line, with -n

Code: [SAM @ Linux_chenwy SAM] $ SED-N '2P' Quote.txt It Was An Evening Of SPLENDID MUSIC AND Company.2 Printing Scope You can specify the range of rows, and you can print 1 to 3 lines, separated by a comma.

Code:.. [Sam @ Linux_chenwy sam] $ sed -n '1,3p' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23 : 00.3 Print mode Assume that you want to match the word N EAVE, and print this line, the method is as follows. Using mode / P A t t e R n / format, here / n e a v e /.

Code: [SAM @ Linux_chenwy SAM] $ SED -N '/ THE / P' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. The local nurse miss P.neave Was in Attendance.4 Mode and line number The query can be used in conjunction with the line number and mode. Assuming to change the word quote. TXT in the last line of words THE, use the SED query THE, return two lines: Code: [SAM @ Linux_chenwy SAM] $ SED -N '/ The / P' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. The local nurse Miss P.neave Was in Attendance. Using the mode with the line number can eliminate the first line, the format is line _ number, / pattern /. Commerity is used to separate the line number and mode start part. To achieve the expected result, use 4, / t h e /. It means that only in the fourth line query mode T h e, the command is as follows:

Code: [SAM @ Linux_chenwy SAM] $ SED -N '4, / The Local Nurse Miss P.neave Was in Attendance. It is failed to play the fourth line. This model should be found in the specified row, and start finding from the next line until it is found, and the print is printed before the line is found. If the specified line itself is compliant, it is necessary to use a backslash / shield a special meaning before matching all prints of the Bank and the rows of the row. The mode is // $ / p.

Code: [SAM @ Linux_chenwy SAM] $ SED -N '/ / $ / P' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90.6 Show the entire file to print the entire file, just set the row range to the first line To the last row, 1, $. $ 意 为 为 行 行.

Code: [sam @ Linux_chenwy sam] $ sed -n '1, $ p' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at.. 23:00. The Local Nurse Miss P.neave Was in Attendance.7 Matching any letters, followed by 0 times or multiple times of any letters, and ending with ING, mode is /. * Ing /. You can use this mode to query any word ending with I N g.

Code: [SAM @ Linux_chenwy SAM] $ SED -N '/.GHENW' Quote.txt It Was An Evening Of SPLENDID MUSIC AND Company.8 The first line is printed in the first line, using the line number:

Code: [SAM @ Linux_chenwy SAM] $ SED -N '1P' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90.9 Last line to print the last line, use $. $ Is a metamor of the last line. Code: [SAM @ Linux_chenwy SAM] $ SED -N '$ P' Quote.txt The Local Nurse Miss P.neave Was in Attendance.10 Print Board To print the line number, use the equal number =. The line number matched by the print mode, uses the format / p A t t e R n / =.

Code:. [Sam @ Linux_chenwy sam] $ sed -e '/ music / =' quote.txt The honeysuckle band played all night long for only $ 90 2 It was an evening of splendid music and company Too bad the disco floor fell through. At 23:00. The Local Nurse Miss P.neave Was in Attendance. The entire file is printed and the matched row prints the line number. If you only care about the actual line number, use the - e option.

Code: [SAM @ Linux_chenwy SAM] $ SED-N '/ Music / =' Quote.txt 2 If only the printed line number and match row, two S e d commands must be used and the E option is used. The first command print mode match line, the second use = option print line number, format is SED -N -E / PATTERN / P -E / PATTERN / =

Code: [SAM @ Linux_chenwy SAM] $ SED -N -E '/ Music / P' -E '/ Music / =' Quote.txt It Was An Evening of Splendid Music and Company. 2p.s: New version of SED ( GNU Sed Version 4.0.5) With -i option, allow direct editing to modify file content without redirect to temporary files such as: / home / le # Cat file Old ABC / Home / Lee # SED -I 'S / OLD / new / 'file / home / le # Cat file new ABC11 Additional text To attach text, use symbol A /, you can attach the specified text or more lines to the specified line. If you do not specify a text placement position, S e d is default it behind each row. Additional text cannot specify a range, only one address mode is allowed. When the text is attached, the result is output on the standard output. Note that it cannot be edited, because SED is executed, first copy the file's line text to the buffer, here the SED editing command executes all operations (not on the initial file), because the text is directly output to the standard output, SED is not copied . To edit the text after the additional operation, save the file, and then run another S e d command to edit it. The content of the file is then moved to the buffer. The additional operation format is as follows:

Code: [address] A / text / text / ... Text address specifies a mode or quotation, locating a new text attached location. A / Notification S e D actually adds A / after the text. Observe the format, pay attention to an oblique line behind each row, which represents the wrap. S e d is executed here, will create a new line and insert the next text. The last row is not inclined, and S e d assumes that this is the end of the additional command. When an additional or inserted text or typing a few S e D commands, the auxiliary S H E L L prompt can be utilized to enter multiple lines of commands. When an additional or inserted text or typing a few S e D commands, the auxiliary S H E L L prompt can be utilized to enter multiple lines of commands. Create a SED script file Create a script file a pp e n d. S e d: The first line is the S E D command interpretation line. The script looks for S e d at this line to run the command, which is positioned in / b i n. The second line begins with / c o m p a n y / start, which is an additional operation start position. A / Notification S e d This is an additional operation, first insert a new line. The third line is an actual text that additional operations to be added to the copy. Output Display additional results. If you want to save the output, redirect to a file. Code: [SAM @ chenwy sam] $ cat append.sed #! / Bin / sed -f / company / a / life Suddenly it Happed. Save it, add executable permissions, run

Code: [Sam @ chenwy sam] chmod u x append.sed [SAM @ chenwy sam] $ ./append.sed quote.txt the Honeysuckle Band Played All Night Long for Only $ 90. it Was An EVENING OF SPLENDID MUSIC AND Company Too Bad The Disco Floor Fell Through at 23:00. The Local Nurse Miss P.neave Was in Attendance. Or use the command line:

Code: [sam @ chenwy sam] $ sed "/ company / a / Then suddenly it happened." Quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Then suddenly it happened.. Too Bad The Disco Floor Fell Through at 23:00. The Local Nurse Miss P.neave Was in Attendance. [SAM @ chenwy sam] $ sed "/ company / i / utter confusion followed." Quote.txt the Honeysuckle Band Played . all night long for only $ 90 utter confusion followed It was an evening of splendid music and company Too bad the disco floor fell through at 23:00 The local nurse Miss P.Neave was in attendance insert text:... insert command similar The additional command is only inserted in front of the specified line. As with the additional command, it only accepts an address. The text UTTER Confusion Followed is inserted in front of the end of A t t e n d c e. Code: [SAM @ chenwy sam] $ sad "/ company / I / utter confusion followed." Quote.txt can also specify:

Code: [SAM @ chenwy sam] $ cat insert.sed #! / Bin / sed -f 4 I / utter confusion followed. Executive result

Code: [SAM @ chenwy sam] $ chmod u x insert.sed [SAM @ chenwy sam] $ ./insert.sed quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. it was an evening of spotid Music and COURL THROUGH AT 23:00. Utter Confusion Followed. The local nurse Miss P.neave WAS in Attendance. Modifying the text modification command will replace the new text of the specified row of matching mode space, the format is as follows: Replace the first row The Honeysuckle Band Played All Night Long for Only $ 90 with The Office Di Bble Band Played Well. First, you must match any part of the first row, you can use mode '/ h o n e y s u c k L E /'. The S e d script file is C H a ​​n g e. s e d. The content is as follows:

Code: [SAM @ chenwy sam] $ cat change.sed #! / Bin / sed -f 3 c / the office dibble band played Well.

Code: [Sam @ chenwy sam] $ chmod u x change.sed [Sam @ chenwy sam] $ ./change.sed quote.txt the Honeysuckle Band Played All Night Long for Only $ 90. it was an evening of spotid Music and COMPANY. The Office Dibble Band Played Well. The Local Nurse Miss P.neave Was in Attendance. or Command Line: Code: [Sam @ Chenwy Sam] $ SED "/ Honeysuck / C / The Office Dibble Band Played Well." quote. TXT The Office Dibble Band Played Well. It was an evening of spotid music and company. Too Bad The Disco Floor Fell Through at 23:00. The local nurse Miss P.neave WAS in Attendance. You can be the same file in the same script. Three motion matching and mixing operations are made to modify, additionally.

Code: [SAM @ chenwy sam] $ cat mix.sed #! / Bin / sed -f 1 c / the dibble Band were groing. / Evening / I / the played Some Great Tunes. 3 A / WHERE WAS THE NURSE TO HELP ?

Code: [SAM @ chenwy sam] $ chmod u x mix.sed [SAM @ chenwy sam] $ ./mix.sed quote.txt the dibble Band were groing. The dibbled Some Great Tunes. It is an evening of spotid MUSIC And Company. Too Bad The Disco Floor Fell Through at 23:00. Where Was The Nurse To Help? The local nurse MISS P.NEAVE WAS in Attendance. Delete text SED Delete text format:

Code: [A D r e s S [, A D r e s s]] D deletes the first line; 1 D means deleting the first line.

Code: [SAM @ Chenwy SAM] $ SED '1D' Quote.txt It Was An Eventing of Splendid Music and Company. Too Bad The Disco Floor Fell Through at 23:00. The local nurse miss p.neave Was in attendance. Delete The first to third lines:

Code: [SAM @ chenwy sam] $ sed '1,3d' quote.txt the local nurse Miss P.neave Was in Attendance. Delete the last line:

Code: [SAM @ chenwy sam] $ SED '$ D' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. It was an evening of spotid Music and company. Too Bad The Disco Floor Fell Through at 23:00. You can also use a regular expression to delete actions. The following example deletes the rows containing text 'n e a v e'. Code: [sam @ chenwy sam] $ sed '/ Neave / d' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23..: 00. Replace the text replacement command Replace the specified mode with the replacement mode, the format is:

Code: [address [, address]] S / PATTERN-TO-FIND / REPLACEMENT-PATTERN / [GPWN] S option Notification SED This is an alternative operation, query pattern - to - find, replacing it with Replacement - Pattern after success . The replacement options are as follows:

Quote: g By default, only the first appearance mode is replaced, use the G option to replace all all appearance mode of the overall situation. p Default S E D Write all Return line to standard output, plus the P option will make the-n option invalid. - n option does not print the output result. W File Name Use this option to orient the output to a file. If the N i g H T is N i g h t, first query N i g h t, then replace it with text N i g h t.

Code: [sam @ chenwy sam] $ sed 's / night / NIGHT /' quote.txt The honeysuckle band played all NIGHT long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at.. 23:00. The local nurse Miss P.neave WAS in Attendance. To remove $ symbols from $ 9 (remember this is a special symbol, you must use / block the special meaning), in the replacement - pattern part does not write any Things, keep blank, but still need to be enclosed in slashes. A string can also be removed in S E D.

Code: [SAM @ chenwy sam] $ SED 'S / / $ /' Quote.txt The Honeysuckle Band Played All Night Long For ONLY 90. IT Was An Evening of Splendid Music and Company. Too Bad The Disco Floor Fell Through At 23:00. THE LOCAL NURSE MISS P.NEAVE WAS IN ATTENDANCE. To make global replacement, replace all appearance mode, just add G option after command. The following example replaces all T h e to WO W! .

Code: [SAM @ chenwy SAM] $ SED 'S / THE / WOW! HONEYSUCKLE BAND Played All Night Long for Only $ 90. It was an evening of spotid Music and company. Too Bad The Disco Floor Fell Through at 23:00. WOW! Local Nurse Miss P.neave WAS in Attendance. Writes the replacement result to a file W option, the following example Replaces the SPLENDID to the SPLENDID's replacement result writes file SED. OUT: Code: [sam @ chenwy sam] $ sed 's / splendid / SPLENDID / w sed.out' quote.txt The honeysuckle band played all night long for only $ 90. It was an evening of SPLENDID music and company. Too bad the disco floor fell THROUGH AT 23:00. The Local Nurse Miss P.neave Was in Attendance. Note that the file is enclosed in the single quotue of the SED. The file results are as follows:

Code: [SAM @ chenwy SAM] $ cat sed.out it was an evening of spotid Music and company. Use Replacement Modify Strings If you are attached or modified a string, you can use the (&) command, & command to save the discovery mode for Re-call it, then put it in the replacement string. A replacement mode is given first, then one is ready to be attached to another mode after the first mode, and there is a back, so that the modification mode will be placed before the match mode. For example, the result of S e d statement S / NURSE / "Hello" & / P is as follows

Code: [SAM @ chenwy sam] $ SED -N 'S / NURSE / "Hello" & / P' Quote.txt the local "hello" Nurse Miss P.neave Was in Attendance. The original sentence is the Local Nurse Miss P.neave Was in Attendance. Remember to use spaces in mode because the output results indicate that the space should be added. Another example:

Code: [sam @ chenwy sam] $ sed -n 's / played / from Hockering & / p' quote.txt The honeysuckle band from Hockering played all night long for only $ 90 is the original sentence The honeysuckle band played all night long for. ONLY $ 90. Write the SED results into a file command Like> file redirect transmission and output to a file, you can also enter the result in the S e D command. The format is a bit like the replacement command:

Code: [A D r e s s [, address]] w filename'w 'option notifies S e d Write the result into the file. f i l e n a m e is a self-explanatory file name. There are two examples below.

Code:.. [Sam @ chenwy sam] $ sed '1,2 w filedt' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23 : 00. The Local Nurse Miss P.neave Was in Attendance. File quote. TXT output to the screen. The mode range is 1, 2 lines output to file f i l e d t. Code: [SAM @ chenwy sam] $ CAT Filedt The Honeysuckle Band Played All Night Long for Only $ 90. IT WAS An Evening of Splendid Music and Company. Query mode n e a v e, match the result row write file F i l e d h t.

Code: [sam @ chenwy sam] $ sed '/ Neave / w dht' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23.. : 00. The Local Nurse Miss P.neave Was in Attendance.

Code: [SAM @ chenwy sam] $ CAT DHT THE LOCAL NURSE MISS P.NEAVE WAS IN ATTENDANCE. When reading text in the file, S E D allows you to read text from another file and attach it to the current file. After this command is placed behind the mode match line, the format is:

Code: Address R FileName This R Notes S e d will read the text from another file source. f i l e n a m e is its file name. Create a small file S e d e x. T x t, the content is as follows:

Code: [SAM @ chenwy sam] $ Echo "Boom Boom Went The Music"> sedex.txt [Sam @ chenwy SAM] $ CAT SEDEX.TXT BOOM BOOM WENT The MUSIC will be attached to the file quote. Txt content . Place an additional text in the mode matching row / C O m p A n y. This example is the third line. Note that the name read needs to be enclosed in single quotes.

Code: [SAM @ chenwy sam] $ SED '/company./r sedex.txt' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. It was an ann of Splendid Music and Company. Boom Boom Went The Music TOO Bad The Disco Floor Fell Through at 23:00. The local nurse Miss P.neave Was in Attendance. After matching, you will sometimes need to exit the SED after the mode match is first appeared to perform other processing scripts. Exit command format is:

Code: Address Q The following example assumes the query mode /. A. * /, Means any character A, follow the characters 0 times or any time. The query is the first appearance mode and then exits. Need to place q at the end of the S e d statement. Code: [SAM @ chenwy sam] $ sad '/.a.*/q' Quote.txt the Honeysuckle Band Played All Night Long for Only $ 90. Displaying the control character 1, $ vi dos.txt After entering VI, Use Ctrl V to generate control characters with Ctrl M, I don't know if you do not view the Cat -V filename command to view the editing file.

Code: [SAM @ chenwy sam] $ cat -v dos.txt 12332 ## DISO ## 45.12 ^ m 00332 ## lpso ## 23.14 ^ m 01299 ## uspd ## 34.46 ^ MS E D format is:

Code: [A D r E S S, [A D r E S]] L'L 'means a list. Under normal circumstances, the entire file is listed instead of mode matching line, so use L to be from the first to the last line. Mode range 1, $ is here.

Code: [Sam @ chenwy sam] $ sed -n '1, $ l' dos.txt 12332 ## DISO ## 45.12 / r $ 00332 ## lpso ## 23.14 / r $ 01299 ## uspd ## 346 / r One important feature of the processing control character uses the SED implementation is to remove the control character in the file downloaded in another system. Here is some of the partial scripts that are transmitted (D O S. T x T). All suspicious characters must be removed so that the account owner uses the file. Deleting all # characters is easy, you can use the global replacement command. Here is two or more # symbols in one space.

Code: [SAM @ chenwy sam] $ SED 'S / ## / / g' dos.txt 12332 DISO 45.12 00332 LPSO 23.14 01299 USPD 34.46. Delete 0 of all lines. Use the ^ symbol representation from the first place, ^ 0 * indicates the first sense of the line. The mode S / ^ 0 * / / g setting the replacement portion is empty, that is, the delete mode is the requirements.

Code: [SAM @ Chenwy SAM] $ SED 'S / ## / / g; s / ^ 0 * / / g' dos.txt 12332 DISO 45.12 332 LPSO 23.14 1299 USPD 34.46 Finally remove the end of the row ^ M symbol, for this purpose Need to do global replacement. Set the replacement portion empty. The mode is: 's / ^ m / / g', pay attention to '^ m', this is a control character. You must also use ^ m to control characters in the command line! ?

Code: [SAM @ chenwy SAM] $ SED 'S / ## / / g; s / ^ 0 * / / g; s / ^ m / / g' dos.txt 12332 DISO 45.12 332 LPSO 23.14 1299 USPD 34.46 or

Code: [SAM @ chenwy sam] $ cat dos.txt | sed 's / ^ 0 * / / g' | SED 'S / ^ m / / g' | SED 'S / ## / / g'

Code: [sam @ Linux_chenwy sam] $ cat quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23:00 The local nurse Miss... P.neave WAS in Attendance.1 Using P (RINT) Shows the line only in the second line, with -n code: [SAM @ Linux_chenwy SAM] $ sed -n '2p' quote.txt it was an evening of splendid Music and Company.2 Print range can specify the range of lines, and you can print 1 to 3 lines, separated by a comma.

Code:.. [Sam @ Linux_chenwy sam] $ sed -n '1,3p' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23 : 00.3 Print mode Assume that you want to match the word N EAVE, and print this line, the method is as follows. Using mode / P A t t e R n / format, here / n e a v e /.

Code: [SAM @ Linux_chenwy SAM] $ SED -N '/ THE / P' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. The local nurse miss P.neave Was in Attendance.4 Mode and line number The query can be used in conjunction with the line number and mode. Assume that the file q u o t e. T x t the word T h e in the last row, use S E D query T h E, return two lines:

Code: [SAM @ Linux_chenwy SAM] $ SED -N '/ The / P' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. The local nurse Miss P.neave Was in Attendance. Mixed Multiplexing The way can remove the first line, the format is line _ number, / pattern /. Commerity is used to separate the line number and mode start part. To achieve the expected result, use 4, / t h e /. It means that only in the fourth line query mode T h e, the command is as follows:

Code: [SAM @ Linux_chenwy SAM] $ SED -N '4, / The Local Nurse Miss P.neave Was in Attendance. It is failed to play the fourth line. This model should be found in the specified row, and start finding from the next line until it is found, and the print is printed before the line is found. If the specified line itself is compliant, it is necessary to use a backslash / shield a special meaning before matching all prints of the Bank and the rows of the row. The mode is // $ / p.

Code: [SAM @ Linux_chenwy SAM] $ SED -N '/ / $ / P' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90.6 Show the entire file to print the entire file, just set the row range to the first line To the last row, 1, $. $ 意 为 为 行 行. Code: [sam @ Linux_chenwy sam] $ sed -n '1, $ p' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at.. 23:00. The Local Nurse Miss P.neave Was in Attendance.7 Matching any letters, followed by 0 times or multiple times of any letters, and ending with ING, mode is /. * Ing /. You can use this mode to query any word ending with I N g.

Code: [SAM @ Linux_chenwy SAM] $ SED -N '/.GHENW' Quote.txt It Was An Evening Of SPLENDID MUSIC AND Company.8 The first line is printed in the first line, using the line number:

Code: [SAM @ Linux_chenwy SAM] $ SED -N '1P' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90.9 Last line to print the last line, use $. $ Is a metamor of the last line.

Code: [SAM @ Linux_chenwy SAM] $ SED -N '$ P' Quote.txt The Local Nurse Miss P.neave Was in Attendance.10 Print Board To print the line number, use the equal number =. The line number matched by the print mode, uses the format / p A t t e R n / =.

Code:. [Sam @ Linux_chenwy sam] $ sed -e '/ music / =' quote.txt The honeysuckle band played all night long for only $ 90 2 It was an evening of splendid music and company Too bad the disco floor fell through. At 23:00. The Local Nurse Miss P.neave Was in Attendance. The entire file is printed and the matched row prints the line number. If you only care about the actual line number, use the - e option.

Code: [SAM @ Linux_chenwy SAM] $ SED-N '/ Music / =' Quote.txt 2 If only the printed line number and match row, two S e d commands must be used and the E option is used. The first command print mode match line, the second use = option print line number, format is SED -N -E / PATTERN / P -E / PATTERN / =

Code: [SAM @ Linux_chenwy SAM] $ SED -N -E '/ Music / P' -E '/ Music / =' Quote.txt It Was An Evening of Splendid Music and Company. 2p.s: New version of SED ( GNU Sed Version 4.0.5) With -i option, allow direct editing to modify file content without redirect to temporary files such as: / home / le # Cat file Old ABC / Home / Lee # SED -I 'S / OLD / new / 'file / home / le # Cat file new ABC11 Additional text To attach text, use symbol A /, you can attach the specified text or more lines to the specified line. If you do not specify a text placement position, S e d is default it behind each row. Additional text cannot specify a range, only one address mode is allowed. When the text is attached, the result is output on the standard output. Note that it cannot be edited, because SED is executed, first copy the file's line text to the buffer, here the SED editing command executes all operations (not on the initial file), because the text is directly output to the standard output, SED is not copied . To edit the text after the additional operation, save the file, and then run another S e d command to edit it. The content of the file is then moved to the buffer. The additional operational format is as follows: Code: [address] a / text / text / ...... text address specifies a mode or quotation, positioning a new text attached location. A / Notification S e D actually adds A / after the text. Observe the format, pay attention to an oblique line behind each row, which represents the wrap. S e d is executed here, will create a new line and insert the next text. The last row is not inclined, and S e d assumes that this is the end of the additional command. When an additional or inserted text or typing a few S e D commands, the auxiliary S H E L L prompt can be utilized to enter multiple lines of commands. When an additional or inserted text or typing a few S e D commands, the auxiliary S H E L L prompt can be utilized to enter multiple lines of commands. Create a SED script file Create a script file a pp e n d. S e d: The first line is the S E D command interpretation line. The script looks for S e d at this line to run the command, which is positioned in / b i n. The second line begins with / c o m p a n y / start, which is an additional operation start position. A / Notification S e d This is an additional operation, first insert a new line. The third line is an actual text that additional operations to be added to the copy. Output Display additional results. If you want to save the output, redirect to a file.

Code: [SAM @ chenwy sam] $ cat append.sed #! / Bin / sed -f / company / a / life Suddenly it Happed. Save it, add executable permissions, run

Code: [Sam @ chenwy sam] chmod u x append.sed [SAM @ chenwy sam] $ ./append.sed quote.txt the Honeysuckle Band Played All Night Long for Only $ 90. it Was An EVENING OF SPLENDID MUSIC AND Company Too Bad The Disco Floor Fell Through at 23:00. The Local Nurse Miss P.neave Was in Attendance. Or use the command line:

Code: [sam @ chenwy sam] $ sed "/ company / a / Then suddenly it happened." Quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Then suddenly it happened.. Too Bad The Disco Floor Fell Through at 23:00. The Local Nurse Miss P.neave Was in Attendance. [SAM @ chenwy sam] $ sed "/ company / i / utter confusion followed." Quote.txt the Honeysuckle Band Played . all night long for only $ 90 utter confusion followed It was an evening of splendid music and company Too bad the disco floor fell through at 23:00 The local nurse Miss P.Neave was in attendance insert text:... insert command similar The additional command is only inserted in front of the specified line. As with the additional command, it only accepts an address. The text UTTER Confusion Followed is inserted in front of the end of A t t e n d c e. Code: [SAM @ chenwy sam] $ sad "/ company / I / utter confusion followed." Quote.txt can also specify:

Code: [SAM @ chenwy sam] $ cat insert.sed #! / Bin / sed -f 4 I / utter confusion followed. Executive result

Code: [SAM @ chenwy sam] $ chmod u x insert.sed [SAM @ chenwy sam] $ ./insert.sed quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. it was an evening of spotid Music and COURL THROUGH AT 23:00. Utter Confusion Followed. The local nurse Miss P.neave WAS in Attendance. Modifying the text modification command will replace the new text of the specified row of matching mode space, the format is as follows: Replace the first row The Honeysuckle Band Played All Night Long for Only $ 90 with The Office Di Bble Band Played Well. First, you must match any part of the first row, you can use mode '/ h o n e y s u c k L E /'. The S e d script file is C H a ​​n g e. s e d. The content is as follows:

Code: [SAM @ chenwy sam] $ cat change.sed #! / Bin / sed -f 3 c / the office dibble band played Well.

Code: [Sam @ chenwy sam] $ chmod u x change.sed [Sam @ chenwy sam] $ ./change.sed quote.txt the Honeysuckle Band Played All Night Long for Only $ 90. it was an evening of spotid Music and COMPANY. The Office Dibble Band Played Well. The Local Nurse Miss P.neave Was in Attendance. or Command Line: Code: [Sam @ Chenwy Sam] $ SED "/ Honeysuck / C / The Office Dibble Band Played Well." quote. TXT The Office Dibble Band Played Well. It was an evening of spotid music and company. Too Bad The Disco Floor Fell Through at 23:00. The local nurse Miss P.neave WAS in Attendance. You can be the same file in the same script. Three motion matching and mixing operations are made to modify, additionally.

Code: [SAM @ chenwy sam] $ cat mix.sed #! / Bin / sed -f 1 c / the dibble Band were groing. / Evening / I / the played Some Great Tunes. 3 A / WHERE WAS THE NURSE TO HELP ?

Code: [SAM @ chenwy sam] $ chmod u x mix.sed [SAM @ chenwy sam] $ ./mix.sed quote.txt the dibble Band were groing. The dibbled Some Great Tunes. It is an evening of spotid MUSIC And Company. Too Bad The Disco Floor Fell Through at 23:00. Where Was The Nurse To Help? The local nurse MISS P.NEAVE WAS in Attendance. Delete text SED Delete text format:

Code: [A D r e s S [, A D r e s s]] D deletes the first line; 1 D means deleting the first line.

Code: [SAM @ Chenwy SAM] $ SED '1D' Quote.txt It Was An Eventing of Splendid Music and Company. Too Bad The Disco Floor Fell Through at 23:00. The local nurse miss p.neave Was in attendance. Delete The first to third lines:

Code: [SAM @ chenwy sam] $ sed '1,3d' quote.txt the local nurse Miss P.neave Was in Attendance. Delete the last line:

Code: [SAM @ chenwy sam] $ SED '$ D' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. It was an evening of spotid Music and company. Too Bad The Disco Floor Fell Through at 23:00. You can also use a regular expression to delete actions. The following example deletes the rows containing text 'n e a v e'. Code: [sam @ chenwy sam] $ sed '/ Neave / d' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23..: 00. Replace the text replacement command Replace the specified mode with the replacement mode, the format is:

Code: [address [, address]] S / PATTERN-TO-FIND / REPLACEMENT-PATTERN / [GPWN] S option Notification SED This is an alternative operation, query pattern - to - find, replacing it with Replacement - Pattern after success . The replacement options are as follows:

Quote: g By default, only the first appearance mode is replaced, use the G option to replace all all appearance mode of the overall situation. p Default S E D Write all Return line to standard output, plus the P option will make the-n option invalid. - n option does not print the output result. W File Name Use this option to orient the output to a file. If the N i g H T is N i g h t, first query N i g h t, then replace it with text N i g h t.

Code: [sam @ chenwy sam] $ sed 's / night / NIGHT /' quote.txt The honeysuckle band played all NIGHT long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at.. 23:00. The local nurse Miss P.neave WAS in Attendance. To remove $ symbols from $ 9 (remember this is a special symbol, you must use / block the special meaning), in the replacement - pattern part does not write any Things, keep blank, but still need to be enclosed in slashes. A string can also be removed in S E D.

Code: [SAM @ chenwy sam] $ SED 'S / / $ /' Quote.txt The Honeysuckle Band Played All Night Long For ONLY 90. IT Was An Evening of Splendid Music and Company. Too Bad The Disco Floor Fell Through At 23:00. THE LOCAL NURSE MISS P.NEAVE WAS IN ATTENDANCE. To make global replacement, replace all appearance mode, just add G option after command. The following example replaces all T h e to WO W! .

Code: [SAM @ chenwy SAM] $ SED 'S / THE / WOW! HONEYSUCKLE BAND Played All Night Long for Only $ 90. It was an evening of spotid Music and company. Too Bad The Disco Floor Fell Through at 23:00. WOW! Local Nurse Miss P.neave WAS in Attendance. Writes the replacement result to a file W option, the following example Replaces the SPLENDID to the SPLENDID's replacement result writes file SED. OUT: Code: [sam @ chenwy sam] $ sed 's / splendid / SPLENDID / w sed.out' quote.txt The honeysuckle band played all night long for only $ 90. It was an evening of SPLENDID music and company. Too bad the disco floor fell THROUGH AT 23:00. The Local Nurse Miss P.neave Was in Attendance. Note that the file is enclosed in the single quotue of the SED. The file results are as follows:

Code: [SAM @ chenwy SAM] $ cat sed.out it was an evening of spotid Music and company. Use Replacement Modify Strings If you are attached or modified a string, you can use the (&) command, & command to save the discovery mode for Re-call it, then put it in the replacement string. A replacement mode is given first, then one is ready to be attached to another mode after the first mode, and there is a back, so that the modification mode will be placed before the match mode. For example, the result of S e d statement S / NURSE / "Hello" & / P is as follows

Code: [SAM @ chenwy sam] $ SED -N 'S / NURSE / "Hello" & / P' Quote.txt the local "hello" Nurse Miss P.neave Was in Attendance. The original sentence is the Local Nurse Miss P.neave Was in Attendance. Remember to use spaces in mode because the output results indicate that the space should be added. Another example:

Code: [sam @ chenwy sam] $ sed -n 's / played / from Hockering & / p' quote.txt The honeysuckle band from Hockering played all night long for only $ 90 is the original sentence The honeysuckle band played all night long for. ONLY $ 90. Write the SED results into a file command Like> file redirect transmission and output to a file, you can also enter the result in the S e D command. The format is a bit like the replacement command:

Code: [A D r e s s [, address]] w filename'w 'option notifies S e d Write the result into the file. f i l e n a m e is a self-explanatory file name. There are two examples below.

Code:.. [Sam @ chenwy sam] $ sed '1,2 w filedt' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23 : 00. The Local Nurse Miss P.neave Was in Attendance. File quote. TXT output to the screen. The mode range is 1, 2 lines output to file f i l e d t. Code: [SAM @ chenwy sam] $ CAT Filedt The Honeysuckle Band Played All Night Long for Only $ 90. IT WAS An Evening of Splendid Music and Company. Query mode n e a v e, match the result row write file F i l e d h t.

Code: [sam @ chenwy sam] $ sed '/ Neave / w dht' quote.txt The honeysuckle band played all night long for only $ 90 It was an evening of splendid music and company Too bad the disco floor fell through at 23.. : 00. The Local Nurse Miss P.neave Was in Attendance.

Code: [SAM @ chenwy sam] $ CAT DHT THE LOCAL NURSE MISS P.NEAVE WAS IN ATTENDANCE. When reading text in the file, S E D allows you to read text from another file and attach it to the current file. After this command is placed behind the mode match line, the format is:

Code: Address R FileName This R Notes S e d will read the text from another file source. f i l e n a m e is its file name. Create a small file S e d e x. T x t, the content is as follows:

Code: [SAM @ chenwy sam] $ Echo "Boom Boom Went The Music"> sedex.txt [Sam @ chenwy SAM] $ CAT SEDEX.TXT BOOM BOOM WENT The MUSIC will be attached to the file quote. Txt content . Place an additional text in the mode matching row / C O m p A n y. This example is the third line. Note that the name read needs to be enclosed in single quotes.

Code: [SAM @ chenwy sam] $ SED '/company./r sedex.txt' Quote.txt The Honeysuckle Band Played All Night Long for Only $ 90. It was an ann of Splendid Music and Company. Boom Boom Went The Music TOO Bad The Disco Floor Fell Through at 23:00. The local nurse Miss P.neave Was in Attendance. After matching, you will sometimes need to exit the SED after the mode match is first appeared to perform other processing scripts. Exit command format is:

Code: Address Q The following example assumes the query mode /. A. * /, Means any character A, follow the characters 0 times or any time. The query is the first appearance mode and then exits. Need to place q at the end of the S e d statement. Code: [SAM @ chenwy sam] $ sad '/.a.*/q' Quote.txt the Honeysuckle Band Played All Night Long for Only $ 90. Displaying the control character 1, $ vi dos.txt After entering VI, Use Ctrl V to generate control characters with Ctrl M, I don't know if you do not view the Cat -V filename command to view the editing file.

Code: [SAM @ chenwy sam] $ cat -v dos.txt 12332 ## DISO ## 45.12 ^ m 00332 ## lpso ## 23.14 ^ m 01299 ## uspd ## 34.46 ^ MS E D format is:

Code: [A D r E S S, [A D r E S]] L'L 'means a list. Under normal circumstances, the entire file is listed instead of mode matching line, so use L to be from the first to the last line. Mode range 1, $ is here.

Code: [Sam @ chenwy sam] $ sed -n '1, $ l' dos.txt 12332 ## DISO ## 45.12 / r $ 00332 ## lpso ## 23.14 / r $ 01299 ## uspd ## 346 / r One important feature of the processing control character uses the SED implementation is to remove the control character in the file downloaded in another system. Here is some of the partial scripts that are transmitted (D O S. T x T). All suspicious characters must be removed so that the account owner uses the file. Deleting all # characters is easy, you can use the global replacement command. Here is two or more # symbols in one space.

Code: [SAM @ chenwy sam] $ SED 'S / ## / / g' dos.txt 12332 DISO 45.12 00332 LPSO 23.14 01299 USPD 34.46. Delete 0 of all lines. Use the ^ symbol representation from the first place, ^ 0 * indicates the first sense of the line. The mode S / ^ 0 * / / g setting the replacement portion is empty, that is, the delete mode is the requirements.

Code: [SAM @ Chenwy SAM] $ SED 'S / ## / / g; s / ^ 0 * / / g' dos.txt 12332 DISO 45.12 332 LPSO 23.14 1299 USPD 34.46 Finally remove the end of the row ^ M symbol, for this purpose Need to do global replacement. Set the replacement portion empty. The mode is: 's / ^ m / / g', pay attention to '^ m', this is a control character. You must also use ^ m to control characters in the command line! ?

Code: [SAM @ chenwy SAM] $ SED 'S / ## / / g; s / ^ 0 * / / g; s / ^ m / / g' dos.txt 12332 DISO 45.12 332 LPSO 23.14 1299 USPD 34.46 or

Code: [SAM @ chenwy sam] $ cat dos.txt | sed 's / ^ 0 * / / g' | SED 'S / ^ m / / g' | SED 'S / ## / / g'

转载请注明原文地址:https://www.9cbs.com/read-32340.html

New Post(0)