格式化其他模板设置错误

我发现原来项目中我的日期格式化设置错误了

1
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

这里的hh表示的是12小时制的小时数,这样会造成下午的时间比上午的实现小的情况。应该改成24小时制
也就是修改成如下的代码:

1
private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

但是项目下的代码比较多,我已经记不得到底是哪个类中有这样的错误。所以需要对整个项目进行全局替换

idea搜索整个项目

点击Edit菜单,然后依次选择:Find,Search Structurally…;即可进入全局查找功能

在Search template:文本域中输入要查找的字符串,然后点击下方的find按钮即可。

idea替换整个项目

点击Edit菜单,然后依次选择:Find,Replace Structurally…;
图片
在Search template中输入要查找的字符串,
在Replace template中输入要替换的字符串,
然后点击find按钮即可
图片

JavaScript响应键盘事件

监听键盘弹起事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<style type="text/css">
</style>
<script>
监听键盘按下时间
document.onkeyup=function(){
console.log("键盘弹起");
};
</script>
</head>

<body>
</body>

</html>

打印keyCode

1
2
3
4
5
6
7
// 打印键盘keyCode
document.onkeyup = function (event) {
event = event || window.event;
console.log("键盘弹起,keyCode=" + event.keyCode);
};
// 判断是否按下了某个按键
// keyCode

使用组合键

1
2
3
4
5
6
7
8
9
10
// a=65,n=78,z=90
document.onkeyup = function (event) {
event = event || window.event;
if (event.keyCode === 78 && event.ctrlKey && event.altKey) {
console.log('您按下了:ctrl+alt' + event.keyCode);
}
if ( event.altKey && event.keyCode === 78) {
console.log('您按下了:alt' + event.keyCode);
}
};

响应组合键

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<script>
document.onkeyup = function (event) {
// alt键
if (event.altKey) {
switch (event.keyCode) {
// i键的keyCode
case 73:
input.focus();
break;
// n键的keyCode
case 78:
niuke();
break;
}
}
}
</script>

JS按键和keyCode的对应关系

参考资料

https://www.bilibili.com/video/BV1cs411u7qt

git add报错:fatal: Unable to create ‘E:/Blog/blogRoot/.git/index.lock’: File exists.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/blogRoot (src)
$ git status
On branch src
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: .gitignore
modified: source/tools/Tools2.html
modified: source/tools/index.md

no changes added to commit (use "git add" and/or "git commit -a")

lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/blogRoot (src)
$ git add .
fatal: Unable to create 'E:/Blog/blogRoot/.git/index.lock': File exists.

Another git process seems to be running in this repository, e.g.
an editor opened by 'git commit'. Please make sure all processes
are terminated then try again. If it still fails, a git process
may have crashed in this repository earlier:
remove the file manually to continue.

lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/blogRoot (src)

解决方案:删除.git/index.lock文件

删除E:/Blog/blogRoot/.git/index.lock文件即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/blogRoot (src)
$ rm E:/Blog/blogRoot/.git/index.lock

lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/blogRoot (src)
$ git status
On branch src
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: .gitignore
modified: source/tools/Tools2.html
modified: source/tools/index.md

no changes added to commit (use "git add" and/or "git commit -a")

lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/blogRoot (src)
$ git add .

lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/blogRoot (src)
$ git status
On branch src
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: .gitignore
modified: source/tools/Tools2.html
modified: source/tools/index.md


lan@DESKTOP-8ISAT6B MINGW64 /e/Blog/blogRoot (src)

项目结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
E:\dev2\idea_workspace\MyJavaTools\Commands
├─Commands.iml
├─META-INF\
│ └─MANIFEST.MF
├─resource\
└─src\
├─main\
│ └─Test.java
└─tools\
├─html\
│ ├─HtmlTools.java
│ └─toMdTable.html
└─string\
└─StringReplace.java

Test.java读取toMdTable.html文件内容

现在在Test.java中,读取toMdTable.html文件中的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
public class Test {
public static void main(String[] args) {
html2MdTable();
}
public static void html2MdTable() {
Scanner scanner = new Scanner(Test.class.getResourceAsStream("/tools/html/toMdTable.html"));
//Scanner scanner = new Scanner(Test.class.getResourceAsStream("toMdTable.html"));
while(scanner.hasNextLine()){
System.out.println(scanner.nextLine());
}

}
}

这里获取toMdTable.html文件的输入流的方法为:

1
Test.class.getResourceAsStream("/tools/html/toMdTable.html")

路径为:

1
/tools/html/toMdTable.html

这个路径由,两部分组成:

  • 第一部分是 反斜杠/,反斜杠表示的是项目的路径。
  • 第二部分是 tools/html/toMdTable.html,这个路径是文件相对于src目录的路径

如果不加根路径,则会读取失败,也就是说如下的写法是错误的。

1
Test.class.getResourceAsStream("tools/html/toMdTable.html")

如果只写文件名,也是不能加载的,也就是如下写法是错误的:

1
Test.class.getResourceAsStream("toMdTable.html")

还有就是不要把文件放到src/main目录下,main目录下的文件是不能用这种方法来读取的

more命令

more命令是一个基于vi编辑器文本过滤器,它以全屏幕的方式按页显示文本文件的内容,支持vi中的关键字定位操作。

more内置快捷键

more名单中内置了若干快捷键,常用的有

  • H(获得帮助信息),
  • Enter(向下翻滚一行),
  • 空格(向下滚动一屏),
  • Q(退出命令)。

该命令一次显示一屏文本,满屏后停下来,并且在屏幕的底部出现一个提示信息,给出至今己显示的该文件的百分比:–More–(XX%)可以用下列不同的方法对提示做出回答:

  • 按Space键:显示文本的下一屏内容。
  • 按Enter键:只显示文本的下一行内容。
  • 按斜线符/:接着输入一个模式,可以在文本中寻找下一个相匹配的模式。
  • 按H键:显示帮助屏,该屏上有相关的帮助信息。
  • 按B键:显示上一屏内容。
  • 按Q键:退出rnore命令。

man more

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
MORE(1)                                                             BSD General Commands Manual                                                            MORE(1)

NAME
more — 在显示器上阅读文件的过滤器

总览 (SYNOPSIS)
more [-dlfpcsu] [-num] [+/ pattern] [+ linenum] [file ...]

描述 (DESCRIPTION)
More 是 一个 过滤器, 用于 分页 显示 (一次一屏) 文本. 这个 版本 非常 基本. 用户 应该 知道 less(1) 提供了 more(1) 的 模拟, 并且 做了 增强.

选项 (OPTION)
下面 介绍 命令行选项. 选项 可以 从 环境变量 MORE 中获取 (要 确保 它们 以 短横线 开头 (``-'')), 但是 命令行选项 能够 覆盖 它们.

-num 这个选项指定屏幕的行数 (以整数表示).

-d 让 more 给 用户 显示 提示信息 "[Press space to continue, 'q' to quit.]", 当 用户 按下 其他键 时, 显示 "[Press 'h' for instructions.]", 而不是 扬声器
鸣笛.

-l More 在 通常情况下 把 ^L (form feed) 当做 特殊字符, 遇到 这个字符 就会 暂停. -l 选项 可以 阻止 这种特性.

-f 使 more 计数 逻辑行, 而不是 屏幕行 (就是说, 长行 不会 断到 下一行).

-p 不卷屏, 而是 清除 整个屏幕, 然后 显示 文本.

-c 不卷屏, 而是 从 每一屏的 顶部 开始 显示 文本, 每 显示完 一行, 就 清除 这一行的 剩余部分.

-s 把 重复的空行 压缩成 一个 空行.

-u 防止下划线.

+/ 在 显示 每个文件 前, 搜索 +/ 选项 指定的 文本串.

+num 从行号 num 开始显示.

命令 (COMMAND)
more 的交互命令基于 vi(1). 有些命令 以 一个 十进制数字 开始, 在 下面的描述 里 称之为 k. 后面的 描述中, ^X 指 control-X.

h or ? 帮助: 显示 这些 命令 的 摘要. 你 如果 忘掉 所有 其他的命令, 请记住这个.

SPACE 显示 接下来的 k 行文本. 缺省值 是 当前的屏幕大小.

z 显示 接下来的 k 行文本. 缺省值 是 当前的屏幕大小. 参数 成为 新的缺省值.

RETURN 显示 接下来的 k 行文本. 缺省值 是 1. 参数 成为 新的缺省值.

d or ^D 卷屏 k 行. 缺省值 是 当前的 卷屏大小, 初始化为 11. 参数 成为 新的缺省值.

q 或 Q 或 INTERRUPT
退出.

s 向前跳过 k 行文本. 缺省值 是 1.

f 向前跳过 k 屏文本. 缺省值 是 1.

b or ^B 向后跳回 k 屏文本. 缺省值 是 1.

' 跳到 上一次 搜索 开始 的 地方.

= 显示当前行号.

/pattern 搜索 第 k 个 符合 正则表达式的 文本串. 缺省值 是 1.

n 搜索 最后 第 k 个 符合 正则表达式的 文本串. 缺省值 是 1.

!&lt;cmd&gt; or :!&lt;cmd&gt;
在子 shell 中执行 &lt;cmd&gt;.

v 启动 /usr/bin/vi, 指向 当前行.

^L 刷新屏幕.

:n 跳到 后面 第 k 个 文件. 缺省值 是 1.

:p 跳到 前面 第 k 个 文件. 缺省值 是 1.

:f 显示 当前文件名 和 行号.

. 重复上次命令.

环境 (ENVIRONMENT)
More 利用 下面的 环境变量 (如果 它们 存在):

MORE 这个变量 设置 你 喜欢的 more 选项.

SHELL 当前使用的 shell (一般说来 就是 登录 shell).

TERM 指定 终端类型, more 用它来 获取 操作屏幕 所需的 终端特性.

另见 (SEE ALSO)
vi(1) less(1)

作者 (AUTHOR)
Eric Shienbrood, UC Berkeley
Modified by Geoff Peck, UCB to add underlining, single spacing
Modified by John Foderaro, UCB to add -c and MORE environment variable

历史 (HISTORY)
more 命令 出现在 3.0BSD. 本手册页 描述了 more version 5.19 (Berkeley 6/29/88), 目前 它 用在 Linux 社团. 制作文档时 使用了 其他几个版本 的 手册页, 并且 根据
源程序 作了 扩充.

测试文件

为了测试文件,现在创建一个more_test.txt文件,写入如下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@localhost Linux_Test]# cat more_test.txt 
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y 、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z
[root@localhost Linux_Test]#

more

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@localhost Linux_Test]# more more_test.txt 
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y
、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
--More--(96%)

more选项

more -num文件:指定屏幕的行数 (以整数表示)

more -5 more_test.txt刚开始只显示文件的前面5行:

1
2
3
4
5
6
7
8
[root@localhost Linux_Test]# more -5 more_test.txt 
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y
、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
--More--(78%)

如果按下空格,则会再显示下一屏幕(5行):

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost Linux_Test]# more -5 more_test.txt 
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y
、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
4
5
6
7
8
--More--(81%)

more -d:显示提示信息 按错键不鸣笛

more -d:让more给用户显示提示信息”[Press space to continue, ‘q’ to quit.]”, 当用户按下其他键时,显示”[Press ‘h’ for instructions.]”,而不是扬声器鸣笛.

1
2
3
4
5
6
7
8
[root@localhost Linux_Test]# more -d -5 more_test.txt 
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y
、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
--More--(78%)[Press space to continue, 'q' to quit.]

more -f:more计数逻辑行而不是屏幕行

more -f:使more计数逻辑行,而不是屏幕行(就是说,长行不会断到下一行).

1
2
3
4
5
6
7
8
[root@localhost Linux_Test]# more -f -5 more_test.txt 
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y 、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
4
--More--(78%)

more -p:清屏后再显示

不卷屏,而是清除整个屏幕,然后显示文本.

1
2
3
4
5
6
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y
、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
--More--(78%)

当你按下空格键,显示下一屏的时候,会再次清屏,然后在输出:

1
2
3
4
5
6
4
5
6
7
8
--More--(81%)

你可以向上滚动屏幕,可以看到清屏,然后输出的效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost Linux_Test]# more -p -5 more_test.txt 
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y
、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
--More--(78%)
4
5
6
7
8
--More--(81%)
9
A
B
C
D
--More--(85%)

more -c:下一屏幕的内容会覆盖当前屏幕的内容

不卷屏,而是从每一屏的顶部开始显示文本,每显示完一行,就清除这一行的剩余部分.

1
2
3
4
5
6
7
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y
、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
--More--(78%)

当你按下空格后,more命令会清除当前屏幕的输出行,然后再使用下一屏幕的内容覆盖到当前屏幕输出。

1
2
3
4
5
6
7
4
5
6
7
8
--More--(81%)

当你向上滚动屏幕时,只会看到more输出了一个屏幕而已,标准输出中不会有之前的输出信息,这就是和more -p的区别:

1
2
3
4
5
6
7
8
[root@localhost Linux_Test]#
4
5
6
7
8
--More--(81%)

more -s:压缩重复空行

测试文件

1
2
3
4
5
6
7
8
[root@localhost Linux_Test]# cat more_test_2.txt 
1
2



6
[root@localhost Linux_Test]#
1
2
3
4
5
6
7
[root@localhost Linux_Test]# more -5 more_test_2.txt 
1
2



--More--(77%)
1
2
3
4
5
6
[root@localhost Linux_Test]# more -s -5 more_test_2.txt 
1
2

6
[root@localhost Linux_Test]#

more +/正则表达式:从有匹配正则表达式的字符串的行开始显示

测试文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost Linux_Test]# cat more_test3.txt 
1
2



6
aaa
b
c
d
e
f
[root@localhost Linux_Test]#

命令:more -5 +/^[a-z] more_test3.txt表示每屏幕显示5行,从匹配正则表达式^[a-z]开始的行附近开始显示。

1
2
3
4
5
6
7
8
9
[root@localhost Linux_Test]# more -5 +/^[a-z] more_test3.txt 

...跳过

6
aaa
b
c
--More--(73%)

more +linenum:从第linenum行开始显示

1
2
3
4
5
6
7
8
[root@localhost Linux_Test]# more -5  more_test.txt 
0英文的26个字母大写是:A、 B、 C、 D、 E、 F、 G 、H 、I、 J、 K 、L、 M 、N 、O、 P 、Q 、R、 S、 T、 U、 V、 W、 X 、Y
、Z。阿拉伯数字为:0、1、2、3、4、5、6、7、8、9。
1
2
3
--More--(78%)

一屏幕显示5行,从第5行开始显示:

1
2
3
4
5
6
7
8
[root@localhost Linux_Test]# more -5 +5 more_test.txt 
4
5
6
7
8
--More--(81%)

more的显示控制命令

满屏后,显示–more–或–more–(xx%),可以使用more命令:

more命令 功能
空格 显示下一屏
回车 上滚一行,当所感兴趣的段落内容正好处于当前屏的尾部,另有一部分在下一页中时,可以连续按回车,将感兴趣的部分滚动上来
q (quit)退出程序,后面的内容不再显示
/pattern 搜索指定模式的字符串,模式描述用正则表达式
/ 继续查找指定模式的字符串
h (Help)帮助信息。打印more命令的所有功能列表
Ctrl-L 屏幕刷新

测试文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
[root@localhost Linux_Test]# cat more_test4.txt 
1
2
3
4
5
6
7
8
9
0
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
PP
QQ
RR
SS
TT
UU
VV
WW
XX
YY
ZZ
[root@localhost Linux_Test]#

显示5行逻辑行

1
2
3
4
5
6
7
8
[root@localhost Linux_Test]# more -f -5 more_test4.txt 
1
2
3
4
5
--More--(12%)

此时可以输入more的命令。

显示more的控制命令的帮助文档

输入h或者shift+?可以显示帮助文档,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Most commands optionally preceded by integer argument k.  Defaults in brackets.
Star (*) indicates argument becomes new default.
-------------------------------------------------------------------------------
<space> Display next k lines of text [current screen size]
z Display next k lines of text [current screen size]*
<return> Display next k lines of text [1]*
d or ctrl-D Scroll k lines [current scroll size, initially 11]*
q or Q or <interrupt> Exit from more
s Skip forward k lines of text [1]
f Skip forward k screenfuls of text [1]
b or ctrl-B Skip backwards k screenfuls of text [1]
' Go to place where previous search started
= Display current line number
/<regular expression> Search for kth occurrence of regular expression [1]
n Search for kth occurrence of last r.e [1]
!<cmd> or :!<cmd> Execute <cmd> in a subshell
v Start up /usr/bin/vi at current line
ctrl-L Redraw screen
:n Go to kth next file [1]
:p Go to kth previous file [1]
:f Display current file name and line number
. Repeat previous command
-------------------------------------------------------------------------------
--More--(12%)

正则查找

输入/可以进入正则表达式查找功能。
例如输入/^[A-Z]跳转到以大写字母开头的行:

1
2
3
4
5
6
7
/^[A-Z]  
...跳过
9
0
A
B
--More--(28%)

输入/[A-Z][A-Z],跳转到包含两个大写字母的行:

1
2
3
4
5
6
7
8
/[A-Z][A-Z]
...跳过
N
O
PP
QQ
--More--(67%)

按键n:跳转到下一个正则匹配行:

按下一次按键n的效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[root@localhost Linux_Test]# more -f -5 more_test4.txt 
1
2
3
4
5
/[A-Z]
...跳过
9
0
A
B
/[A-Z][A-Z]
...跳过
N
O
PP
QQ
RR
SS
TT
UU
--More--(81%)

再按一次按钮n的效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@localhost Linux_Test]# more -f -5 more_test4.txt 
1
2
3
4
5
/[A-Z]
...跳过
9
0
A
B
/[A-Z][A-Z]
...跳过
N
O
PP
QQ
RR
SS
TT
UU
VV
WW
XX
YY
--More--(96%)
more中输入正则表达式需要注意的点

在more中输入的正则表达式只能从左至右按顺序输入,无法使用左右箭头移动光标。
如果输入错了可以按下Ctrl+backSpace键进行删除,单独按backSpace键是无法删除的,反而会输入^H字符。

调用Vi编辑器打开当前文件

输入v可以使用vi编辑器打开more正在打开的文件。

参考资料

https://www.cnblogs.com/peida/archive/2012/11/02/2750588.html
https://www.yiibai.com/linux/more.html
https://man.linuxde.net/more
https://www.runoob.com/linux/linux-comm-more.html

cat命令

cat(英文全拼:concatenate)命令用于连接文件并打印到标准输出设备上。

语法格式

1
cat [-AbeEnstTuv] [--help] [--version] fileName

参数

1
2
3
4
5
6
7
8
9
-n 或 --number:由 1 开始对所有输出的行数编号。
-b 或 --number-nonblank:和 -n 相似,只不过对于空白行不编号。
-s 或 --squeeze-blank:当遇到有连续两行以上的空白行,就代换为一行的空白行。
-v 或 --show-nonprinting:使用 ^ 和 M- 符号,除了 LFD 和 TAB 之外。
-E 或 --show-ends : 在每行结束处显示 $。
-T 或 --show-tabs: 将 TAB 字符显示为 ^I。
-A, --show-all:等价于 -vET。
-e:等价于"-vE"选项;
-t:等价于"-vT"选项;

示例

cat -n:显示行号

1
2
3
4
5
6
7
8
[root@localhost C_Test]# cat -n file1.txt 
1 Hello World!
2
3
4
5 Hello World!
6 ------file1 end-------
[root@localhost C_Test]#

cat -b:显示行号空白行不编号

1
2
3
4
5
6
7
8
[root@localhost C_Test]# cat -b file1.txt 
1 Hello World!



2 Hello World!
3 ------file1 end-------
[root@localhost C_Test]#

cat -s:替换多个空白行为一个空白行

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost C_Test]# cat -n file1.txt 
1 Hello World!
2
3
4
5 Hello World!
6 ------file1 end-------
[root@localhost C_Test]# cat -ns file1.txt
1 Hello World!
2
3 Hello World!
4 ------file1 end-------
[root@localhost C_Test]#

cat -v:

cat -E:显示结束符

1
2
3
4
5
6
7
8
[root@localhost C_Test]# cat -E file1.txt 
Hello World!$
$
$
$
Hello World!$
------file1 end-------$
[root@localhost C_Test]#

cat -T:显示tab键

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost C_Test]# cat file3.txt 
hello file3
hello


--------file3 end-----------
[root@localhost C_Test]# cat -T file3.txt
hello file3
^Ihello


--------file3 end-----------
[root@localhost C_Test]#

cat -A:显示tab键,显示换行符

1
2
3
4
5
6
7
[root@localhost C_Test]# cat -A file3.txt 
hello file3$
^Ihello$
$
$
--------file3 end-----------$
[root@localhost C_Test]#

同时查看多个文件

1
cat file1 file2

例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost C_Test]# cat -n file1.txt file2.txt 
1 Hello World!
2
3
4
5 Hello World!
6 ------file1 end-------
7 hello file2
8
9
10
11
12 hello file2
13 ------file2 end---------
[root@localhost C_Test]#

其他应用

cat从标准输入读取内容

cat > filename

1
cat >file4.txt

此时可以从标准输入中,输入文字,如果想结束输入请按下Ctrl键,然后按D键。

这种方法输入并不友好,可以不输入,直接按下Ctrl键,然后按D键,这样就创建一个空的文件。类似于:

1
touch file4.txt

例如从键盘输入hello world!到文件中。然后按下Ctrl+D结束输入:

1
2
3
4
5
6
[root@localhost Linux_Test]# cat >cat_test.txt
hello world![root@localhost Linux_Test]# ls
cat_test.txt less_test.txt ls_sort.txt more_test2.txt more_test4.txt sortFile.txt
date_test.txt ls_out.txt man_less.txt more_test3.txt more_test.txt vi_replaceAllTest.txt
[root@localhost Linux_Test]# cat cat_test.txt
hello world![root@localhost Linux_Test]#

cat >> filename

从标准输入中读取内容,追加到文件中。
上面在文件中没有添加换行符,现在追加一个换行符到文件中:

1
2
3
4
5
6
hello world![root@localhost Linux_Test]# cat >>cat_test.txt 

[root@localhost Linux_Test]# cat cat_test.txt
hello world!
[root@localhost Linux_Test]#

将cat命令与more或less命令一起使用

如果具有大量内容的文件无法容纳在输出终端中,并且屏幕快速滚动,则可以通过cat命令使用越来越多的参数,如上所示。

1
2
cat song.txt | more
cat song.txt | less

输出一个文件的内容到另一个文件的末尾

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@localhost C_Test]# cat file3.txt 
hello file3
hello


--------file3 end-----------
[root@localhost C_Test]# cat file1.txt >> file3.txt
[root@localhost C_Test]# cat file3.txt
hello file3
hello


--------file3 end-----------
Hello World!



Hello World!
------file1 end-------
[root@localhost C_Test]#

合并文件

执行cat时使用输出重定向,可以把多个文件按指定顺序合并成一个文件,这是一个很有用的功能。
例如:

1
cat file1.txt file2.txt >file3.txt 

file1.txt和file2.txt合并成一个文件file3.txt。

1
2
3
4
5
6
7
8
9
10
11
12
[root@localhost C_Test]# cat file1.txt file2.txt 
Hello file1!
------file1 end-------
hello file2
------file2 end---------
[root@localhost C_Test]# cat file1.txt file2.txt >file3.txt
[root@localhost C_Test]# cat file3.txt
Hello file1!
------file1 end-------
hello file2
------file2 end---------
[root@localhost C_Test]#

参考资料

https://www.runoob.com/linux/linux-comm-cat.html
https://zhuanlan.zhihu.com/p/91870070
https://www.jianshu.com/p/69ef4587c874
https://www.howtoing.com/13-basic-cat-command-examples-in-linux

tail命令

tail 命令可用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件。

tail -f filename 会把 filename 文件里的最尾部的内容显示在屏幕上,并且不断刷新,只要 filename 更新就可以看到最新的文件内容。

命令格式

1
tail [参数] [文件] 

参数

1
2
3
4
5
6
7
8
9
10
11
-c, --bytes=NUM                 输出文件尾部的NUM(NUM为整数)个字节内容。
-f, --follow[={name|descript}] 显示文件最新追加的内容。“name”表示以文件名的方式监视文件的变化。
-F 与 “--follow=name --retry” 功能相同。
-n, --line=NUM 输出文件的尾部NUM(NUM位数字)行内容。
--pid=<进程号> 与“-f”选项连用,当指定的进程号的进程终止后,自动退出tail命令。
-q, --quiet, --silent 当有多个文件参数时,不输出各个文件名。
--retry 即是在tail命令启动时,文件不可访问或者文件稍后变得不可访问,都始终尝试打开文件。使用此选项时需要与选项“--follow=name”连用。
-s, --sleep-interal=<秒数> 与“-f”选项连用,指定监视文件变化时间隔的秒数。
-v, --verbose 当有多个文件参数时,总是输出各个文件名。
--help 显示指令的帮助信息。
--version 显示指令的版本信息。

实例

tail file:显示文件的最后10行

要显示 notes.log 文件的最后 10 行,请输入以下命令:

1
tail notes.log

tail -f file:追踪显示 查看实时日志

要跟踪名为 notes.log 的文件的增长情况,请输入以下命令:

1
tail -f notes.log

此命令显示 notes.log 文件的最后 10 行。当将某些行添加至 notes.log 文件时,tail 命令会继续显示这些行。 显示一直继续,直到您按下(Ctrl-C)组合键停止显示。

tail -n +N file:从第N行显示到文件末尾

显示文件 notes.log 的内容,从第 20 行至文件末尾:

1
tail -n +20 notes.log

查看第100行至文件末尾:

1
tail -n +100 itbilu.log

tail -c N file:显示文件最后的N个字节

显示文件 notes.log 的最后 10 个字节:

1
tail -c 10 notes.log
1
2
3
4
5
6
7
tail file #(显示文件file的最后10行)
tail -n +20 file #(显示文件file的内容,从第20行至文件末尾)
tail -c 10 file #(显示文件file的最后10个字节)

tail -25 mail.log # 显示 mail.log 最后的 25 行
tail -f mail.log # 等同于--follow=descriptor,根据文件描述符进行追踪,当文件改名或被删除,追踪停止
tail -F mail.log # 等同于--follow=name --retry,根据文件名进行追踪,并保持重试,即该文件被删除或改名后,如果再次创建相同的文件名,会继续追踪

tail结合其他命令使用

结合其他命令使用tail
通过使用管道将标准输出从其他实用程序重定向到其他实用程序,可以将tail命令与其他命令结合使用。

例如,要监视apache访问日志文件并仅显示包含IP地址192.168.42.12的行,可以使用:

1
tail -f /var/log/apache2/access.log | grep 192.168.42.12

以下ps命令将显示按CPU使用率排序的前十个正在运行的进程:

1
ps aux | sort -nk +3 | tail -5

head和tail结合使用:查看从第N行到第M行

配合head命令,实现查看文件的第10到20行:

1
head -20 itbilu.log | tail -10

实时日志查看与grep过滤关键字

通过-f参数,并配合grep命令,可以实现对文件内容的过滤。如:查看前几行、后几行、或前后几行,这时可以通过以下几个参数实现:

参数 描述
-A <显示行数> 除了显示符合匹配内容的那一行之外,并显示该行之后的内容
-B <显示行数> 在显示符合匹配内容的那一行之外,并显示该行之前的内容
-C <显示行数>或-<显示行数> 除了显示符合匹配内容的那一列之外,并显示该列之前以及之后的内容

控itbilu.log日志件,并查看含有’foo’关键字的前后5行:

1
tail -f itbilu.log|grep 'foo' -C 5

1
tail -f itbilu.log|grep 'foo' -5

同时显示多个文件

如果提供了多个文件作为tail命令的输入,它将显示每个文件的最后十行。

1
tail filename1.txt filename2.txt

您可以使用与显示单个文件时相同的选项。 此示例显示文件filename1.txt和filename2.txt的最后20行:

1
tail -n 20 filename1.txt filename2.txt

参考资料

https://www.runoob.com/linux/linux-comm-tail.html
https://wangchujiang.com/linux-command/c/tail.html
Linux tail命令与实时日志、文件查看及过滤:https://itbilu.com/linux/man/H1_dxWhz4.html
tail在线帮助文档:https://man7.org/linux/man-pages/man1/tail.1.html
https://zhuanlan.zhihu.com/p/105741730
https://www.yuque.com/gaoxizhi/linux/tail

C语言代码

1
vim strerror.c

输入如下c语言代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/************关于本文档********************************************
*filename: strerror.c
*purpose: 列出了系统定义的所有错误代码及描述
*/

#include <string.h> /* for strerror */
#include <errno.h>
#include <stdio.h>

int main(int argc, char ** argv) {
int i = 0;
for(i = 0; i < 256; i++)
printf("errno.%02d is: %s\n", i, strerror(i));
return 0;
}

编译

1
gcc -Wall strerror.c

运行

1
2
3
[root@localhost C_Test]# ls
a.out strerror.c
[root@localhost C_Test]# ./a.out

错误码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
errno.00 is: Success
errno.01 is: Operation not permitted
errno.02 is: No such file or directory
errno.03 is: No such process
errno.04 is: Interrupted system call
errno.05 is: Input/output error
errno.06 is: No such device or address
errno.07 is: Argument list too long
errno.08 is: Exec format error
errno.09 is: Bad file descriptor
errno.10 is: No child processes
errno.11 is: Resource temporarily unavailable
errno.12 is: Cannot allocate memory
errno.13 is: Permission denied
errno.14 is: Bad address
errno.15 is: Block device required
errno.16 is: Device or resource busy
errno.17 is: File exists
errno.18 is: Invalid cross-device link
errno.19 is: No such device
errno.20 is: Not a directory
errno.21 is: Is a directory
errno.22 is: Invalid argument
errno.23 is: Too many open files in system
errno.24 is: Too many open files
errno.25 is: Inappropriate ioctl for device
errno.26 is: Text file busy
errno.27 is: File too large
errno.28 is: No space left on device
errno.29 is: Illegal seek
errno.30 is: Read-only file system
errno.31 is: Too many links
errno.32 is: Broken pipe
errno.33 is: Numerical argument out of domain
errno.34 is: Numerical result out of range
errno.35 is: Resource deadlock avoided
errno.36 is: File name too long
errno.37 is: No locks available
errno.38 is: Function not implemented
errno.39 is: Directory not empty
errno.40 is: Too many levels of symbolic links
errno.41 is: Unknown error 41
errno.42 is: No message of desired type
errno.43 is: Identifier removed
errno.44 is: Channel number out of range
errno.45 is: Level 2 not synchronized
errno.46 is: Level 3 halted
errno.47 is: Level 3 reset
errno.48 is: Link number out of range
errno.49 is: Protocol driver not attached
errno.50 is: No CSI structure available
errno.51 is: Level 2 halted
errno.52 is: Invalid exchange
errno.53 is: Invalid request descriptor
errno.54 is: Exchange full
errno.55 is: No anode
errno.56 is: Invalid request code
errno.57 is: Invalid slot
errno.58 is: Unknown error 58
errno.59 is: Bad font file format
errno.60 is: Device not a stream
errno.61 is: No data available
errno.62 is: Timer expired
errno.63 is: Out of streams resources
errno.64 is: Machine is not on the network
errno.65 is: Package not installed
errno.66 is: Object is remote
errno.67 is: Link has been severed
errno.68 is: Advertise error
errno.69 is: Srmount error
errno.70 is: Communication error on send
errno.71 is: Protocol error
errno.72 is: Multihop attempted
errno.73 is: RFS specific error
errno.74 is: Bad message
errno.75 is: Value too large for defined data type
errno.76 is: Name not unique on network
errno.77 is: File descriptor in bad state
errno.78 is: Remote address changed
errno.79 is: Can not access a needed shared library
errno.80 is: Accessing a corrupted shared library
errno.81 is: .lib section in a.out corrupted
errno.82 is: Attempting to link in too many shared libraries
errno.83 is: Cannot exec a shared library directly
errno.84 is: Invalid or incomplete multibyte or wide character
errno.85 is: Interrupted system call should be restarted
errno.86 is: Streams pipe error
errno.87 is: Too many users
errno.88 is: Socket operation on non-socket
errno.89 is: Destination address required
errno.90 is: Message too long
errno.91 is: Protocol wrong type for socket
errno.92 is: Protocol not available
errno.93 is: Protocol not supported
errno.94 is: Socket type not supported
errno.95 is: Operation not supported
errno.96 is: Protocol family not supported
errno.97 is: Address family not supported by protocol
errno.98 is: Address already in use
errno.99 is: Cannot assign requested address
errno.100 is: Network is down
errno.101 is: Network is unreachable
errno.102 is: Network dropped connection on reset
errno.103 is: Software caused connection abort
errno.104 is: Connection reset by peer
errno.105 is: No buffer space available
errno.106 is: Transport endpoint is already connected
errno.107 is: Transport endpoint is not connected
errno.108 is: Cannot send after transport endpoint shutdown
errno.109 is: Too many references: cannot splice
errno.110 is: Connection timed out
errno.111 is: Connection refused
errno.112 is: Host is down
errno.113 is: No route to host
errno.114 is: Operation already in progress
errno.115 is: Operation now in progress
errno.116 is: Stale file handle
errno.117 is: Structure needs cleaning
errno.118 is: Not a XENIX named type file
errno.119 is: No XENIX semaphores available
errno.120 is: Is a named type file
errno.121 is: Remote I/O error
errno.122 is: Disk quota exceeded
errno.123 is: No medium found
errno.124 is: Wrong medium type
errno.125 is: Operation canceled
errno.126 is: Required key not available
errno.127 is: Key has expired
errno.128 is: Key has been revoked
errno.129 is: Key was rejected by service
errno.130 is: Owner died
errno.131 is: State not recoverable
errno.132 is: Operation not possible due to RF-kill
errno.133 is: Memory page has hardware error
errno.134 is: Unknown error 134
errno.135 is: Unknown error 135
errno.136 is: Unknown error 136
......剩下的全都是Unknown
errno.253 is: Unknown error 253
errno.254 is: Unknown error 254
errno.255 is: Unknown error 255

shell中查看上一个命令的返回值(错误码)

在控制台下,有一个特殊的环境变量$?,保存着前一个程序的返回值,我们可以试试:

1
2
3
4
5
[root@localhost C_Test]# ls
a.out strerror.c
[root@localhost C_Test]# echo $?
0
[root@localhost C_Test]#

可以看到ls命令的返回值是0,这表示ls命令运行成功。

参考资料

http://www.cppblog.com/aaxron/archive/2012/01/06/163702.html
https://www.bbsmax.com/A/WpdKYW7AJV/
https://zhuanlan.zhihu.com/p/61767857
Linux错误代码及其含义:https://blog.csdn.net/u013457167/article/details/79196306
附录:Linux错误码(errno)列表:https://wiki.swoole.com/wiki/page/p-errno.html
http://blog.chinaunix.net/uid-10347480-id-3263127.html