Vim last-line mode and sed command

Source: Internet
Author: User

First, in Vim into the last line mode to find and replace:

Press/or in edit mode to find a pattern

Format:/pattern

You can use N or N to find the previous or next


The S command in the last command, which is the command to replace, looks for and then replaces it according to the pattern.

Format:

: [Address]s/pattern/string/control Slash delimiter can be used with any character


[Address]: Addresses delimiter

1. If omitted, indicates that the cursor is in the row

2.# the line number represented by the number,

3.$ the last line of the current document; $-1, next to the penultimate line

4.m,n:m and N both represent numbers, meaning from line m to nth row

5.m,+n

6./pattern/: line to which PATTERN is matched

7./pattern1/,/pattern2/: Rows from PATTERN1 to PATTERN2

8.%: All Rows


PATTERN: the string to replace

String: Replaced

CONTROL:

G: All matching characters in the current line are replaced all


Cases:

: s/asd/123/replaces the current line the first ASD is 123

:%s/asd/123/Replace All lines the first ASD is 123

:%s/asd/123/g replaces all lines all ASD is 123

:/qwe/s/asd/123/replaces the first ASD in the first row containing the qwe is 123

: G/qwe/s/asd/123/replaces the first ASD in all rows that contain qwe is 123


Ii. sed command

When you implement text file processing, you edit one or more files in the behavior unit

When the SED processes a file, it opens a temporary memory buffer called pattern space, in which each row is placed sequentially into the pattern space for processing. For rows that are cached within a pattern space

Format: sed [options] ' script ' [file]

By default, the file content processed by SED is done in memory and does not affect the contents of files stored on disk

-N: For lines that cannot be matched by pattern, do not send to standard output

-E script: Editing with multiple script conditions, the order in which the commands are executed has an effect on the result

-F Sript-file: reads the contents of the script in the specified script-file and edits

-I edit source files directly

-R enables SED to support extended regular expressions


Script:addresses+command

Addresses: Address delimiter

1. Empty address: Represents the editing process for all lines of the specified file

2. Single address: sed processes a unique row that can match the address:

Number: Indicates the specified line numbers

/pattern/: Specifies all rows that are matched by PATTERN

$: Indicates last line, $-1 not supported

3. Address range:

ADDR1,ADDR2 All represent line numbers, all lines ending from addr1 to ADDR2, including ADDR1 and ADDR2

Fisrt~step First and step are numbers, all rows that go through step step

Addr1,+n altogether n+1 line

Addr1,~n starting from Addr1 line, looking backwards for addr1*n rows, including add1 rows

4./pattern1/,/pattern2/from a line that is PATTERN1 matched to the end of a line that is PATTERN2 matched


Command:

= Show line number matched by pattern

A \text appends the text character to the line that is matched by the pattern, and supports line wrapping to append multiple lines of information

I \text the text before appending the character

c \text characters modified to text

D Delete rows that are pattern-matched in pattern space

Note: Using the-n option is generally not recommended when using the D command

p sends matching rows in pattern space to standard output

Note: It is generally recommended to use the-N option when using the P command

w filename can be either an absolute or a relative path, and the W command stores the lines in the pattern space that are matched by pattern in the specified file

Note: It is recommended to use the-N option

r filename adds the matched line to the specified file


! Command: The line that is not matched by the pattern is executed by the row


s///: Find and replace, the separator can be changed arbitrarily

S/regexp/replacement/[control]

regexp: Regular expression, PATTERN

Replacement: Replace the content, pure string

support for back-to-reference

s/\ (string\)/&/

s/\ (string\)/\1/


Example:

Sed-n/asd/p file finds rows in file that are matched by ASD and outputs to standard output

Sed-i/asd/s/[[:d Igit:]]/a/file converts all numbers in file that are matched by ASD to a and write to the file

Sed-i ' $a 123 ' file adds a row at the end of the file 123


Advanced Editing Commands:

The SED command places some of the results of the operation in another buffer to perform some complex operations, called holding space


H: Store the contents of the pattern space in the hold space, and overwrite the original content in the preserving space;

H: Store the contents of the pattern space in the hold space, and append to the original content;

G: From the holding space to take out the data stored in the mode space, and cover the original content of the pattern space;

G: Data from the hold space is stored in the mode space, and appended to the original content;

x: Exchange The content in the pattern space with the content in the hold space;

N: Reads the next row of the line that the pattern matches to, overwriting the pattern space;

N: reads the next row of the line that the pattern matches to, appending to the pattern space;

D: Delete all rows in the pattern space;


Such as:

Sed-n ' n;p ' FILE output even rows

Sed ' 1! G;h;$!d ' FILE Reverse output

Sed ' $! n;$! D ' FILE output last two lines

Sed ' $!d ' FILE output last line

Sed ' G ' FILE joins blank line output after each line

Blank line for the Total row count of the sed ' g ' File output files

Sed '/^$/d; G ' FILE adds a blank line output after each line, and if it is one or more blank lines, it is merged into a single row

Sed ' n;d ' FILE to delete even rows

Sed-n ' 1! g;h; $p ' FILE reverse output


Vim last-line mode and sed command

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.