Sed is a non-interactive text editor that edits files or Copies files exported from standard input. The standard input may be from the keyboard, file redirection, string or variable, or a pipe file. Sed can edit small or large files at will. There are many sed commands used to edit and delete files and allow them to be absent from the site during this operation. Sed processes all changes at a time and thus becomes very effective. The most important thing for users is to save time. Sed must use the row number and regular expression to specify how the text line to be changed sed reads data: sed reads data from a text line of the file or from several standard input formats, copy it to an editing buffer, then read the first command of the command line or script, and use these commands to search for the mode or locate the line number to edit it. Repeat the process until the command ends calling the SED command: type a command in the command line, insert the SED command into the script file, and then call sed. Insert the SED command into the script file, the SED script can execute the SED [Option] sed command to input the file and use the SED command on the command line, the actual command must be enclosed by single quotes sed [Option]-F sed script file input file use sed script file [Option] the first line of the input file contains the SED command interpreter sed script file option: N does not print; Sed does not write the editing row to the standard output. By default, all rows are printed (edited and not edited). The p command can be used to print and edit the row. The next command in C is the editing command, this option is added when multiple edits are used. F if you are calling the SED script file, use this option to notify sed of the SED commands supported by a script file, such as sed-F myscript. sed input_file here myscript. sed is a file that supports the SED command. You can use a redirected file to save SED's output. Use sed to locate text in the text: X is a line number, such as 1 X, Y indicates that the row number ranges from X to Y. For example, 2nd indicates that rows from 5th to/pattern/query rows in the include mode, for example,/Disk/or/[A-Z] // pattern/queries rows that contain two modes, such as/Disk/disks // pattern /, X queries rows in the include mode on the given row number, for example,/Disk/, 3 x,/pattern/queries matched rows through the row number and mode, such as 3,/Disk/x, y! The query command does not include the basic sed edit command for lines X and Y: P print matching Row C/replace positioning text with new text = display file row number S replace mode with the corresponding mode A/Add new text information after locating row number r read text from another text i/Insert new text information after locating the row number W write text to a file D Delete positioning row Q exit or immediately exit after matching the first mode l display equivalent control with octal ASCII code character y transfers CHARACTER n to read the next line of text from another text, add the command group G in the next line {} and paste Mode 2 to the/pattern N/basic sed programming example: Use P (RINT) to display the line: sed-N '2p' temp.txt displays only 2nd rows. Use Option n to print the following rows: sed-n'1, 3 P 'temp.txt: Print 1st rows to 3rd rows: sed-n'/movie/'P temp.txt print the row usage mode and row number query with movie: SE D-N '3,/movie/'P temp.txt only searches for movie in row 3rd and prints the entire file: sed-N '1, $ 'P temp.txt $ any character in the last line: sed-n '/. * ing/'P temp.txt note. * ing instead of * ing printing line number: sed-e '/music/= 'temp.txt additional text :( create sed script file) chmod U + x script. sed, runtime. /script. sed temp.txt #! /Bin/sed-F/name1/A/# A/indicates to add text here add new line. # insert text to the added text content:/name1/A/change to 4 I/4 to indicate the row number, and I to insert the modified text: /name1/A/change to/name1/C/modify the entire line. c modify and delete the text: sed '1d 'temp.txt or SED '1, 4D 'temp.txt to replace the text: sed's/source/okstr/'temp.txt replace source with okstr SED's // $ ///G' temp.txt and delete all the $ symbols in the text SED's/source/ okstr/W temp2.txt 'temp.txt Replace the modified string with the last record in the temp2.txt file: sed's/source/"Add before" &/P' temp.txt Add "Add before" before the E string. Here & represents the source character found and saves the SED result and writes it to the file: sed '1, 2 W temp2.txt 'temp.txt sed '/name/W temp2.txt 'temp.txt read text from the file: sed'/name/R temp2.txt 'temp.txt add text at the end of each column: sed's/[0-9] */& pass/G' temp.txt transmits a value from shell to SED: echo $ name | sed "s/go/$ rep/g" note that you need to use double quotation marks to run the following command:'s //. $ // G' Delete the line '-E/ABCD/d' ending with a period to delete the row's/[] [] [] */[]/G' more than one space, use a space to remove the first space of the line 's/^ [] [] * // G //. [] [] */[]/G' Delete the ending period followed by two or more spaces. Use one Spaces replace '/^ $/d' to delete empty rows' s/^. // G' Delete the first character, difference's //. // G' delete all periods (S/COL /(... /) // G' Delete the last three letters (S/^ // G') that follow Col to delete the first letter in the path ///////////// //////////////////////////////////////// /////////////////. Use a period to match a single-character period. "can match any single character. "." Can match the string header or any character in the middle. Assume that a text file is being filtered. For a script set with 1 0 characters, the first four characters must be followed by X c. The matching operation is as follows :.... X c .... 2. match strings or character sequences ^ at the beginning of a row to match only characters or words at the beginning of a row. The first 4th characters in a row are 1, and the matching operation is represented as: ^... 13. match a string or character with $ at the end of a row. It can be said that $ is the opposite of ^. It matches a string or character at the end of a row, and $ is placed after a match word. If the word j e t 0 1 is matched at the end of a row, perform the following operations: j e t 0 1 $ if only one character row is returned, perform the following operations: ^. $4. Use * to match a single character in a string or its recurring series. Use this special character to match any character or string's repeated expressions multiple times. 5. When using/shielding the meaning of a special character, you sometimes need to find some characters or strings that contain a character specified as a special character. If you want to match the regular expression *. the following operations can be performed on all files ending with p a s :/*/. p a S6, use [] to match a range or set, use [] to match a specific string or string set, you can use commas to separate different strings to be matched in the arc, however, this is not mandatory (some systems advocate using commas in complex expressions), which can increase the readability of the mode. "-" Indicates a string range, indicating that the string range starts from the character on the left of "-" and ends with the character on the right. If you want to match any number, you can use: [0 1 2 3 4 5 6 7 8 9] to match any letter, use: [A-z A-Z] indicates the letter range from a-Z and A-Z. 7. Use the/{/} match mode to match all matching results at any time. If the number of matching results is specified, use /{/}, this mode has three forms: Pattern/{n/} match mode appears n times. Pattern/{n,/} match mode appears at least N times. Pattern/{n, m} match mode appears between N to m times, n, m is 0-2 5 any integer in 5. The matching letter A appears twice and ends with B. The operation is as follows: A/{2/} B matches a at least four times for a B. Use: a/{4,/} B in S H E L programming, one of the differences between a good script and a perfect script is to be familiar with regular expressions and learn to use them. In comparison, extracting a piece of text using one command saves a lot of time than using three or four commands to get the same result. //////////////////////////////////////// //// // Detailed description of the regular expression (1) -Lu Xiaobo if we ask UNIX fans what they like most, the answer is not only stable systems and remote startup, but also regular expressions; if we ask them what the biggest headache is, it may be a regular expression in addition to complicated process control and installation processes. So what is a regular expression? How can we grasp Regular Expressions and use them correctly and flexibly? This article will introduce you and hope to help readers who are eager to understand and master regular expressions. In brief, regular expressions are a powerful tool for pattern matching and replacement. We can find regular expressions in almost all UNIX-based tools, such as the VI Editor, Perl or PHP scripting language, and awk or SED Shell programs. In addition, client scripting languages such as JavaScript also provide support for regular expressions. It can be seen that regular expressions have gone beyond the limits of a language or system and become widely accepted concepts and functions. Regular Expressions allow users to construct matching modes by using a series of special characters, and then compare the matching modes with target objects such as data files, program input, and form input on the web page, execute the corresponding program based on whether the comparison object contains the matching mode. For example, the most common application of regular expressions is to verify whether the format of the email address entered by the user online is correct. If the regular expression is used to verify that the email address format is correct, the form information entered by the user will be processed normally. Otherwise, if the email address entered by the user does not match the regular expression mode, A prompt will pop up asking the user to re-enter the correct email address. It can be seen that regular expressions play an important role in the logic judgment of Web applications. After a preliminary understanding of the functions and functions of a regular expression, let's take a look at the syntax format of the regular expression. The regular expression format is generally as follows:/love/where the part between the "/" delimiters is the pattern to be matched in the target object. You only need to place the pattern content of the desired matching object between the "/" delimiters. To enable users to customize the mode content more flexibly, regular expressions provide special "metacharacters ". Metacharacters are special characters that have special meanings in regular expressions. They can be used to specify the mode in which the leading character (that is, the character before the metacharacters) appears in the target object. Frequently Used metacharacters include "+", "*", and "?". The "+" metacharacter specifies that its leading character must appear one or more times consecutively in the target object, the "*" metacharacter specifies that the leading character must appear zero or multiple times in the target object, and "?" Metacharacter specifies that the leading object must appear zero or once consecutively in the target object. Next, let's take a look at the specific application of the regular expression metacharacters. /FO +/because the regular expression above contains the "+" metacharacter, it can be used with the "fool", "FO ", or "football", and so on, one or more character strings that match the letter "F" consecutively. /EG */because the above regular expression contains "*" metacharacters, it can be used with "easy", "ego ", or, "egg" and other strings that appear after the letter E are matched with zero or multiple Letter g consecutively. /Wil? /Because the above regular expression contains "?" Metacharacter, indicating that it can match the "win" or "Wilson" in the target object, and matches zero or one character string after the letter I. In addition to metacharacters, you can also precisely specify the frequency of occurrence of a pattern in a matching object. For example, the/Jim {}/regular expression specifies that the character m can appear 2-6 times in a row in the matching object. Therefore, the regular expression can match strings such as Jimmy or jimmmmmy. After a preliminary understanding of how to use regular expressions, let's take a look at the usage of several other important metacharacters. /S: Used to match a single space character, including the tab key and line break;/S: Used to match all characters except a single space character;/D: Used to match numbers from 0 to 9; /W: Used to match letters, numbers, or underscores./W: Used to match all characters that do not match/W ;.: Used to match all characters except line breaks. (Note: we can regard/s and/W and/w as inverse operations.) let's take a look at how to use the above metacharacters in regular expressions through examples. // S +/the above regular expression can be used to match one or more space characters in the target object. /// D000/if we have a complex financial statement in hand, we can easily find all the total amount of RMB through the above regular expression. In addition to the metacharacters described above, regular expressions also have a unique special character, that is, the positioning character. Specifies the position where the matching mode appears in the target object. Commonly used positioning symbols include "^", "$", "/B", and "/B ". The "^" operator specifies that the matching mode must start with the target string, and the "$" operator specifies that the matching mode must end with the target object, the/B locator specifies that the matching mode must appear at either the beginning or end of the target string, the "/B" Locator specifies that the matched object must be within the boundary of the start and end of the target string. That is, the matched object cannot start with the target string, it cannot end with the target string. Similarly, we can regard "^" and "$" as well as "/B" and "/B" as two sets of operators for inverse operation. For example:/^ hell/because the above regular expression contains the "^" locator, you can use "hell" with the target object ", the string starting with "hello" or "Hellhound" matches. /AR $/because the regular expression above contains the "$" operator, it can match the string ending with "car", "bar", or "Ar" in the target object. // Bbom/because the above regular expression pattern starts with "/B", it can match a string starting with "bomb" or "Bom" in the target object. /Man/B/because the above regular expression pattern ends with a "/B" positioning character, you can use "human" with the target object ", the string ending with "woman" or "man" matches. To make it easier for users to set matching modes flexibly, regular expressions allow users to specify a range in the matching mode, not limited to specific characters. For example:/[A-Z]/the above regular expression will match any uppercase letter from A to Z. /[A-Z]/the above regular expression will match any lowercase letter in the range from A to Z. /[0-9]/the above regular expression will match any number from 0 to 9. /([A-Z] [A-Z] [0-9]) +/the above regular expression will be associated with any string consisting of letters and numbers, for example, "ab0" matches. Note that you can use "()" in a regular expression to combine strings. The content contained by the "()" symbol must appear in the target object at the same time. Therefore, the above regular expression cannot match strings such as "ABC", because the last character in "ABC" is a letter rather than a number. If we want to implement the "or" operation similar to the programming logic in the regular expression, and select one of multiple different modes for matching, we can use the pipe character "| ". For example:/to | too | 2/the above regular expression will match "to", "too", or "2" in the target object. There is also a common operator in the regular expression, that is, the negative character "[^]". Unlike the positioning character "^" described above, the "[^]" negation specifies that the target object cannot contain strings specified in the pattern. For example:/[^ A-C]/the above string will match any character except A, B, and C in the target object. In general, when "^" appears in "[]", it is regarded as a negative operator. When "^" is located outside of "[]" or, it should be regarded as a positioning character. Finally, you can use the Escape Character "/" to add metacharacters to the regular expression mode and find matching objects. For example, the/Th/*/regular expression will match the "th *" in the target object rather than ".
Sed command + Regular Expression