DAY8: Text Processing tool sed and vim

Source: Internet
Author: User
Tags line editor

August 8, the main learning contents are:

First, the Text Processing tool SED

Second, VIM editor

First, the Text Processing tool SED

1) Introduction

    • Sed is a stream editor that processes a single line of content at a time. When processing, the currently processed rows are stored in a temporary buffer called pattern space, followed by the SED command to process the contents of the buffer, and after processing is done, the contents of the buffer are sent to the screen. Then the next line is processed, so it repeats until the end of the file. The file content does not change unless you use redirection to store the output. SED is mainly used to automatically edit one or more files, simplify the repeated operation of the file, write the conversion program and so on.

    • Usage: sed [option] ... ' Script ' Inputfile ...

    • Common options:
      -N: Automatic printing of non-output mode space content

      -e: Multi-point editing

      -f/path/to/script_file: Reading the edit script from the specified file

      -R: Supports the use of extended regular expressions

      -I: In situ editing

      -i.bak: Back up the files before editing

    • Script: ' address command '


2) Address delimitation

    • Do not give address: the full text of the processing

    • Single Address:

      #: The specified row

      /pattern/: Each row to which the pattern can be matched

    • Address range

      #,#

      #,+#

      /pat1/,/pat2/matches rows to PAT1 for the first time until the first match to the PAT2 line

      #,/pat1/

      $: Last line

    • ~: Step (add as many lines each time)

      Odd lines

      2~2 even rows


3) Edit Command

D: Delete the line matching the pattern space

P: Display the contents of the mode space

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

I \text: inserting text in front of the line; Support for multi-row 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

=: Prints line numbers for lines in pattern space

!: matching row in pattern space reverse processing

s///: Find replacement, support use of other separators, [email protected]@@,s### (default to replace only the first of each line)

Replace tag (placed last):

g: In-line global substitution

P: Show the rows that were successfully replaced

W/path/to/somefile: Saving a successful replacement row to a file


4) Example

Sed ' 2p '/etc/passwd

Sed–n ' 2p '/etc/passwd

Sed–n ' 1,4p '/etc/passwd

Sed–n '/root/p '/etc/passwd

Sed–n ' 2,/root/p '/etc/passwd starting from 2 lines

Sed-n '/^$/= ' file displays empty line line numbers

Sed–n–e '/^$/p ' –e '/^$/= ' file

Sed '/root/a\superman '/etc/passwd after line (root on both sides to add/)

Sed '/root/i\superman '/etc/passwd before line

Sed '/root/c\superman '/etc/passwd instead of line


Sed '/^$/d ' file

Sed ' 1,10d ' file

nl/etc/passwd | Sed ' 2,5d '

nl/etc/passwd | Sed ' 2a tea '

Sed ' s/test/mytest/g ' example

Sed–n ' s/root/&superman/p '/etc/passwd after the word

Sed–n ' s/root/superman&/p '/etc/passwd words before

Sed-e ' s/dog/cat/'-e ' s/hi/lo/' pets

Sed–i.bak ' s/dog/cat/g ' pets


5) Advanced Editing commands

H: Overwrite the contents of the pattern space in the hold space

H: Append the contents of the pattern space to the hold space

G: Remove data from hold space to pattern space

G: Append content from hold space to mode space

x: Swap the content in the pattern space with the content in the hold space

N: Reads the next line of the matched row to overwrite the pattern space

N: Append the next line of matching rows to the mode space

D: Delete rows in the pattern space

D: Delete all rows in multi-line mode space

Example:

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

Sed ' 1! G;h;$!d ' file contents in reverse order

Sed-n ' 1! g;h; $p ' file to display the contents in reverse order

Sed ' $! n;$! D ' FILE takes out the last two lines

Sed ' $!d ' FILE takes out last line

Sed ' G ' FILE adds a blank line after each original line

Sed ' g ' FILE each row is displayed in a blank line

Sed '/^$/d; G ' FILE deletes the original blank line and adds a blank line after the non-blank line

Sed ' n;d ' FILE displays odd rows



Second, VIM editor

1) Text editor

Vi:visual Interface, Text editor
Text: Ascii,unicode

Type of text editing:

Line Editor: SED

Full Screen Editor: Nano,vi

Vim=vi impromved


2) Open File

# vim [OPTION] ... FILE ...

+#: After opening the file, just leave the cursor at the beginning of line #

+/pattern: Immediately after opening the file, leave the cursor at the beginning of the first line that is matched to the PATTERN

Vim + file opens file directly, the cursor is in the last line

VIM–B file binary mode open files

Vim–d file1 file2 ... Compare multiple Files

If the file exists, the file is opened and the contents are displayed if the file does not exist, it is created when you save it for the first time after editing


3) Edit mode

    • Three main modes:

Command mode (default): Move cursor, cut/paste text

Insert (Edit) mode: Modify text

Extended Command mode: Save, exit, etc.

    • ESC key to exit the current mode

      ESC key always returns to command mode

4) mode conversion

    • Command mode---insert mode
      I:insert, enter at the cursor position

      I: Input at the beginning of the line where the current cursor is located

      A:append, enter after cursor location

      A: Enter at the end of the line at the current cursor

      O: Opens a new line below the line where the current cursor is located

      O: Opens a new line above the line where the current cursor is located

    • Insert mode----> Command mode ESC

    • Command mode----> Extended mode:

    • Extended mode----> Command mode ESC

    • Locking and unlocking: ctrl+s,ctrl+q


5) Close the file

    • Extended mode

      : Q exit

      : q! Force exit, discard the modifications made

      : Wq Save Exit

      : X Save exit

    • Command mode

      ZZ: Save exit

      ZQ: Do not save exit


6) Extended mode (ex mode)

    • Press ":" in command mode to enter ex mode

    • Command

      W Write (Save) disk File

      Wq Write and exit

      X Write and exit

      Q exit

      Q! Do not save the log out, even if the changes are lost

      R filename reads the contents of the file into the current file

      W FileName writes the current file contents to another file

      !command temporarily switch from vim interface to view command run result interface

      R!command to write command run results into the file being edited


7) Command mode cursor jump

  • Jump between characters:

    H: Left L: Right J: Next K: Upper #COMMAND: Jumps the number of characters specified by #

  • Jump between words:

    W: The first word of the next word =ctrl+ right

    E: The ending of the current or next word

    B: The first word of the current or previous word =ctrl+ left #COMMAND: Specifies the number of words to jump by #

  • Current page Jump: H: Top M: Page Middle row L: Bottom of page

  • Line End Jump: ^: Jump to the beginning of the first non-whitespace character; 0: Jump to the beginning of the line $: jump to the end of a row

  • Inline move: #G, (Extended mode: #): Jump to the specified line by # G: last line 1G, GG: First line

  • Move between sentences:): Next sentence (: previous sentence)

  • Move between paragraphs:}: Next paragraph {: Previous paragraph

  • Command mode Flip screen operation
    CTRL+F: Flip a screen to the end of the file

    CTRL+B: Flip a screen to the file header

    Ctrl+d: Turn half screen at the end of the file

    Ctrl+u: Turn half screen to file header


8) Command mode operation

  • Character editing:

    X: Delete the character at the cursor;

    #x: Delete the # characters at the beginning of the cursor

    XP: Swap the position of the character where the cursor is located and the character after it

    ~: Convert Case

  • Replace command (R, replace) R: replaces the character at which the cursor is located

  • Delete command:

    D: Delete command, can be combined with the cursor jump character, to achieve range deletion

    d$: Delete to end of line

    d^: Delete to non-empty header (d function same)

    D0: Delete to the beginning of the line

    DW:DE:DB:

    #COMMAND

    DD: Delete the line where the cursor is located #dd: Multi-line Delete

  • Copy command (y, yank):

    Y: Copy, behaves similar to D command
    YY: Copy line #yy: Copy Multiple lines

    Y: Copy Entire row

  • Paste command (P, paste):

    P: If the buffer is an entire row, paste the current cursor below the row, or paste it at the end of the current cursor.

    P: If the buffer is an entire row, the current cursor is pasted above the row, otherwise, it is pasted to the front of the current cursor position.

  • Changing the command (c, change) C: Modify and switch to insert mode (first D again:)

  • GU to uppercase, GU to lowercase, full line


9) Extended Mode

  • Geological delimitation (followed by an edit command D, y)

    : Start_pos,end_pos
    #: The specific # line, for example, 2 means line 2nd

    #,#: From the left # indicates the line start, to the right # indicates the end of the line

    #,+#: The start of the line from the left #, plus the number of rows on the right #: 2,+3 represents 2 to 5 rows

    .: When moving forward

    $: Last line

    ., $-1 current line to penultimate line

    %: Full text, equivalent to 1,$

    /pat1/,/pat2/ starts from the first line that is matched to the pat1 pattern, until the end of the line that was first matched to by the PAT2
    #,/pat/

    /pat/,$
    W File: Save a range of rows to a specified file R file: Inserts all content from the specified file at the specified location

  • Find

    /pattern: Looks at the end of the file from the current cursor location

    ? PATTERN: Finds the file header from the current cursor location

    N: With the command in the same direction N: With the command inverse direction

  • Find and replace (by default, each line replaces only the first occurrence)

    S: Complete the Find and replace operation in extended mode

    Format: s/What to look for/replace with content/modifiers

    What to look for: Available modes

    Replace with: cannot use mode, but can use \1, \2, ... You can also use the "&" reference to find the entire contents of the previous lookup

    Modifier: I: Ignoring case, G: global substitution; GC: global substitution, asking before each replacement

    Find the delimiter in the replacement/can be replaced with other characters, such as [email protected]/[email protected]/[email protected] s#/boot#/#i


10) Other

  • Undo Changes

    U Undo Recent Changes

    #u撤销之前多次更改

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

    Press Ctrl-r (cancel the undo action just now)

    . Repeat previous action

  • Editing a binary file
    Open file in binary mode vim–b binaryfile

    In extended command mode, use the XXD command to convert to a readable 16 binary:%!xxd
    Conversion back to binary:%!xxd–r

  • Visualization mode
    V line-oriented for character V

  • multi-window mode

    vim-o|-o FILE1 FILE2 ... -O: Split horizontally-o: Vertical split between Windows: Ctrl+w again

    single file window split:
    Ctrl+w,s:split, split horizontally

    ctrl+w,q: Cancel adjacent window

    :wqall exit

  • Configure VIM

    Configuration file: Permanently valid

    Global:/ETC/VIMRC Personal: ~/.VIMRC

    Extended mode: Current VIM process is valid
    (1) Line No.: Display: Set number, abbreviated set NU Suppress display: Set Nonu
    (2) pairs of parentheses match: set SM

    (3) Auto indent enabled: Set AI

    (4) Highlight search enabled: Set Hlsearch shorthand: Set HL

    (5) syntax highlighting enabled: syntax on
    (6) The case of ignoring characters is enabled: Set IC
    (7) file format enabled: Fileformat=unix not ignored: Fileformat=dos


11) Help

Vi/vim built-in Help

: Help

: Help Topic

Use:q to exit Help


This article is from the "Laugh Monkey" blog, please be sure to keep this source http://xiaomonky.blog.51cto.com/11869371/1836683

DAY8: Text Processing tool sed and vim

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.