What's new in file content vim
Modal editor, full screen editor
Centos7 Installing VIM:yum -y install vim
Three different modes
- 1, the editing mode, open vim after the default mode, no eggs;
- 2, insert mode, directly can modify the text content;
3, the last line mode, you can use the command to manipulate the text content:
:10d 删除第10行:10,20d 删除第10行-20行
transitions between modes :
VIM related Operations :
1. Open File:
vim file 普通打开文件vim +n file 打开文件且光标位于第n行首部vim + file 打开文件并编辑最后一行vim +/pattren 打开文件并定位第一次被模式匹配到的行的行首
- 2. Close the file
To close a file in the last-line mode:
:q 不保存退出:wq 保存退出:q! 强制退出:w 保存退出:w! 强行保存:wq! 强行保存并退出
Exit in edit mode: ZZ
Save and exit
3. Move the cursor (in edit mode)
# 逐字符移动: 箭头# 按单词为单位移动 w # 移至下一单词的词首 e # 移至当前或下一个单词的词尾 b # 移至当前或前一个单词的词首# 行内跳转 0 # 绝对行首 ^ # 行首的第一个非空白字符 $ # 绝对行尾,包含空白字符# 行间跳转 nG # 直接跳转至第 n 行行首 G # 跳转至最后一行行首 # 在末行模式下,直接给出行号即可# 翻屏操作(编辑模式) ctrl+f 向下 ctrl+b 向上
4. Delete (character, Word, line):
# 命令行删除单个字符:x # 删除光标所在处的单个字符nx # 删除光标所在处以及向后的共n个字符 # 编辑模式下,跟跳转命令结合使用 dw # 删除当前单词 delete word dd # 删除当前所在行 ndd # 往下删除当前所在行在内的n行# 末行模式: :2,4d # 删除第2-4行 :2,+5 # 删除第2行以及随后的5行 $d 删除最后一行 .d 删除当前行 $-1d 删除倒数第二行
5. Copy command (in edit mode):
yy 复制整行
6. Paste Command p
:
If the copied or deleted content is the whole line, paste it below the row;
The copied or deleted content is a non-whole line, then pasted to the character after the cursor;
7. Undo the edit operation:
u # 撤销前一次的编辑操作,可以连续使用n次nu # 撤销最近n次的编辑操作ctrl+r # 撤销最近一次的撤销操作
8, repeat the previous edit operation:.
9. Find:
/pattren?pattren
10. Find and replace (in last line mode)
n,ms/ad/AD/g # 在n-m行,将ad替换成AD1,3s/hello/hi/g # 在1-3行中将hello字符串替换为hi
Echo
REDIRECT has related introduction
Append a line to the end of the file:echo asasas >> file
Empty the original file and write the contents:echo asasas > file
Append multiple lines of content:
cat >> 3.txt << EOF> q> w> e> rt> EOF
View Content Cat
View all content
-n
Show line Numbers
-c
Shown in bytes, for example-c3 display first three bytes (seemingly nonexistent)
--help
See introduction, similar to man, but contains Chinese
Head
By default, the first 10 rows are viewed, and you can specify the number of rows to display.
Tail
You can specify the number of rows to display by default when you view the following 10 rows.
Sed
- Stream editor for filtering and transforming text.
By default, the original file is not edited, only the schema space data is processed.
format :sed ‘AddressCommands‘ file ...
- Options:
-n
: Silent mode, does not display the contents of the mode space
-i
: Modify the original file directly
-e
: Multiple scripts can be executed at the same time
-f
: Save to file /path/to/script
, sed -f /path/to/script file
run script in specified file
-r
: Indicates the use of an extended regular expression
Address: Used in conjunction with the following commands
# 1、Startline、Endline 例如: 1,100 $ 表示最后一行 $-1 倒数第二行# 2、/RegExp/ /^root/ # 表示以root字符串开始的行 sed '/3/d' file # 不显示包含3的行 sed -n '/a/p' 1.txt # 显示包含字符a的行# 3、/pattren1/,/pattren2/,正则匹配 # 第一次被pattern1匹配的行开始,到第一次被pattern2匹配到的行结束中间的所有行。 sed -n /hi/,/a/p 1.txt # 显示从第一次匹配到字符 hi 和字符 a 以及之间所有行# 4、指定行 sed -n 'mp' 1.txt # 显示第m行 [[email protected] ~]# sed -n 2p 3.txt b# 5、Startline, +N # 从startline开始,向后的N行 sed '1,+3d' file # 删除1-4行 [[email protected] ~]# sed 1,+2d 3.txt # 输出删除后剩余的所有内容 d e f g h [[email protected] ~]# cat 3.txt a b c d e f g h
Commands
# 1, d delete (not show) qualifying line sed 1,2d file # shows other rows except 1-2 rows # 2, p displays rows that match the criteria, combined with-n use, only shows qualifying rows sed-n '/^a/p ' 1.txt # display with a All lines beginning with # 3, Na\string appends a new line after the next line of the specified line after sed ' 3a\hahaha ' 1.txt # Appends a line to the third row with the HAHAHA string # 4, and ni\string appends a new line to the previous line of the specified row # 5. R file adds the contents of the specified file to the qualifying line at SED ' nr demo1.sh ' F1.txt # reads all the contents of the demo1.sh, placing it after the nth line of F1.txt # 6, W file will refer to The contents of the file are added to the specified file, the target file will be emptied. Sed '/a/w f2.txt ' F1.txt # writes the line containing the character a in f1.txt to the file F2.txt # 7, s/pattren/string/sed ' s/a /a/' F1.txt # replaces character A with the character a sed ' s/.*a.*/a/' f1.txt # will contain a line substitution character A-g # global substitution, as long as the match replaces SED ' s/a /a/g ' F1.txt # replaces all a in each line with a for a-i # ignoring case the SED s/h/y/i 3.txt # delimiter can be s###. [email protected]@@ # do not use the character you want to replace as a delimiter & # Reference to the string previously matched to: sed ' s/a/&r/g ' f1.txt # after match to the a character Append r character sed ' s#l\ (.. e\) #L \1#g ' F1.txt # replaces only two characters in the middle of E with L, example hello,my love Hello, Linux >>> Hello,my Love Hello, Linux
Awk
Displays one or more lines, pattern scanning and processing language
awk 'NR==n' 3.txt # 显示第 n 行内容awk "NR==n, NR==m" 3.txt # 显示 n-m 行的内容
Grep
- Global Research searches the text based on the pattern and displays the lines of text that conform to the pattern, partially matching.
Patterns a matching condition that consists of a combination of meta-characters of the text wildcards regular the expression
- Options:
-i
Ignore case
--color
Matching content highlighting
-v
Reverse Lookup
-o
Show only strings that match to
grep 'ab' 1.txt # 查看包含 字符 ab 的所有行grep -v '2' 1.txt # 列出所有不包含 字符 2 的行
Regular Expressions :
. # 匹配任意单个字符* # 匹配其前面的字符任意次 .* # 匹配任意多个任意字符\? # 匹配前一个字符0或1次\{n, m\} # 匹配前面的字符至少n次,之多m次^ # 字符出现在行首$ # 字符出现再行尾^$ # 空白行[] # 指定集合内的任意一个字符[^] # 非[:digit:] # 同通配符使用 grep '[[:digit:]]' 1.txt # 匹配包含数字的行
Cut
The contents of the corresponding column of each row are taken at the specified location after splitting.
-d
: Specify delimiter, default is a space
-f
: Specify the fields to display
[[email protected] ~]# cat 3.txt sdasdaa 12as asdasdvvc 12as asdasdsd 12a asdasdf[[email protected] ~]# cut -d' ' -f1 3.txt # 按照空格分割,显示每行的第一个字段sdasdaavcdcut -d' ' -f1,2 1.txt # 每行的第1个和第2个字段cut -d' ' -f1-3 1.txt # 显示每行的第 1-3 个字段
Sort
- View file contents and sort by row
- By default, the decimal digit size in ASCII corresponds to the beginning of the line in ascending order
- Options:
-n
Sort by numeric size
-r
Descending
-t
Specify delimiter
-k
Specify a field,sort -t: -k3 -n /ect/passwd
-u
Duplicate rows are displayed only once
-f
Case insensitive
Uniq
Adjacent and identical rows are displayed only once
[[email protected] ~]# cat 3.txt abbaaaabbcccc
-d
Show duplicate rows, show only once
[[email protected] ~]# uniq -d 3.txt aacc
-D
Show duplicate rows, how many are displayed
[[email protected] ~]# uniq -D 3.txt aaaacccc
-c
Show duplicate rows and the corresponding number of times
[[email protected] ~]# uniq -c 3.txt 1 a1 bb2 aa1 bb2 cc
Wc
- Statistical text content (line, Word, number of bytes)
- Options:
-l
Number of rows displayed
-w
Display the number of words
-c
Number of bytes displayed
-m
Number of characters
-L
The longest line contains the number of characters
[email protected]:~/data$ wc 1.txt10 18 80 1.txt行 单词 字节
Tr
Translate or delete characters
Displays the replaced character, where the AC refers to the collection rather than a string
tr 'ac' 'bd' < file # 将文件中的字符a替换成字符btr 'a-z' 'A-Z' < file # 将小写换成大写字母tr -d '1' < file # 删除字符1
Linux (5), file content-related commands