The use of SED and vim

Source: Internet
Author: User
Tags uuid

Tools for working with text the SED of the Three Musketeers


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, etc.


The use of SED


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: Edit script in situ: ' Address command '


There are several ways to define an address:


(1) Not to address: The full text of the processing

(2) Single address: #: Specified line/pattern/: Each row that the pattern can match to

(3) Address range: #,# #,+#/pat1/,/pat2/#,/pat1/

(4) ~: Step up or down odd lines 2~2 even lines


Address delimitation for Extended mode


: Start_pos,end_pos

#: The specific # line, for example, 2 means line 2nd;

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

+#: The start of the line from the left #, plus the number of rows on the right # representation

: 2,+3 = 2 to 5 rows

.: Current line $: last row

., $-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 is first matched to the PAT2

#,/pat/

/pat/,$

How to use: followed by an edit command

D

Y

W File: Save a range of rows to a specified file

R file: Inserts all content from the specified file at the specified location



The edit command options for SED are:


D: Delete the line matching the pattern space

P: Display the contents of the mode space

A:\text: Append text after line, support multi-row append using \ n

I: \text: Inserting text in front of 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


Find and replace mode for SED


s///: Find replacement, support using other separators, [email protected]@@,s### replacement Tag:


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


Sed 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 the contents of the current mode space beginning to \ n (not passed to standard output), discard the following command, but re-execute sed on the remaining mode space


Some examples of SED advanced editing commands


Sed-n ' N;p ' FILE

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

Sed ' $! n;$! D ' FILE

Sed ' $!d ' FILE

Sed ' G ' FILE

Sed ' g ' FILE

Sed '/^$/d; G ' FILE

Sed ' n;d ' FILE

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



Vim Text Editor


The features and difficulties of Vim lie in the conversion between three modes and the command options in three modes


Vim's three main modes: Command mode (default): Move cursor, cut/paste text Insert (edit) mode: Modify text extension command mode: Save, exit, etc.


The switch between the three modes is


Insert mode----> Command mode ESC

Command mode----> Extended mode:

Extended mode----> Command mode ESC

Locking and unlocking: ctrl+s,ctrl+q


First mode of command


# 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

Vim-m file read-only open files

Ex file or vim–e directly into ex mode if the file exists, the file is opened and the content is displayed if the file does not exist, it is created when you save it for the first time after editing


Second insert mode


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


The third type of Extended command mode


Press ":" to enter the EX mode to create a command prompt: At the bottom of the screen on the left

Command:

W Write (Save) disk File

Wq 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 R!command


Cursor Jump in command mode

Jump between characters:

H: Left

L: Right

J: Next

K: Up

#COMMAND: Jumps the number of characters specified by #;

Jump between words:

W: The first word of the next word

E: The ending of the current or next word

B: The first word of the current or previous word

#COMMAND: Specifies the number of words to jump by # at one time

Current page Jump:

H: Top of page

M: Middle row of page

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 end of line

Inter-row movement: #G,

Extension mode: #: Jump to line specified by #

G: Last line

1G, GG: First line

Move between sentences:

): Next sentence

(: The previous sentence

Move between paragraphs:

}: Next Paragraph

{: Last paragraph


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


Close 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


Paste Copy Delete substitution modification under Command mode operation


Deletion and substitution of character edits


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


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

D0: Delete to the beginning of the DW:

De

DB: #COMMAND

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

D: Always delete from the current cursor position to the end of the line, leaving blank lines, equivalent to d$

Replace command (R, replace)

R: the character at which the cursor is replaced

R: Switch to replace mode


Edit the Copy command


Copy command (y, yank):

Y: Copy, behaves similar to D command

y$

Y0

y^

Ye

yw

Yb

#COMMAND

YY: Copying rows

#yy: Copying Multiple lines

Y: Copy Entire row


Edit the Paste


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 at the end of P: If the buffer is a whole row, paste the current cursor over the row, or paste it to the front of the current cursor where it is located.


Edit the changes


changing commands (c, change)


C: Switch to insert mode after modification

Command mode---insert mode

C $

c^

C0

Cb

Ce

cw

#COMMAND

CC: Delete the current line and enter new content

#cc:

C: Delete the current cursor to the end of the line and switch to insert mode


Command:y Copy, d Delete, GU to uppercase, GU to lowercase



Find and Replace in extended mode



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: Same direction as command

N: Opposite direction of command


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: Ignore case

G: global substitution; By default, each row replaces only the first occurrence of the

GC: Global substitution, asking for the delimiter in the find substitution before each substitution/replaceable with other characters,

For example [email protected]/[email protected]/[email protected] s#/boot#/#i


Undo and change of command mode


1 U undo Recent Changes

2 #u撤销之前多次更改

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

4 Press Ctrl-r to redo the final undo change

5. Repeat previous action

6 N. Repeat the previous action n times



Vim Registers


There are 26 named registers and 1 unnamed registers, which often contain different content of the Clipboard, which can be shared between different sessions. With a A, B,..., Z, with "register representation, placed between the number and the command, such as: 3" tyy means to copy 3 rows to the T register "TP means to paste the T register content is unspecified, will use the unnamed register has 10 digital registers, with 0,1,...,9, 0 to store the most recently copied content, 1 Store recently deleted content. When new text changes and deletions, 1 dumps to 2, 2 dumps to 3, and so on. Digital registers cannot be shared between different sessions.


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

Editing a binary file

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

Save exit



The visual mode of VIM


Allow selection of text blocks

V Character-oriented

V Line-oriented

CTRL-V Block-oriented

Visualization keys can be used in conjunction with the move key: W,),}, arrows, etc.

Highlighted text can be deleted, copied, changed, filtered, searched/replaced, etc.


Multi-file Multi-window mode


Vim FILE1 FILE2 FILE3 ...

: Next Next

:p Rev A previous

: First One

: Last One

: Wall Save All

: Qall Quit all

: Wqall


Multi-file Segmentation vim-o|-o FILE1 FILE2 ...

1-o: Horizontal Split

2-o: Vertical Split Switch between windows: Ctrl+w, Arrow


Single File Window segmentation:

1 Ctrl+w,s:split, Horizontal split

2 ctrl+w,v:vertical, Vertical split

3 ctrl+w,q: Cancel adjacent window

4 ctrl+w,o: Cancel All windows

5:wqall exit


Vim's private tailor-made


Configuration file: Permanently valid

Global:/ETC/VIMRC

Personal: ~/.VIMRC

Extended mode: Current VIM process is valid

(1) The line number is displayed: Set numbers, abbreviated set NU Suppress: Set nonumber, shorthand for set Nonu

(2) parentheses match match: Set Showmatch, abbreviated set SM Cancel: Set NOSM

(3) Auto indent enabled: Set AI Disabled: set Noai

(4) Highlight search enabled: Set hlsearch disabled: Set Nohlsearch

(5) syntax highlighting enabled: syntax on disabled: syntax off

(6) The case of ignoring characters is enabled: Set IC is not ignored: set Noic

(7) file format enabled: Fileformat=unix do not ignore: Fileformat=dos (8) Set text width: Set textwidth=65 (VIM only): Set Wrapmargin=15:help Option-li St:set Or:set All


Vim's built-in Help documentation


Vi/vim built-in Help: Help:help topic use:q to exit assist Vimtutor





Homework


1. Delete all whitespace characters from the beginning of lines in the/etc/grub2.conf file that begin with whitespace


1 sed ' s/^[[:space:]]+//'/etc/grub.conf

1 Computer Demo

[[Email protected] tmp]# sed ' s/^[[:space:]]\+//'/etc/grub.conf

# grub.conf generated by Anaconda

#

# Note that you don't have the to rerun grub after making changes to the This file

# notice:you has a/boot partition. This means

# all kernel and INITRD paths is relative to/boot/, eg.

# root (hd0,0)

# kernel/vmlinuz-version RO root=/dev/sda2

# initrd/initrd-[generic-]version.img

#boot =/DEV/SDA

Default=0

Timeout=5

Splashimage= (hd0,0)/grub/splash.xpm.gz

Hiddenmenu

Title CentOS 6 (2.6.32-642.el6.x86_64)

Root (hd0,0)

kernel/vmlinuz-2.6.32-642.el6.x86_64 ro root=uuid=d496e55b-f1c4-4267-b357-cdd06fa77352 Rd_NO_LUKS rd_NO_LVM LANG=en _us. UTF-8 rd_no_md sysfont=latarcyrheb-sun16 crashkernel=auto keyboardtype=pc keytable=us Rd_NO_DM rhgb quiet

Initrd/initramfs-2.6.32-642.el6.x86_64.img

[Email protected] tmp]#






2. Delete all # and white space characters at the beginning of the line beginning with #, followed by at least one white-space character, in the/etc/fstab file

1 sed ' s/^#[[:space:]]\+//'/etc/fstab

2 Sed-r ' s/^ (#|[ [: space:]] +)//'/etc/fstab


1 Computer Demo

[[Email protected] tmp]# sed ' s/^#[[:space:]]\+//'/etc/fstab


#

/etc/fstab

Created by Anaconda on Tue Jul 19 20:14:32 2016

#

Accessible filesystems, by reference, is maintained under '/dev/disk '

See Mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info

#

UUID=D496E55B-F1C4-4267-B357-CDD06FA77352/EXT4 Defaults 1 1

Uuid=a142fc0a-a404-49c7-a4a2-494c867ec71c/boot EXT4 Defaults 1 2

Uuid=cd32c025-5104-4670-bab4-e5db886ed7b9/testdir EXT4 Defaults 1 2

uuid=84187402-904e-4673-81cd-3228183a2437 swap swap defaults 0 0

TMPFS/DEV/SHM TMPFS Defaults 0 0

Devpts/dev/pts devpts gid=5,mode=620 0 0

Sysfs/sys Sysfs Defaults 0 0

PROC/PROC proc Defaults 0 0

/dev/cdrom/mnt iso9660 Defaults 0 0

[Email protected] tmp]#





3. Add # At the beginning of each line of/root/install.log

1 sed ' s/^/#&/'/root/install.log


1 Computer Demo

[[Email protected] tmp]# sed ' s/^/#&/'/root/install.log

#{

#333

#anaconda-ks.cfg

#Desktop

#Documents

#Downloads

#f1

#f22

#f99

#f [A,b,c,d,e,f]

#history. Log

#install. Log

#install. Log.syslog

#Music

#nusers

#Pictures

#Public

#Templates

#Videos

[Email protected] tmp]#







4. Add # to the beginning of the line in the/etc/fstab file that does not begin with #

1 sed ' s/^[^#]/#&/'/etc/fstab

1 Computer Demo

[[Email protected] tmp]# sed ' s/^[^#]/#&/'/etc/fstab


#

#/etc/fstab

# Created by Anaconda on Tue Jul 19 20:14:32 2016

#

# Accessible filesystems, by reference, is maintained under '/dev/disk '

# See mans Pages Fstab (5), Findfs (8), mount (8) and/or Blkid (8) for more info

#

#UUID =D496E55B-F1C4-4267-B357-CDD06FA77352/EXT4 Defaults 1 1

#UUID =a142fc0a-a404-49c7-a4a2-494c867ec71c/boot EXT4 Defaults 1 2

#UUID =cd32c025-5104-4670-bab4-e5db886ed7b9/testdir EXT4 Defaults 1 2

#UUID =84187402-904e-4673-81cd-3228183a2437 Swap swap defaults 0 0

#tmpfs/dev/shm TMPFS defaults 0 0

#devpts/dev/pts devpts gid=5,mode=620 0 0

#sysfs/sys Sysfs Defaults 0 0

#proc/proc proc Defaults 0 0

#/dev/cdrom/mnt iso9660 Defaults 0 0

[Email protected] tmp]#






5, processing/etc/fstab path, use sed command to take out its directory name and base name

1 echo "/etc/sysconfig/network-scripts/" | Sed-r ' [email protected] (. */) ([^/]+/?) [Email protected]\[email protected] '


2 echo "/ETC/FST/SD" | Sed-r ' [email protected] (. *)/([^/]+/?) @\[email protected] '


3 echo "/ETC/FST/SD" | Sed-r ' [email protected] (. */) ([^/]+/?) [Email protected]\[email protected] '


3 Computer Demo

[Email protected] tmp]# echo "/ETC/FST/SD" | Sed-r ' [email protected] (. */) ([^/]+/?) [Email protected]\[email protected] '

/etc/fst/

[Email protected] tmp]# echo "/ETC/FST/SD" | Sed-r ' [email protected] (. */) ([^/]+/?) [Email protected]\[email protected] '

Sd

[Email protected] tmp]#







6. Use SED to remove the IPv4 address of the ifconfig command

1 Ifconfig | Sed ' 2!d ' | Sed-r ' [Email protected]*inet.addr: (. *) B.*@\[email protected] ' [! reverse]


1 Computer Demo

[Email protected] tmp]# Ifconfig | Sed ' 2!d ' | Sed-r ' [Email protected]*inet.addr: (. *) B.*@\[email protected] '

172.18.16.71

[Email protected] tmp]#





7. Statistics of all RPM files in the package directory on the CentOS installation CD. Number of repetitions of the second-to-last field


1 ls/mnt/packages/* | Sed-r ' [email protected]*\. (. *) \[email protected]\[email protected] ' | Sort | Uniq-c


2 Ls/run/media/root/centos |sed ' [email protected]\[email protected]@ ' | Sed ' [email protected]*\[email protected]@ ' |sort|uniq-c


3 ls *.rpm |rev|cut-d.-f2 |rev|sort|uniq-c


1 Computer Demo


[Email protected] tmp]# ls/mnt/packages/* | Sed-r ' [email protected]*\. (. *) \[email protected]\[email protected] ' | Sort | Uniq-c

4 i686

1/mnt/packages/trans. TBL

919 Noarch

2283 x86_64

[Email protected] tmp]#


This article from "11892658" blog, declined reprint!

The use of 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.