Linux vi, vim, sed usage

1. Create a text file under Linux (detailed use of vi/vim command)

vi test.txt/vi filename
or
vim test.txt/vim filename
or
touch test.txt/filename
vim is an upgraded version of vi, with more instructions and stronger functions.
The following is the collected vim usage, when you want to exit in vim, the first thing to do is to press [Esc], and then enter [:wq]
1. General mode: delete, copy and paste commands
x, X x is the direction Delete a character afterward, X is the previous character deleted
nx (n represents a number) Delete n characters backward
dd Delete the current line
D Delete all characters in the current line, try to be a blank line
ndd (n represents a number) Delete the direction of the line where the cursor is located Next n columns
d1G Delete all data from the cursor line to the first line
dG Delete all data from the cursor line to the last line
yy Copy the cursor line
y1G Copy all the data from the cursor line to the first line yG Copy the cursor
line to the end All data in a row
ynj(n stands for number) Copy the line where the cursor is located down n+1 lines
dnj(n stands for numbers) Delete the line where the cursor is located n+1 lines down
p,P p is the copied data and paste it in the next line of the cursor, P is the copy Paste the data in the previous line of
the cursor J Combine the data of the line where the cursor is located and the data of the next line into one line
u Restore the previous action (undo)
2. Edit mode command
i,I i is to insert the entered text at the current cursor position, I To insert the entered text a at the first non-blank character of the line where the cursor is
, A a is to insert the entered text at the next character where the cursor is located, A is to insert the entered text at the next character of the last character of the line where the cursor is located Text
o,O o means to insert a character at the beginning of the next line where the cursor is located, O means to insert a character r at the beginning of the previous line where the cursor is located
, R r means to replace the character where the cursor is located, R means to always replace the cursor The text pointed to, until exit
Esc, exit, return to normal mode
3. Command mode
h Move the cursor to the left by one character
j Move the cursor down one character
k Move the cursor up one character
l The cursor moves one character to the right
Ctrl+f The screen turns down one page
Ctrl+b The screen turns up one page
Ctrl+d The screen turns down half a page
Ctrl+u The screen turns up half a page
+ The cursor moves to the first of the next line non-blank characters
- the cursor moves to the first non-blank character of the current line
n spaces (n represents a number) the cursor moves to the right of the current line n characters
0 (number 0) the cursor moves to the first character of the current line ( It can be a null character, pay attention to distinguish it from -)
$ The cursor moves to the last character of the current line (it can be a null character, pay attention to distinguish it from -) H The cursor moves to the first non-null character M
of the line at the top of the current screen
Move the cursor to the first non-blank character of the middle line of the
current screen L Move the cursor to the first non-blank character of the bottom line of the current screen
G Move the cursor to the first non-blank character of the last line of the article
nG (n stands for number) The cursor moves to the first non-blank character of the nth line of the article
n The cursor moves down from the current line to the first non-blank character of n lines
/word Find the word string after the cursor
?word in Find word string before the cursor
: s/word1/word2/g Find word1 in the current line of the cursor and replace it with word2
:n1,n2s/word1/word2/g Find word1 between lines n1 and n2, and replace into word2
:%s/word1/word2/g Find word1 in the entire article and replace it with word2
:w Save the edited data to a file on the hard disk
: w [filename] Save the edited data to another file on the hard disk
: r [filename] When editing data, read the data in another file, that is, add the content of the filename file to the next line of the line where the cursor is
: wq or: x Save and exit
: q exit, applicable to unmodified files
: q! Forced exit, suitable for modifying the file and exiting without saving
: set nu to display the line number
:set nonu cancel line number
:n1,n2 w [filename] save the contents of lines n1 to n2 to a file named filename
 

2. sed usage of linux

Sed is a very good file processing tool. It is a pipeline command. It is mainly processed in behavioral units. It can perform specific tasks such as replacing, deleting, adding, and selecting data lines. Let's first understand the usage of sed. The
sed command The line format is:
         sed [-nefri] 'command'        
Common options for entering text: -n:
        Use silent mode. In normal sed usage, all data from STDIN is normally listed on the screen. But if you add the -n parameter, only the line (or action) that has been specially processed by sed will be listed.
        -e: Edit sed actions directly in command line mode;
        -f: Write sed actions directly in a file, -f filename can execute sed actions in filename; -r: sed
        actions support The syntax of extended regular notation. (Default is basic regular notation syntax)
        -i: Directly modify the contents of the read file instead of outputting it on the screen.       
Common commands:
        a : Add, a can be followed by a string, and these strings will appear in a new line (the current next line) ~
        c : Replace, c can be followed by a string, and these strings can be replaced The line between n1,n2!
        d : delete, because it is delete, so there is usually no dong dong after d;
         i : insert, after i can be followed by strings, and these strings will appear on a new line (the current previous line);
         p : Print, that is, print out the selected data. Usually p will work with the parameter sed-n ~
         s: replace, you can directly replace the work! Usually the action of this s can be paired with regular notation! For example 1,20s/old/new/g and that's it!
Example: (assuming we have a file named ab)
     delete a line
     [root@localhost ruby] # sed '1d' ab #delete the first line 
     [root@localhost ruby] # sed '$d' ab #delete the last line
     [root @localhost ruby] # sed '1,2d' ab #delete the first line to the second line
     [root@localhost ruby] # sed '2,$d' ab #delete the second line to the last line

  显示某行
.    [root@localhost ruby] # sed -n '1p' ab           #显示第一行 
     [root@localhost ruby] # sed -n '$p' ab           #显示最后一行
     [root@localhost ruby] # sed -n '1,2p' ab        #显示第一行到第二行
     [root@localhost ruby] # sed -n '2,$p' ab        #显示第二行到最后一行

  使用模式进行查询
     [root@localhost ruby] # sed -n '/ruby/p' ab    #查询包括关键字ruby所在所有行
     [root@localhost ruby] # sed -n '/\$/p' ab        #查询包括关键字$所在所有行,使用反斜线\屏蔽特殊含义

  增加一行或多行字符串
     [root@localhost ruby]# cat ab
     Hello!
     ruby is me,welcome to my blog.
     end

     [root@localhost ruby] # sed '1a drink tea' ab  #第一行后增加字符串"drink tea"
     Hello!
     drink tea
     ruby is me,welcome to my blog. 
     end

     [root@localhost ruby] # sed '1,3a drink tea' ab #第一行到第三行后增加字符串"drink tea"
     Hello!
     drink tea
     ruby is me,welcome to my blog.
     drink tea
     end
     drink tea

     [root@localhost ruby] # sed '1a drink tea\nor coffee' ab   #第一行后增加多行,使用换行符\n
     Hello!
     drink tea
     or coffee
     ruby is me,welcome to my blog.
     end

  代替一行或多行
     [root@localhost ruby] # sed '1c Hi' ab                #第一行代替为Hi
     Hi
     ruby is me,welcome to my blog.
     end
     [root@localhost ruby] # sed '1,2c Hi' ab             #第一行到第二行代替为Hi
     Hi
     end

  替换一行中的某部分
  格式:sed 's/要替换的字符串/新的字符串/g'   (要替换的字符串可以用正则表达式)
     [root@localhost ruby] # sed -n '/ruby/p' ab | sed 's/ruby/bird/g'    #替换ruby为bird
   [root@localhost ruby] # sed -n '/ruby/p' ab | sed 's/ruby//g'        #删除ruby
     
插入
     [root@localhost ruby] # sed -i '$a bye' ab         #在文件ab中最后一行直接输入"bye"
     [root@localhost ruby]# cat ab
     Hello!
     ruby is me,welcome to my blog.
     end
     bye

     删除匹配行
      sed -i '/匹配字符串/d'  filename  (注:若匹配字符串是变量,则需要“”,而不是‘’。记得好像是)

      替换匹配行中的某个字符串
      sed -i '/匹配字符串/s/替换源字符串/替换目标字符串/g' filename

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325980200&siteId=291194637