Sed in Linux Shell notes

Source: Internet
Author: User
Tags printable characters

This document describes the basic usage of sed. We recommend that you take a look at the details of SED and awk In the third edition. Sed matches the regular expression to output the command to process the text, it is a headache to be unfamiliar with regular expressions. Note that the following scripts are all tested on the script file using the command sed-F scrift files

1. sed first reads a row to the mode space, and first clears the content of the previous mode space (except command N in multi-line mode). The script starts from the top of the script to process the content of the mode space, process a command to update the content of the mode space. The new command is executed in the updated mode space. After arriving at the bottom of the script, it is sent to the screen by default. You can also use-N to disable this default output, sed does not modify the source file content.

2. The SED command can specify 0 addresses, one or two. Each address is a regular expression of the description mode, line number or line addressing symbol.

3. Sed Command Format: sed [Options] 'command' files; Sed [Options]-F scriptfile files

3.1 append, insert, and change

A \ adds a line of text behind the current line. I \ inserts data on the current row. The newly inserted data is not read and processed. C \ changes the line text, such as 1 ~ 3 rows into 123456789 1, 3C \ 123456789 $ A \ end of file added $ I \ end of file after the last line added $ C \ end of file before the last line replaced the last line encountered a strange problem, the third edition of SED and awk states that AIC will modify the content of the mode space, which is not mentioned in the built-in help file. The test shows that the subsequent commands are not executed after the AIC command is executed, for example, 1 {c \ 1234 = p} changes the content of the first line to 1234, and then prints the row number and test output. However, the command following the result is not executed, if you know, please let me know. Thank you.

3.2 Replace [address] S/pattern/replacement/flags with replacement to replace regular expression pattern. Flag can be a flag.

 

N 1 ~ The number between 512, indicating that only the occurrence of the nth occurrence of pattern G is replaced, if no G is used to replace pattern p that appears for the first time, it only prints W file. If no matching address exists, all rows are matched. the delimiters with different addresses are slash /, the regular expression can be separated by any character, such as S/444/111/. It can be written as s244421112. Now the Delimiter is 2. Of course, this is an example. If the expression contains delimiters, use a backslash to escape them. In the pattern section, we can use \ (and \) to block parts. In the replacement section, we can use \ n (n is a number) to represent the nth part of pattern, use & to indicate the string matched by the entire pattern. In this example, the o in Google appears in multiple places in the document, but it is found that less E is written. Can be S/\ <go \ {2, \} GL \>/& E/g, \ <and \> is the anchoring word, & E indicates that only an E is added based on the original one. For example, if Google does not find that there is less g in O, what should we do if we need to retain the original number of o During replacement? This requires the use of blocks, S/\ <G \ (O \ {2, \} \) Le \>/g \ 1gle/g, here, \ (and \) is used in pattern to divide the block, and then \ 1 is called in replacement.

3.3 Delete row D, for example, Delete row 1, 3, and 3D.

3.4 List l list of characters that cannot be printed. The printable characters are displayed as ASCII codes for two numbers, such as \ n \ t...

3.5 Convert [address] Y/ABC/XYZ/

This command converts each character in the string ABC to the corresponding character in XYZ by position.
For example, you can use y/abcdefghijklmnopqrstuvwxyz/

3.6 print P

3.7 print row number =

3.8 read the next line N and use the next command for processing, instead of reprocessing. The following shows how to delete the next line if the next line is empty after the row 111 is processed.

/111/{S/111/222/; n;/^ $/d;} In fact, the next line entered replaces the current row of the mode space, subsequent commands of the script act on the replaced line.

3.9 read/write files

R file: The read file does not exist and no error will be reported. W file: if the file does not exist, a new one will be created. If so, the old data will be written for the first time and the new data will be written as the append data.

3.10 quit Q quit SED

For example, exit SED/111/Q file after matching the row to 111.

3.11! Indicates that the subsequent commands will work on all unselected lines.
3.12 = print the current row
3.13 # comment row

Advanced commands, sed usually reads a row to the mode space and executes the commands in the script one by one. When the script ends, this row is output and the mode space is cleared, and then the next row to the mode space is read in this loop, the SED multi-line mode command can read multiple lines at a time and then perform matching, such as processing cross-line phrases.

3.14 multi-row mode space n d p corresponds to n d p in single-row Mode

P prints only the first row of the multi-row mode, and P prints the content of the control in the entire mode. P often appears after N and before D. D. Only Delete the first row of the multiline mode. If the new row is not read to the mode space, it will return to the top of the script and re-execute the script. D. Delete the content in the mode space, as a result, when the new row is read and script N is re-executed to read the new row and the existing content in the mode space is added, the read rows are automatically separated by line breaks \ n. Read all rows in the multi-row mode space can be seen as a single row with a line break \ n, ^ match the beginning of the first line $ match the end of the last line. For example, replace the phrase Hello sed with a single row. Ex1: Replace the adjacent row Hello sed with a row of Hello SED/Hello $/{n; S/Hello \ nsed/Hello SED/} ex2: replace multiple blank rows with one blank row/^ * $/{n;/^ * \ N $/d;} If you change d to lowercase D, then, when the blank behavior is even, all the blank spaces are deleted, and if the odd number is, one row is retained, because the mode space is sufficient for two rows. Then, the D command deletes all the spaces and removes one row, use D to match a blank row first. If the next row is a blank row consecutively, delete the first row, return to the top of the script, and re-execute the script based on the content of the current mode space.

3.15 use the reserved space to save the content of the mode space, and use the H g x operation to keep the content of the space

The mode space is the buffer currently being processed, and there is still a space for each retained. Similar to the clipboard, you can copy the processed content to the retained space, hold h/h copy/append the content of the mode space to the keep space get G/G will keep the content of the space assigned/append to the mode space X mode space and keep space content interchange H copy the selected content to the reserved space, previously cached content is cleared. H. append the selected content to the reserved space. The previously cached content remains unchanged, so that multiple rows can be cached. G. G.

3.16 use branch and condition commands: B t

The control in the branch (B) and test (T/T) command scripts is transferred to rows containing special labels. If there is no tag, move it to the end of the script. Branch B is used to transfer [address] B [label] label unconditionally. Optional. When no label exists, it jumps to the end of the script. Test T is used for conditional transfer [address] T [label] label. Optional. When no label exists, it jumps to the end of the script. If a successful replacement is performed on the row with the matched address, the test command is forwarded to the tag for testing (t) and (t) on the contrary, conditional transfer [address] T [label] label is optional. If the replacement fails on the row of the current matched address, then the test command will go to the tag where T and T are used to test the result tag of the S // replace command. The result tag itself occupies one line and starts with "B label" and jumps unconditionally, use the branch B jump Method to delete multiple blank lines and replace them with one blank line. When the blank lines are read cyclically, then, replace the blank lines in the multi-row mode space with a single blank line/^ * $/{: rdn N;/^ \ (* \ n * \) \ {1, \} $/B rdn; S/^ \ (* \ n * \) \ {1, \} \ n/} use the test t jump mode to delete multiple blank lines and replace them with one blank line. When a blank line is read, read the next row and replace the two blank rows. If the replacement is successful, read the next row until the replacement fails/^ * $/{: loop N; s/^ \ (* \ n * \) \ {1, \} $ //; T Loop}

 

4. [Options]

4.1-e command, -- Expression = command allows multiple edits. 4.2-h, -- help print the help and display the address of the Bug list. 4.3-N, -- quiet, -- silent cancel the default output. 4.4-F, -- filer = script-file guide sed script file name. 4.5-V, -- version: print the version and copyright information.

5. metacharacters

^ The start of the anchor line matches the line starting with 1111, and the end of the anchor line matches the end of AAAA./AAAA $ /. match a character including a newline character * match zero or multiple characters [] match a character in a specified range, for example, [123] K match 1 K 2 K 3 K [^] match a character not specified characters in the range \(.. \) Save matched characters, such as S/\ (love \) Able/\ 1RS, and loveable is replaced with lovers. & Save the search characters to replace other characters, such as S/love/** & **/. The love character is ** love **. \ <Specifies the start of a word, for example, "/\ <love. \> Anchor specifies the end of a word, such as/love \>/matches a row containing a word ending with love. X \ {M \} repeated characters X, m times, such as:/0 \ {5 \}/matched rows containing 5 o. The characters x \ {M, \} are repeated at least m times, for example,/O \ {5, \}/matches rows with at least 5 o. The characters x \ {M, N \} must be repeated at least m times, not more than N times, such as:/O \ {5, 10 \}/matching rows of 5--10 O. \ + At least one matching \? 0 or 1 \ | 'regexp1 \ | regexp2 'matches either regexp1 or regexp2. Delete rows containing 111 or 222 \ | 111/d

6. nested grouping command

Sed uses braces to nest an address in another address, or uses multiple commands for the same address, such as matching 3 ~ The SED script file containing the address 444 can be matched in the eight rows as follows: 3, 8 {/444/{S/444/333/other instructions} the above example S/444/333/can be matched with the address before, each command can have its own address and allow multi-layer nesting. If there is a semicolon between commands, multiple sed commands can be placed in the same line, some commands will; when the data is output, for example, A is not easy to read and does not advocate writing in the same line. Some sed statements indicate that the annotation intelligence appears in the first line. No extra space is allowed in the command, no space is allowed after the braces, and the right braces must be a separate line of constraints, however, the SED version I used, GNU sed version 4.2.1, does not have these restrictions.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.