0%

单字符正则表达式的组合

串结

abc串结在一起,可以匹配abc本身。
[A-Z].[0-9].串结在一起,可以匹配AA00

星号(*)

单字符正则表达式后跟*,表示匹配这个单字符正则表达式的0次或任意多次。
例:正则表达式12*4
与字符串1234不匹配,与1224,12224,14匹配

例:正则表达式[A-Z][0-9]*
此例中*作用的单字符正则表式为[0-9],代表

1
2
3
4
5
[A-Z]
[A-Z][0-9]
[A-Z][0-9][0-9]
[A-Z][0-9][0-9][0-9]
......

这与A,A1,C45,D768匹配,与b64512,T56t不匹配。

阅读全文 »

正则表达式的概念

正则表达式Regular Expressions应用范围

字符串匹配操作和替换操作
举例:Linux中的vi more grep yacc lex awk sed
其他:Visual Studio,Word等文本编辑器

正则表达式的功能

描述一个字符串模式
注意
正则表达式规则与文件名通配符规则不同

阅读全文 »

Linux uniq命令:筛选文件中的重复行

Linux的uniq 命令用于检查及删除文本文件中重复出现的行列,一般与 sort 命令结合使用。

uniq —help

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]# uniq --help
用法:uniq [选项]... [文件]
Filter adjacent matching lines from INPUT (or standard input),
writing to OUTPUT (or standard output).

With no options, matching lines are merged to the first occurrence.

Mandatory arguments to long options are mandatory for short options too.
-c, --count prefix lines by the number of occurrences
-d, --repeated only print duplicate lines, one for each group
-D, --all-repeated[=METHOD] print all duplicate lines
groups can be delimited with an empty line
METHOD={none(default),prepend,separate}
-f, --skip-fields=N avoid comparing the first N fields
--group[=METHOD] show all items, separating groups with an empty line
METHOD={separate(default),prepend,append,both}
-i, --ignore-case ignore differences in case when comparing
-s, --skip-chars=N avoid comparing the first N characters
-u, --unique only print unique lines
-z, --zero-terminated end lines with 0 byte, not newline
-w, --check-chars=N 对每行第N 个字符以后的内容不作对照
--help 显示此帮助信息并退出
--version 显示版本信息并退出

若域中为先空字符(通常包括空格以及制表符),然后非空字符,域中字符前的空字符将被跳过。

Note: 'uniq' does not detect repeated lines unless they are adjacent.
You may want to sort the input first, or use 'sort -u' without 'uniq'.
Also, comparisons honor the rules specified by 'LC_COLLATE'.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告uniq 的翻译错误
要获取完整文档,请运行:info coreutils 'uniq invocation'
[root@localhost Linux_Test]#
阅读全文 »

Linux sort命令:对文本文件的行排序

sort 是 Linux 的排序命令,而且可以依据不同的数据类型来进行排序。sort 将文件的每一行作为一个单位,相互比较。比较原则是从首字符向后,依次按 ASCII 码值进行比较,最后将它们按升序输出。

Sort是用于对单个或多个文本文件内容进行排序的Linux程序。Sort命令以空格作为字段分隔符,将一行分割为多个关键字对文件进行排序。需要注意的是除非你将输出重定向到文件中,否则Sort命令并不对文件内容进行实际的排序(即文件内容没有修改),只是将文件内容按有序输出。

阅读全文 »

Linux wc命令

功能

  • 列出文件中一共有多少行,有多少个单词,多少字符
  • 当指定的文件数大于 1 时,最后还列出一个合计
  • 常用选项l:只列出行计数

man wc

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
WC(1)                                                                           FSF                                                                          WC(1)

NAME
wc - 输出文件中的行数、单词数、字节数

SYNOPSIS 总览
wc [选项列表]... [文件名列表]...

SYNOPSIS 总览
wc [选项列表]... [文件名列表]...

DESCRIPTION 描述
对每个文件输出行、单词、和字节统计数,如果指定了多于一个文件则还有一 个行数的总计。没有指定文件或指定的文件是 -,则读取标准输入。

-c, --bytes, --chars
输出字节统计数。

-l, --lines
输出换行符统计数。

-L, --max-line-length
输出最长的行的长度。

-w, --words
输出单词统计数。

--help 显示帮助并退出

--version
输出版本信息并退出

AUTHOR 著作者
由 Paul Rubin 和 David MacKenzie 完成。
阅读全文 »

tee命令功能

将从标准输入 stdin 得到的数据抄送到标准输出 stdout 显示 ,同时存入磁盘文件中
简单的说就是把数据重定向到给定文件和屏幕上。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@localhost 2021年04月]# tee --help
用法:tee [选项]... [文件]...
将标准输入复制到每个指定文件,并显示到标准输出。

-a, --append 内容追加到给定的文件而非覆盖
-i, --ignore-interrupts 忽略中断信号
--help 显示此帮助信息并退出
--version 显示版本信息并退出

如果文件指定为"-",则将输入内容复制到标准输出。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告tee 的翻译错误
要获取完整文档,请运行:info coreutils 'tee invocation'
[root@localhost 2021年04月]#

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
TEE(1)                                                                          FSF                                                                         TEE(1)

NAME
tee - 从标准输入写往文件和标准输出

总览 (SYNOPSIS)
tee [OPTION]... [FILE]...

描述 (DESCRIPTION)
把 标准输入 的 数据 复制到 每一个 文件 FILE, 同时 送往 标准输出.
TEE(1) FSF TEE(1)

NAME
tee - 从标准输入写往文件和标准输出

总览 (SYNOPSIS)
tee [OPTION]... [FILE]...

描述 (DESCRIPTION)
把 标准输入 的 数据 复制到 每一个 文件 FILE, 同时 送往 标准输出.

-a, --append
追加到 给出的 文件, 而不是 覆盖

tee - 从标准输入写往文件和标准输出

总览 (SYNOPSIS)
tee [OPTION]... [FILE]...

描述 (DESCRIPTION)
把 标准输入 的 数据 复制到 每一个 文件 FILE, 同时 送往 标准输出.

-a, --append
追加到 给出的 文件, 而不是 覆盖

-i, --ignore-interrupts
忽略 中断信号

--help 显示 帮助信息, 然后 结束

--version
显示 版本信息, 然后 结束

阅读全文 »

Linux查看文件头命令:head

head —help

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
[root@localhost Linux_Test]# head --help
用法:head [选项]... [文件]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-c, --bytes=[-]K print the first K bytes of each file;
with the leading '-', print all but the last
K bytes of each file
-n, --lines=[-]K print the first K lines instead of the first 10;
with the leading '-', print all but the last
K lines of each file
-q, --quiet, --silent 不显示包含给定文件名的文件头
-v, --verbose 总是显示包含给定文件名的文件头
--help 显示此帮助信息并退出
--version 显示版本信息并退出

K 后面可以跟乘号:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, 对于T, P, E, Z, Y 同样适用。

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告head 的翻译错误
要获取完整文档,请运行:info coreutils 'head invocation'
[root@localhost Linux_Test]#
阅读全文 »

Linux字节打印命令od

od octal dump 逐字节打印( c, t c, t x1 t d1, t u1 选项)

od —help

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
[root@localhost Linux_Test]# od --help
用法:od [选项]... [文件]...
 或:od [-abcdfilosx]... [文件] [[+]偏移量[.][b]]
 或:od --traditional [选项]... [文件] [[+]偏移量[.][b] [+][标签][.][b]]

将指定文件以八进制形式(默认)转储到标准输出。如果指定了多于一个的文件
参数,程序会自动将输入的内容整合为列表并以同样的形式输出。
如果没有指定文件,或指定文件为"-",程序从标准输入读取数据。

If first and second call formats both apply, the second format is assumed
if the last operand begins with + or (if there are 2 operands) a digit.
An OFFSET operand means -j OFFSET. LABEL is the pseudo-address
at first byte printed, incremented when dump is progressing.
For OFFSET and LABEL, a 0x or 0X prefix indicates hexadecimal;
suffixes may be . for octal and b for multiply by 512.

Mandatory arguments to long options are mandatory for short options too.
-A, --address-radix=RADIX output format for file offsets; RADIX is one
of [doxn], for Decimal, Octal, Hex or None
-j, --skip-bytes=BYTES skip BYTES input bytes first
-N, --read-bytes=BYTES limit dump to BYTES input bytes
-S BYTES, --strings[=BYTES] output strings of at least BYTES graphic chars;
3 is implied when BYTES is not specified
-t, --format=TYPE select output format or formats
-v, --output-duplicates do not use * to mark line suppression
-w[BYTES], --width[=BYTES] output BYTES bytes per output line;
32 is implied when BYTES is not specified
--traditional accept arguments in third form above
--help 显示此帮助信息并退出
--version 显示版本信息并退出


Traditional format specifications may be intermixed; they accumulate:
-a same as -t a, select named characters, ignoring high-order bit
-b same as -t o1, select octal bytes
-c same as -t c, select printable characters or backslash escapes
-d same as -t u2, select unsigned decimal 2-byte units
-f 即 -t fF,指定浮点数对照输出格式
-i 即 -t dl,指定十进制整数对照输出格式
-l 即 -t dL,指定十进制长整数对照输出格式
-o 即 -t o2,指定双字节单位八进制数的对照输出格式
-s 即 -t d2,指定双字节单位十进制数的对照输出格式
-x 即 -t x2,指定双字节单位十六进制数的对照输出格式


TYPE is made up of one or more of these specifications:
a named character, ignoring high-order bit
c printable character or backslash escape
d[尺寸] 有符号十进制数,每个整形数占指定尺寸的字节
f[尺寸] 浮点数,每个整形数占指定尺寸的字节
o[尺寸] 八进制数,每个整形数占指定尺寸的字节
u[尺寸] 无符号十进制数,每个整形数占指定尺寸的字节
x[尺寸] 十六进制数,每个整形数占指定尺寸的字节

SIZE is a number. For TYPE in [doux], SIZE may also be C for
sizeof(char), S for sizeof(short), I for sizeof(int) or L for
sizeof(long). If TYPE is f, SIZE may also be F for sizeof(float), D
for sizeof(double) or L for sizeof(long double).

Adding a z suffix to any type displays printable characters at the end of
each output line.


BYTES is hex with 0x or 0X prefix, and may have a multiplier suffix:
b 512
KB 1000
K 1024
MB 1000*1000
M 1024*1024
and so on for G, T, P, E, Z, Y.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告od 的翻译错误
要获取完整文档,请运行:info coreutils 'od invocation'
[root@localhost Linux_Test]#
阅读全文 »