Linux-Beginners Series - Part 7_ Text Editing and Processing Commands

Text Editing and Processing Commands - Contents

Previous: Linux-Beginner Series——Part 6_ System Basic Operation Management

1. System basic editing commands

vi\vim, vim is an enhanced version of the vi command, such as: first line indentation, code highlighting, etc.

The default centos7 system may not have the vim command, you need to install it with the command

Install the vim software toolkit
yum install -y vim
Grammar format:
vim [option] [file]
vim    选项    文件

1. vim editing command mode

Vim has three modes: normal mode, edit mode, and command mode.

01 Normal Mode

After the vim command opens a file, the default state is normal mode.

In this mode, the input operation cannot be edited, the cursor can be moved, and the following operation commands can be executed to delete, copy, paste, etc.

02 Edit Mode

Only when letters such as 'ilo O a A r R s S' enter the editing mode can the editing operation be performed.

insert image description here

03 command mode

In normal mode, enter ':' or '/' or '? ', the cursor will be automatically positioned on the lowest line.

In the command mode, you can perform related operations such as saving, exiting, searching, and line numbers.

See the first article here

2. Editing file skills

01 Batch delete multiple rows of specified information
  • Enter batch editing mode ctrl+v visual block
  • Batch delete operation d or x
02 Add multi-column specified information in batches
  • Enter batch editing mode ctrl+v visual block
  • Select the batch edit area and use the arrow keys
  • Enter the insert edit mode size letter I or shift + i
  • Start editing content information
  • Exit batch editing operation esc
03 Edit FAQ
Mistake 1: No edit information specified
[root@bogon ~]# vim

insert image description here

Error 2: Path information does not exist
[root@bogon ~]# vim zhang/zhang/zhang.txt

insert image description here

This error indicates that the path information corresponding to the edited file does not exist

Mistake 3: Editing is interrupted abnormally

insert image description here

When the current file is abnormal, because the file has not been saved to exit or the virtual machine is disconnected, a prompt is displayed.

Restore R

Vi abnormal problem handling method:

Restore temporary files: vi -r files to be edited

Show temporary files: vi -r/-L

Cancel the generation of temporary files: vi -n files that need to be edited

2. File editing commands

1、echo

The echo command can display text on the Linux command line, or redirect symbols into a specified file.
insert image description here

Grammar format:
echo [选项] [文本]
Common parameters:
serial number parameter options illustrate
1 -n don't word wrap
2 -E Do not parse escape characters (default parameter)
3 -e If the following characters appear in the string, special processing is required, and they cannot be output as ordinary text.
\a: issue a warning
\b: delete the previous character
\c: do not add a newline character at the end
\f: change the line but the cursor remains at the original position
\n: change the line and the cursor moves to the beginning of the line
\r: move the cursor to the line First, but no newline
\t: insert tab
Practical
01 -n parameter does not wrap
[root@bogon ~]# echo 'zhang01'; echo  'zhang02'
zhang01
zhang02
[root@bogon ~]# echo -n 'zhang01'; echo  'zhang02'
zhang01zhang02
[root@bogon ~]# 
02 printout uses escape symbols
\n
[root@bogon ~]# echo 'zhang01\nzhang02'
zhang01\nzhang02
[root@bogon ~]# echo -e 'zhang01\nzhang02'
zhang01
zhang02

\r
[root@bogon ~]# echo -e 'zhang01\rzhang02'
zhang02

\t 制表符
[root@bogon ~]# echo -e '01\tzhang\tlinux\n02\tzhang\tlinux'
01	zhang	linux
02	zhang	linux
03 Write the output information to the specified file
>/>>表示重定向,可以在命令执行后输出到屏幕的信息存到指定文件夹中
> 覆盖
[root@bogon ~]# cat zhang.txt
文件里内容忽略...
[root@bogon ~]# echo huanghun > zhang.txt
[root@bogon ~]# cat zhang.txt
huanghun

insert image description here

>> 追加
[root@bogon ~]# cat zhang.txt
huanghun
[root@bogon ~]# echo huanghun >> zhang.txt
[root@bogon ~]# cat zhang.txt
huanghun
huanghun

insert image description here

2、cat

The cat command displays the contents of a single file, or concatenates the contents of several files for display.

It can also read content from standard input and display it. In a production environment, it is used in conjunction with redirection or append symbols.

Grammar format:
cat [选项] [文件]
Common parameters:
serial number parameter options illustrate
1 -n Line number all output starting from the first line
2 -b Similar to the -n function, but ignores the display of blank line numbers
3 -s When encountering more than two consecutive blank lines, replace them with one blank line
4 -E Display a $ sign at the end of each line
Practical
01 Realize the editing operation of multi-line information content
[root@bogon ~]# cat > zhang.txt <<EOF
> zhang01
> zhang02
> zhang03
> zhang04
> EOF
[root@bogon ~]# cat zhang.txt
zhang01
zhang02
zhang03
zhang04
[root@bogon ~]# 

cat > zhang.txt means to open the zhang.txt file

zhang.txt <<EOF…EOF means input information to the opened file

02 Realize the combination of multiple file information and redirect the specified file
[root@bogon ~]# cat zhang.txt
zhang01
zhang02
zhang03
zhang04
[root@bogon ~]# cat /dev/null > zhang.txt
[root@bogon ~]# cat zhang.txt
[root@bogon ~]# 
serial number Common functions of cat command illustrate
1 view file content 例: cat zhang.txt
2 Merge multiple files into one 例:cat zhang01.txt zhang02.txt > newzhang.txt
3 Non-interactively edit or append content to the end of the file cat >> zhang.txt <<EOF
file
EOF
4 clear file content Use the above command cat /dev/null > zhang.txt
to clear the content of the file, but the file still exists.

3. File viewing command

1、more

The more command is similar to cat. The cat command displays the contents of the entire file on the screen at one time.

And more will display the contents of the file page by page.

Grammar format:
more [选项] [文件]
Common parameters:
serial number Parameters/special functions illustrate
1 -num Specifies that the screen display size is num lines
2 +num Start displaying from line number num
3 Enter show 1 line down
4 space bar scroll down one screen
5 b back to previous screen
6 = output the line number of the current line
7 /lookup text Find the specified text
8 v call vi editor
9 q exit more view status
Practical
01 View only a few lines of information
[root@bogon ~]# more -3 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
#
--More--(0%)
02 Only view the content information after the specified line
[root@bogon ~]# more +333 -3 /etc/services
ica             1494/tcp                        # Citrix ICA Client
ica             1494/udp                        # Citrix ICA Client
wins            1512/tcp                        # Microsoft's Windows Internet Name
--More--(2%)

2、less

The less command is similar to the more command, which can display the contents of the file in pages, which is more powerful than the more command.

When the less command reads the content of the file, it does not need to load the entire file at once and display it, but loads the content of the file as needed.

And the less command supports key functions such as [page up] [page down], which is convenient for viewing file content.

Grammar format:
less [选项] [文件]
Common parameters:
serial number parameter options illustrate
1 -i Ignore case when searching
2 -m Show progress percentage similar to more command
3 -N display the line number of each line
4 -s Compress consecutive blank lines into one line for display
5 -e 当文件显示到结尾时自动退出文件,若不使用此选项则需要使用交互命令q退出less

交互操作:

再交互模式下,less命令也是基于more命令和vi命令的。

序号 子命令 说明
1 b 向前翻一页
2 空格键 向后翻一页
3 向上滚动一行
4 ↓/回车键 向下滚一行
5 /字符串 向下搜索字符串
6 ?字符串 向上搜索字符串
7 n 向后查找下一个匹配的文本
8 N 向前查找前一个匹配的文本
9 v 进入vi编辑界面
10 G 移动到最后一行
11 g 移动到第一行
12 h 显示帮助界面
13 q 退出less命令

3、head

head命令用于显示文件头部的内容,执行head命令默认会输出文件的开头10行内容。

语法格式:
head [选项] [文件]
常用参数:
序号 参数选项 说明
1 -n<行数> 制定显示的行数
2 -c<字节数> 指定显示的字节数
3 -q 不显示包含给定文件名的文件头
4 -v 总是显示包含给定文件名的文件头
实操:
01 显示文件前几行

insert image description here

[root@bogon ~]# head -n 2 /etc/services
# /etc/services:
# $Id: services,v 1.55 2013/04/14 ovasik Exp $
[root@bogon ~]# 

4、tail

语法格式:
tail [选项] [文件]
常用参数:
序号 参数选项 说明
1 -n<行数> 指定显示的字节数
2 -c<数目> 指定显示的行数
3 -f 实时输出文件变化后的追加的数据
4 -F 功能等同于-f --retry
5 –retry 不停尝试打开文件直到打开为止,与-f参数合用
6 –pid=进程号 与-f参数连用,在进程结束后自动退出tail命令
7 -s秒数N 监视文件变化的间隔秒数
8 -q 不显示包含给定文件名的文件头
9 -v 总是显示包含给定文件名的文件头
实操:
01 显示文件指定最后几行的信息
[root@bogon ~]# tail -2 zhang.txt
2
abc
[root@bogon ~]# tail -n 2 zhang.txt
2
abc
[root@bogon ~]# 
02 从第几行开始显示文件内容
[root@bogon ~]# cat zhang.txt
qqqqsss1111111
qqqqsss1111111
省略中间内容...
1
2
abc
[root@bogon ~]#
[root@bogon ~]# tail -n +20 zhang.txt
5
6
省略中间内容...
1
2
abc
[root@bogon ~]# 
03 tail命令实时监控文件的变化

主要用于日志文件信息,便于排查系统或程序的问题。

[root@bogon ~]# ll /var/log/messages
-rw-------. 1 root root 870936 4月  21 11:10 /var/log/messages
[root@bogon ~]# 

[root@bogon ~]# cat /var/log/messages
省略中间内容...
[root@bogon ~]# 

[root@bogon ~]# tail -5 /var/log/messages
Apr 21 11:00:01 bogon systemd: Starting Session 16 of user root.
Apr 21 11:01:01 bogon systemd: Started Session 17 of user root.
Apr 21 11:01:01 bogon systemd: Starting Session 17 of user root.
Apr 21 11:10:01 bogon systemd: Started Session 18 of user root.
Apr 21 11:10:01 bogon systemd: Starting Session 18 of user root.
[root@bogon ~]# 

[root@bogon ~]# tail -f /var/log/messages
Apr 21 10:52:24 bogon gnome-software-service.desktop: 02:52:24:0178 Gs  no app for changed [email protected]
Apr 21 10:52:24 bogon gnome-software-service.desktop: 02:52:24:0178 Gs  no app for changed [email protected]
Apr 21 10:52:24 bogon gnome-software-service.desktop: 02:52:24:0183 Gs  no app for changed [email protected]
Apr 21 10:52:24 bogon gnome-software-service.desktop: 02:52:24:0191 Gs  no app for changed [email protected]
Apr 21 11:00:01 bogon systemd: Started Session 16 of user root.
Apr 21 11:00:01 bogon systemd: Starting Session 16 of user root.
Apr 21 11:01:01 bogon systemd: Started Session 17 of user root.
Apr 21 11:01:01 bogon systemd: Starting Session 17 of user root.
Apr 21 11:10:01 bogon systemd: Started Session 18 of user root.
Apr 21 11:10:01 bogon systemd: Starting Session 18 of user root.

// 重启服务
[root@bogon ~]# systemctl restart NetworkManager
[root@bogon ~]# tail -f /var/log/messages
Apr 21 11:14:55 bogon dnsmasq[1586]: using nameserver 114.114.114.114#53
Apr 21 11:14:55 bogon dnsmasq[1586]: using nameserver 8.8.8.8#53
Apr 21 11:14:55 bogon nm-dispatcher: req:3 'up' [ens33]: new request (4 scripts)
Apr 21 11:14:56 bogon systemd: Started Network Manager Wait Online.
Apr 21 11:14:56 bogon NetworkManager[5893]: <info>  [1682046896.0008] manager: NetworkManager state is now CONNECTED_GLOBAL
Apr 21 11:14:56 bogon nm-dispatcher: req:4 'connectivity-change': new request (4 scripts)
Apr 21 11:14:56 bogon systemd: Unit iscsi.service cannot be reloaded because it is inactive.
Apr 21 11:14:56 bogon nm-dispatcher: req:3 'up' [ens33]: start running ordered scripts...
Apr 21 11:14:56 bogon systemd: Unit iscsi.service cannot be reloaded because it is inactive.
Apr 21 11:14:56 bogon nm-dispatcher: req:4 'connectivity-change': start running ordered scripts...

四、文本数据处理命令

1、grep

grep是从文本文件或管道数据流中筛选匹配的行及数据,如配合正则表达式一块使用功能会更强大。

语法格式:
grep 选项 匹配模式 文件

匹配模式:是要获取的内容,可以是普通的文字符号也可以是正则表达式。

选项参数:
序号 选项参数 说明
1 -v 显示不匹配的行,或者也就是排除某些行,显示其他行信息
2 -n 显示匹配的行及行号
3 -i 不区分大小写,默认是区分大小写
4 -c 只统计匹配的行数,注意不是匹配的次数
5 -w 以单词为单位进行过滤
6 -o 只输出匹配的内容
实操:
01 grep过滤不包含qqq字符串的行(-v参数实践)
[root@bogon ~]# cat grep zhang.txt
qqqqsss1111111
qqqqsss1111111
省略中间内容...
1
2
abc

[root@bogon ~]# grep -v 'qqq' zhang.txt
2222222
3333333
4
省略中间内容...
1
2
abc
02 grep显示过滤后的内容的行号(-n参数实践)
[root@bogon ~]# grep -n 'qqq' zhang.txt
1:qqqqsss1111111
2:qqqqsss1111111
省略中间内容...
15:qqqqsss1111111
16:qqqqsss1111111
[root@bogon ~]# 
03 grep不区分大小写进行过滤(-i参数实践)
[root@bogon ~]# cat grep_zhang.txt
zhang
linux
zhang01
zhang02
H5
[root@bogon ~]# grep -i 'zhang' grep_zhang.txt
zhang
zhang01
zhang02
04 grep calculates the number of matching strings (-c parameter practice)
[root@bogon ~]# cat grep_zhang.txt
zhang
linux
zhang01
zhang02
H5
[root@bogon ~]# grep -c 'zhang' grep_zhang.txt
3
05 grep only outputs matching content (-o parameter practice)
[root@bogon ~]# grep -o 'zhang' grep_zhang.txt
zhang
zhang
zhang
[root@bogon ~]# 
06 grep search for users that meet the requirements (-w parameter practice)
[root@bogon ~]# cat grep_zhang.txt
zhang
linux
zhang01
zhang02
H5
[root@bogon ~]# grep -w 'zhang' grep_zhang.txt
zhang

2、tr

The tr command can read data information to replace , reduce or delete characters, and display the results.

Grammar format:
tr 选项 字符1 字符2
Option parameters:
serial number option parameter illustrate
1 -d Delete characters※
2 -s Keep consecutive characters and send the first character, delete other characters
3 -c use the complement of the first string, negate
Practice:
01 Replace 'abc' in the file with 'zhang'
[root@bogon ~]# cat tr_test.txt
I am zhang
linux
H5
ui
[root@bogon ~]# tr 'zhang' '12345' tr_test.txt
tr: 额外的操作数 "tr_test.txt"
Try 'tr --help' for more information.
[root@bogon ~]# tr 'zhang' '12345' <  tr_test.txt
I 3m 12345
li4ux
H5
ui
[root@bogon ~]# 

The tr command needs to use the standard input redirection symbol < to read the file content, and the tr replacement information is replaced by character 1:1.

02 Filter out the 'qqq' appearing in the file, and do not display the output.
[root@bogon ~]# tr -d 'linux' < tr_test.txt
I am zhag

H5

In specific scenarios, use the tr command to perform targeted operations on specified characters.

Guess you like

Origin blog.csdn.net/m0_62181310/article/details/130323036