Sed usage Summary

Source: Internet
Author: User

Google uses sed every time, because I can't remember the symbols. Simply summarize some usage to save search time.

1. What is sed?
Sed is an improved non-interactive editor. You can use commands to implement all the VI editing functions. Sed does not change the file content,
Instead, output the result to the screen.

2. Sed and Regular Expressions
Like grep, sed also supports regular expression. Metacharacter and sed supported by grep are supported.

3. Some important commands
(1) P: Output command. For example, sed '/love/P' will output all the text, and the rows that match love will be output 2 times.

(2) N: suppress the default behavior of sed. For example, in the above example, the default output line is SED, and the command P outputs the line again. Sed-n'/love/P' outputs all rows containing love without repeating them.

(3) D: delete command. Delete a row. For example, sed '3d 'deletes the third row and outputs other rows by default.
Sed '3, $ d' deletes the content from the 3 rows to the last row. ',' indicates the range.

(4) S: Replacement command. Replace the string, such as SED's/love/hate/G' FILENAME> filename. Back.
The love string is hate.
Another example is SED's/[0-9] [0-9] $ /&. 5/'datafile searches for the row ending with 2 as a number in the file and follows the two digits. 5, & indicates the string matched by the regular expression.
Another example is SED's #3 #88 # G'. Any symbol after s in datafile is regarded as an alternative '/' and becomes a new demarcation mark.

(5): confirm the selection range. For example, sed-n'/West/,/East/P' datafile first matches west in the file, and then the output line starts until East is matched, note that the regular expression can be a number or a combination of characters. Sed-n'/West/, // &/P' datafile first matches West, and the output row ends until the last row.

(6)-E: editing command: used to connect multiple sed editing commands
For example, sed-e '1, 3D '-E's/love/gloom/G' datafile: First Delete lines 1 to 3, and then replace love with gloom in the full text.
However, you must note that the output of the previous command affects the previous command. They are executed sequentially.

(7) R: Read File command: Allow sed to load the content of a specific file to the next row of the current line.
For example, sed '/Susan/R tmpfile' currentfile: matches Susan in currentfile. Once it matches, the next line
Load the content of tmpfile. If Susan appears multiple times in currentfile, tmpfile will be loaded multiple times.

(8) W: file writing command: Allow sed to write matched lines to the new file
For example, sed-n'/North/W tmpfile' currentfile: match the North in currentfile.
The row is written to tmpfile.

(9) A: Line appending command: append a line of information to the next line of the current line. In TC shell, there must be a \ After A, and the first a is required by SED, which indicates that the command has not ended yet and is transferred to the next line. The replaced text must be placed in a single line. For example: sed '/^ love/A \ Hello world! 'Datafile matches the rows starting with love in datafile, and adds a line of text Hello world!

(10) I: Insert a line of new text before matching the line.

(11) C: Line replacement command: The above, but the difference is to replace the matched line with a new line of text.

(12) N: Read the next row: Read and process the next row of the matched row.
Sed '/^ love/{n; S/is/was/;}' datafile: Search for datafile, match the row starting with love, and read the next row of the matched row
Use the new SED command S/is/was for processing until the last line.

(13) Y: conversion command: defines the ing relationship of character conversion and converts it one by one based on the ing relationship.
For example: sed '1, 5y/abcdefg/'datafile: Based on the given ing relationship, the, B, C, D, E, F, convert g letters to uppercase letters.
(14) Q: exit command: notifies sed to stop processing and quit.
for example, sed '5q' datafile: sed outputs the first five rows by default, and then exits.
for example, sed '/Lewis/{S/Lewis/Joseph/; q;}' datafile: First matches Lewis, then replaces Lewis with Joseph, and immediately exits.
(15) H & G: storage and extraction command: When SED is run, an additional holding buffer is maintained for storing rows, the H command assigns the content in the pattern buffer to the holding buffer, and the G command extracts and processes the row from the holding buffer. For example, sed-e '/love/H'-e' $ G' datafile: search for love in datafile, store matching rows in holding buffer, and process the rows to the last row, reads row data from the holding buffer and outputs the data by default.

(16) H & X: storage and replacement command: H functions as above. X will replace the row data in the holding buffer with the row data currently being processed (data in the pattern buffer ).

3. Sed script
You can write a series of SED commands in a script file. When you run the command sed-F shellfile datafile, sed reads
Each line is processed with the command in shellfile until all lines in datafile are processed completely.
Note that there must be no space after each command; otherwise, an error is reported.

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.