Linux restudying Nine

Source: Internet
Author: User
Tags line editor

Sed advanced editing commands, mode space, hold space, vi visualization mode, VIMRC, address delimitation

Sed (stream deitor): line editor

Overview Sed is a streaming editor that processes a single line of content at a time. When processing, all the contents of the currently processed row are stored in a temporary buffer, called the pattern space, followed by the SED command to process the contents of the buffer, row by line, without modifying the file. SED is mainly used for processing multiple files, simplifying the repeated operation of files, writing conversion programs and so on.

Usage: sed "option" ... ' Script ' File ...

"Option"


-N: Turn off the automatic output of the mode space content, the default output full text includes the modified rows, note the use of occasions, such as in addition to the P command, need to display in addition to the matching mode of the content without

-e: Multi-point editing, merge processing

-f/path/file: Read script from specified file, call script without quotation mark

-R: Support for extended regular expressions

-I: Edit Sed-i.bak ' 3a\sss ' file #创建备份

"Script"


Address delimitation: # #,# +#/pat1/,/pat2//^$/matches multiple lines of blank line

~ Step: Odd-numbered rows 2~2 even rows

"Edit Command"


P: Display modified rows in the mode space

D: Delete the line matching the pattern space

A \text: Append text at the end of the line; Support for multiline append using \ n

I\text: Inserting text in front of a line; support for multiline insertion using \ n

C \text: Replacing a line of behavior or multiple lines of text

W/path/to/somefile: Save the pattern matching line to the specified file

R/path/from/somefile: Reads the text from the specified file to the line in the pattern space

= line number is printed below the line in the pattern space

! Matching row-fetching anti-processing in pattern space

"Find and Replace"


s///: Available @, #代替/number

s/pattern/&new&/p: Append,& to find content to represent pattern

G: In-line global substitution

P: Show the rows that were successfully replaced

W/path/to/where: Replace successful row to file

"Command Action"


Sed-n ' 2,+2p ' file #2到4行的内容

Sed-n '/abc/p ' file #包含abc的行

Sed-n ' 2,/abc/p ' file #2到被模式匹配的行

Sed ' 2i\s\nss ' file #2行后面插入多行

Sed ' 2a\s\nss ' file #2行后面追加多行

Sed ' 2c\word ' file #改变2行内容

Sed-n '/abc/= ' file #显示abc所在的行号, no content

Sed-n-E '/^abc/p '-e '/^abc/= ' #显示abc开头的行号和内容

650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M00/89/C8/wKiom1gcMlDir-89AAAYuYTJ8Ik681.png-wh_500x0-wm_3 -wmp_4-s_2681601544.png "title=" 1.png "alt=" Wkiom1gcmldir-89aaayuytj8ik681.png-wh_50 "/>

Sed-n '/^$/= ' file #显示空行行号

Sed-n-E '/^$/p '-e '/^$/= ' file #在空行下方行号

Sed '/^$/d ' file #去除空行

Sed ' s/oldword/replace/g ' file #替换每一行中需要替换的内容

Sed-n ' s/root/&superman/p '/etc/passwd #单词后追加, displaying modified rows

Sed-n ' s/root/superman&/p '/etc/passwd #单词前追加

Sed-e ' s/dog/cat/'-e ' s/hi/lo/' pets #显示全文包括修改的行

Sed-i.bak ' s/dog/cat/g ' pets #创建备份文件 and modify the contents of the file

"Advanced Editing Commands"


Sed-n ' n;p ' file #把模式空间中的内容覆盖至保持空间中

Sed ' 1! G;h;$!d ' file #除了第一行都覆盖模式空间; overwrite to hold space; Delete pattern space line: Reverse

Sed ' $! n;$! D ' file #读两行; Delete multi-line mode space: last two lines

Sed ' G ' file #追加单行模式空间: Add empty lines

Sed ' g ' file #覆盖模式空间: Replace blank line

Sed '/^$/d; G ' file #删除空行; Append mode space: two blank lines

Sed ' n;d ' file #读取下一行并覆盖; Delete pattern space row (no-n display mode space d Delete mode space row skips one line): Show Odd lines

Sed-n ' 1! G;h: $p ' #除了第一行都覆盖模式空间; overlay hold space; show modification process


"Summary": SED is a streaming editor that reads one line at a time to the schema space and therefore does not manipulate the original file. When the line of the pattern space is modified to be available p for the specified, the-n turns off the automatic printing of the mode space, so that a specific match is achieved; none-N and p implement the pattern space content to print all, such as using D to delete a matching row and then print. In addition to the mode space SED still exists to maintain space, through the advanced editing commands to the pattern space and maintain space coverage, append, delete and other operations to achieve more complex text processing such as: text reverse, mail information processing, etc.


650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M02/89/C9/wKiom1gcOs-REbyCAAJNW3qfhdY297.png-wh_500x0-wm_3 -wmp_4-s_2460580116.png "title=" 1.png "alt=" Wkiom1gcos-rebycaajnw3qfhdy297.png-wh_50 "/>


Open File

VI +# File #定位

VI + file #最后一行

VI +/pattern File #定位到第一个模式所在行

Vi-b Hexfile #hex文件

vi-d file1 file2 #比较打开多个文件, highlighting different places

Vi-m file #只读方式打开

GF #打开光标处文件名的文件, such as Main.c

Ctrl ^ #切换到上次打开的文件. equals: First

Three modes and conversions


Command mode (default): Mainly to view the contents of the file, control the movement of the screen cursor, character, word or line deletion, move to copy a section and enter insert mode, or to external mode


CTRL S | CTRL Q #锁定了, no response, q unlock

Shift ~ #小写变大写, lowercase to uppercase

Shift # #高亮向上找当前光标相同的单词

Shift * #高亮向下找相同单词, Noh cancel highlighting

Shift ZZ # (ZZ) Save and exit, ZQ do not save exit

Page Flip (screen): Ctrl d (lower) Ctrl U (top half) Ctrl f (next screen) Ctrl B (previous screen) PgUp

Page turn (cursor): ZT (cursor line scrolls to the first row) ZZ (cursor row roll middle) ZB (cursor row roller bottom-end) #zt (# line rolled to the first row) #zz #zb

Word Jump: W ere e lower end b Current, first

Cursor movement: Arrow J, K, H, l

Cursor positioning (in line): 0 ^ $

Cursor positioning (Pairing):% #跳转到匹配的)]}

Cursor positioning (inline characters): f_ (positioning to the first of the Bank) F (forward to _)

Cursor positioning (full text): #G (line #) GG (line 1th) G (last line) h\m\l (first position of first row, middle, last row of the current screen)

Cursor position (sentence): Shift (#上一句 shift) #下一句

Cursor positioning (screen line): g (Arrow J K ^ $) #当句子太长时, screen line is very convenient

Cursor position (paragraph): shift} #下一段 Shift} #下一段

Cursor positioning (function): [[#上一个函数]] #下一个函数

Cursor Bookmark settings and jump: M A-Z #设置光标书签, with 26 letters ' A-Z jump to bookmarks

Increase indent: Mate with visual selection > Increase Indent < Decrease Indent

Delete command: D (^ $0 W E B ({)} GG 1G G) explain (line first visible character, the end of line, the beginning of the word, the first, the next word, the current or previous word, the previous sentence, the previous paragraph, the next sentence, the next paragraph, the first, the end of the text) DD (delete the current line) d (delete the end of the line) =d $

Cut command: X (single character) #x (cursor start # characters) XP (swap current x content after next character)

Delete, cut, change commands, stored in the number 1 register

Replacement character: R (whole row) R (single character)

Copy command: Y (^ $0 W E B ({)} GG 1G G)

Paste command: P cursor before P cursor (copy with p from beginning of line when pasting word, word complete)

Pairing symbol Action: First enter the pairing symbol and then execute the following command

What's changed: Ci "'" ({< "

Delete content: Di "'" ({[<]

Copy content: Yi "'" ({[<]

Selected content: VI "'" ({< "


Enter a number in the command mode, enter the input character in the insert mode, ESC ... #重复n次输入

# Y|d|gu|gu #如0y $ represents a copy from the beginning of the bank to the end, GU to uppercase, GU to lowercase

U Undo Ctrl R Redo #u撤销 # times

U undo all changes to this row after the cursor falls on this line

Insert mode: VI is run, usually in command mode, type the following command to get VI out of command mode, into insert mode (command mode, and insert mode):


I (i) the beginning of the line with the current position, the end of a (a) and the current position, O (o) upstream and downstream

S #删除一行并进入insert模式

S #删除当前光标字母进入insert模式

Number S (Arrow h, J, K, L) Delete the # letters after the cursor

CC #删除一行并进入insert模式

Change character: C ($ ^ 0 C W E B ({)} GG 1G G) Digital C (Arrow, H, J, K, L) #删除并进入插入模式

Extended mode (external command): Ex file or vi-e directly into ex mode. You can save or exit VI, or set up an editing environment, such as searching for a string, listing line numbers ... However, in general, we use the VI to simplify the two mode, that is, the extension mode (external mode) is also counted in the command line mode (commands mode)


: Wq:x ZZ

: R file

: w filename #另存为

!command #执行命令

R!command #读入命令的结果

: # #第几行

"Address delimitation"

Start_pos,end_pos

How to use: followed by an edit command D, y, W newfile (>> append to file), R file

# #具体行

#,+# #从前面的 # line plus # line range

. #当前行

% #全文, equivalent to 1,$

/pattern/,/pattern/#模式匹配, lines from the first match to PATTERN2

#,/pattern/#模式匹配, from # line to the line to which pattern is matched

/pattern/,$ #从/pattern/to the end of the text.

Find

/pattern #向下查找 N N

/?pattern #向上查找 N N

"Find and Replace"

s/find/replace/modifier

I: Ignore case

G: Global substitution, the default is to replace the first occurrence of each line

GC: Global substitution, hint

@, #代替分割符/, avoid using escape multiple times

Vim Registers


A-z,a-z Register, 0-9 Digital Register, "unnamed register (default register when not specified),-Short Delete register


650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/89/C6/wKioL1gcO2uT8HnnAAHPLwtQv1I902.png-wh_500x0-wm_3 -wmp_4-s_1922281892.png "title=" 1.png "alt=" Wkiol1gco2ut8hnnaahplwtqv1i902.png-wh_50 "/>

Name Register: # "F yy means copy # line to F register, while" FP means read paste; Extended mode: #put F place F Register contents to line 5th

Digital registers, 0 stores the most recently copied content, and 1 stores the most recent deletions. When new text is changed and deleted, 1 dumps to 2, 2 to 3 ... Different sessions cannot share content

View register contents in extended mode: Register F #显示寄存器内容


650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/89/C9/wKiom1gcO7qju8ZrAABLglI1rFo356.png-wh_500x0-wm_3 -wmp_4-s_1532340674.png "title=" 1.png "alt=" Wkiom1gco7qju8zraablgli1rfo356.png-wh_50 "/>


Vim Edit binary file


Vim-b Binfile #打开二进制文件

In the extended command mode, use:%! Xxd convert open 2 binary to 16 readable file, after modification:%!xxd-r return and exit

Visualization mode


V #字符可视化模式 (characterwise visual mode), the selected unit is the character

GV #选中上次可视化模式时选择的文本

V #行可视化模式 (linewise visual mode), the selected unit is the line

Ctrl V #块可视化模式 (blockwise visual mode), select Unit is block

Toggle Visualization Mode: V or V or ctrl v in command mode to enter visualization mode, CTRL V in Visual mode to enter block visualization, ESC to wait 1 seconds to exit

Edit in Visual mode: Delete, copy, ~ case transform, > Add indent, J Merge to Row, W NewFile Save As, sort sort in visual mode

Vim Multiple windows


Vim file1 file2 file3 Multi-file mode

: Next:p Rev:first:last Ctrl ^

: Wall:qall:wqall

Multiple file segmentation


Vim-(o) o file1 file2 ...

CTRL W + arrow

Single File segmentation


CTRL W+s #水平分割多次

CTRL W+v #垂直分割多次

CTRL w+q #取消相邻的, CTRL w+o Cancel all other

Vim configuration file


Personal: ~/.VIMRC #默认不存在的配置文件, automatically loaded when Vim is turned on

Global:/ETC/VIMRC

Set the working characteristics of vim

Line No.: Set "Nu (number), Nonu"

Bracket matching: Set "SM (Showmatch), NOSM"

Auto indent: Set "Ai,noai"

Highlight search: Set "Hlsearch,nohlsearch", Cancel can also Noh

Syntax highlighting: "Syntax on,syntax off"

Ignore case: Set "Ic,noic"

File format: "Fileformat=nuix,fileformat=dos"

Text width: Set "textwidth=65,wrapmargin=15"

Auto indent width: Set "shiftwidth=4"

Spell check: Set "Spell,nospell"

: H (Help)

: Set (Set All) #全部set topic

: H Topic

Vimtutor #tutor



Linux restudying Nine

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.