grep、sed、awk我们叫他们三剑客,掌握它们可以更好的运维,提升工作效率,即使不是运维,对我们处理数据都是非常方便的~就很多数据处理来讲,写程序肯定是也能处理的,但是远没有已经存在特定功能的命令更高效,我们只需要操作命令即可。
grep
简介
grep是一款强大的文本搜索工具,支持正则表达式。 全称( global search regular expression(RE) and print out the line)
语法: grep [option]... PATTERN [FILE]...
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
|
grep --help
用法: grep [选项]... PATTERN [FILE]...
在每个 FILE 或是标准输入中查找 PATTERN。
默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。
例如: grep -i 'hello world' menu.h main.c
正则表达式选择与解释:
-E, --extended-regexp PATTERN 是一个可扩展的正则表达式(缩写为 ERE)
-F, --fixed-strings PATTERN 是一组由断行符分隔的定长字符串。
-G, --basic-regexp PATTERN 是一个基本正则表达式(缩写为 BRE)
-P, --perl-regexp PATTERN 是一个 Perl 正则表达式
-e, --regexp=PATTERN 用 PATTERN 来进行匹配操作
-f, --file=FILE 从 FILE 中取得 PATTERN
-i, --ignore-case 忽略大小写
-w, --word-regexp 强制 PATTERN 仅完全匹配字词
-x, --line-regexp 强制 PATTERN 仅完全匹配一行
-z, --null-data 一个 0 字节的数据行,但不是空行
Miscellaneous:
-s, --no-messages suppress error messages
-v, --invert-match select non-matching lines
-V, --version display version information and exit
--help display this help text and exit
输出控制:
-m, --max-count=NUM NUM 次匹配后停止
-b, --byte-offset 输出的同时打印字节偏移
-n, --line-number 输出的同时打印行号
--line-buffered 每行输出清空
-H, --with-filename 为每一匹配项打印文件名
-h, --no-filename 输出时不显示文件名前缀
--label=LABEL 将LABEL 作为标准输入文件名前缀
-o, --only-matching show only the part of a line matching PATTERN
-q, --quiet, --silent suppress all normal output
--binary-files=TYPE assume that binary files are TYPE;
TYPE is 'binary', 'text', or 'without-match'
-a, --text equivalent to --binary-files=text
-I equivalent to --binary-files=without-match
-d, --directories=ACTION how to handle directories;
ACTION is 'read', 'recurse', or 'skip'
-D, --devices=ACTION how to handle devices, FIFOs and sockets;
ACTION is 'read' or 'skip'
-r, --recursive like --directories=recurse
-R, --dereference-recursive
likewise, but follow all symlinks
--include=FILE_PATTERN
search only files that match FILE_PATTERN
--exclude=FILE_PATTERN
skip files and directories matching FILE_PATTERN
--exclude-from=FILE skip files matching any file pattern from FILE
--exclude-dir=PATTERN directories that match PATTERN will be skipped.
-L, --files-without-match print only names of FILEs containing no match
-l, --files-with-matches print only names of FILEs containing matches
-c, --count print only a count of matching lines per FILE
-T, --initial-tab make tabs line up (if needed)
-Z, --null print 0 byte after FILE name
文件控制:
-B, --before-context=NUM 打印以文本起始的NUM 行
-A, --after-context=NUM 打印以文本结尾的NUM 行
-C, --context=NUM 打印输出文本NUM 行
-NUM same as --context=NUM
--group-separator=SEP use SEP as a group separator
--no-group-separator use empty string as a group separator
--color[=WHEN],
--colour[=WHEN] use markers to highlight the matching strings;
WHEN is 'always', 'never', or 'auto'
-U, --binary do not strip CR characters at EOL (MSDOS/Windows)
-u, --unix-byte-offsets report offsets as if CRs were not there
(MSDOS/Windows)
‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。
直接使用‘egrep’或是‘fgrep’均已不可行了。
若FILE 为 -,将读取标准输入。不带FILE,读取当前目录,除非命令行中指定了-r 选项。
如果少于两个FILE 参数,就要默认使用-h 参数。
如果有任意行被匹配,那退出状态为 0,否则为 1;
如果有错误产生,且未指定 -q 参数,那退出状态为 2。
请将错误报告给: bug-grep@gnu.org
GNU Grep 主页: <http://www.gnu.org/software/grep/>
GNU 软件的通用帮助: <http://www.gnu.org/gethelp/>
|
实际使用
准备好一个小故事txt:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@iz2ze76ybn73dvwmdij06zz ~]# cat monkey
One day,a little monkey is playing by the well.一天,有只小猴子在井边玩儿.
He looks in the well and shouts :它往井里一瞧,高喊道:
“Oh!My god!The moon has fallen into the well!” “噢!我的天!月亮掉到井里头啦!”
An older monkeys runs over,takes a look,and says,一只大猴子跑来一看,说,
“Goodness me!The moon is really in the water!” “糟啦!月亮掉在井里头啦!”
And olderly monkey comes over.老猴子也跑过来.
He is very surprised as well and cries out:他也非常惊奇,喊道:
“The moon is in the well.” “糟了,月亮掉在井里头了!”
A group of monkeys run over to the well .一群猴子跑到井边来,
They look at the moon in the well and shout:他们看到井里的月亮,喊道:
“The moon did fall into the well!Come on!Let’get it out!”
“月亮掉在井里头啦!快来!让我们把它捞起来!”
Then,the oldest monkey hangs on the tree up side down ,with his feet on the branch .
然后,老猴子倒挂在大树上,
And he pulls the next monkey’s feet with his hands.拉住大猴子的脚,
All the other monkeys follow his suit,其他的猴子一个个跟着,
And they join each other one by one down to the moon in the well.
它们一只连着一只直到井里.
Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢
He yells excitedly “Don’t be so foolish!The moon is still in the sky!”
它兴奋地大叫:“别蠢了!月亮还好好地挂在天上呢!
|
直接查找符合条件的行
1
2
3
4
5
6
7
8
9
|
[root@iz2ze76ybn73dvwmdij06zz ~]# grep moon monkey
“Oh!My god!The moon has fallen into the well!” “噢!我的天!月亮掉到井里头啦!”
“Goodness me!The moon is really in the water!” “糟啦!月亮掉在井里头啦!”
“The moon is in the well.” “糟了,月亮掉在井里头了!”
They look at the moon in the well and shout:他们看到井里的月亮,喊道:
“The moon did fall into the well!Come on!Let’get it out!”
And they join each other one by one down to the moon in the well.
Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢
He yells excitedly “Don’t be so foolish!The moon is still in the sky!”
|
查找反向符合条件的行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -v moon monkey
One day,a little monkey is playing by the well.一天,有只小猴子在井边玩儿.
He looks in the well and shouts :它往井里一瞧,高喊道:
An older monkeys runs over,takes a look,and says,一只大猴子跑来一看,说,
And olderly monkey comes over.老猴子也跑过来.
He is very surprised as well and cries out:他也非常惊奇,喊道:
A group of monkeys run over to the well .一群猴子跑到井边来,
“月亮掉在井里头啦!快来!让我们把它捞起来!”
Then,the oldest monkey hangs on the tree up side down ,with his feet on the branch .
然后,老猴子倒挂在大树上,
And he pulls the next monkey’s feet with his hands.拉住大猴子的脚,
All the other monkeys follow his suit,其他的猴子一个个跟着,
它们一只连着一只直到井里.
它兴奋地大叫:“别蠢了!月亮还好好地挂在天上呢!”
|
直接查找符合条件的行数
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -c moon monkey
8
|
忽略大小写查找符合条件的行数
先来看一下直接查找的结果
1
|
[root@iz2ze76ybn73dvwmdij06zz ~]# grep my monkey
|
忽略大小写查看
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -i my monkey
“Oh!My god!The moon has fallen into the well!” “噢!我的天!月亮掉到井里头啦!”
|
查找符合条件的行并输出行号
1
2
3
4
5
6
7
8
9
|
[root@iz2ze76ybn73dvwmdij06zz ~]# grep -n monkey monkey
1:One day,a little monkey is playing by the well.一天,有只小猴子在井边玩儿.
4:An older monkeys runs over,takes a look,and says,一只大猴子跑来一看,说,
6:And olderly monkey comes over.老猴子也跑过来.
9:A group of monkeys run over to the well .一群猴子跑到井边来,
13:Then,the oldest monkey hangs on the tree up side down ,with his feet on the branch .
15:And he pulls the next monkey’s feet with his hands.拉住大猴子的脚,
16:All the other monkeys follow his suit,其他的猴子一个个跟着,
19:Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢
|
查找开头是J的行
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# grep '^J' monkey
Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢
|
查找结尾是呢的行
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# grep "呢$" monkey
Just before they reach the moon,the oldest monkey raises his head and happens to see the moon in the sky,正好他们摸到月亮的时候,老猴子抬头发现月亮挂在天上呢
|
sed
sed是一种流编辑器,是一款处理文本比较优秀的工具,可以结合正则表达式一起使用。
sed执行过程

sed命令
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
|
[root@localhost test-delete]# sed
用法: sed [选项]... {脚本(如果没有其他脚本)} [输入文件]...
-n, --quiet, --silent
取消自动打印模式空间
-e 脚本, --expression=脚本
添加“脚本”到程序的运行列表
-f 脚本文件, --file=脚本文件
添加“脚本文件”到程序的运行列表
--follow-symlinks
直接修改文件时跟随软链接
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if SUFFIX supplied)
-c, --copy
use copy instead of rename when shuffling files in -i mode
-b, --binary
does nothing; for compatibility with WIN32/CYGWIN/MSDOS/EMX (
open files in binary mode (CR+LFs are not treated specially))
-l N, --line-length=N
指定“l”命令的换行期望长度
--posix
关闭所有 GNU 扩展
-r, --regexp-extended
在脚本中使用扩展正则表达式
-s, --separate
将输入文件视为各个独立的文件而不是一个长的连续输入
-u, --unbuffered
从输入文件读取最少的数据,更频繁的刷新输出
-z, --null-data
separate lines by NUL characters
--help
display this help and exit
--version
output version information and exit
如果没有 -e, --expression, -f 或 --file 选项,那么第一个非选项参数被视为
sed脚本。其他非选项参数被视为输入文件,如果没有输入文件,那么程序将从标准
输入读取数据。
GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
|
替换
查看文件:
1
2
3
4
5
6
|
[root@loclhost test-delete]# cat word
nkle, twinkle, little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
When the blazing sun is gone
|
替换:
1
2
3
4
5
6
|
[root@localhost test-delete]# sed 's/little/big/' word
nkle, twinkle, big star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
When the blazing sun is gone
|
查看文本:
1
2
3
4
5
6
7
|
[root@localhost test-delete]# cat word1
Oh if there's one thing to be taught
it's dreams are made to be caught
and friends can never be bought
Doesn't matter how long it's been
I know you'll always jump in
'Cause we don't know how to quit
|
全局替换:
1
2
3
4
5
6
7
|
[root@localhost test-delete]# sed 's/to/can/g' word1
Oh if there's one thing can be taught
it's dreams are made can be caught
and friends can never be bought
Doesn't matter how long it's been
I know you'll always jump in
'Cause we don't know how can quit
|
按行替换(替换2到最后一行)
1
2
3
4
5
6
7
|
[root@localhost test-delete]# sed '2,$s/to/can/' word1
Oh if there's one thing to be taught
it's dreams are made can be caught
and friends can never be bought
Doesn't matter how long it's been
I know you'll always jump in
'Cause we don't know how can quit
|
删除
查看文本:
1
2
3
4
5
6
|
[root@localhost test-delete]# cat word
Twinkle, twinkle, little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky
When the blazing sun is gone
|
删除:
1
2
3
4
5
|
[root@localhost test-delete]# sed '2d' word
Twinkle, twinkle, little star
Up above the world so high
Like a diamond in the sky
When the blazing sun is gone
|
显示行号:
1
2
3
4
5
6
7
8
9
10
|
[root@localhost test-delete]# sed '=;2d' word
1
Twinkle, twinkle, little star
2
3
Up above the world so high
4
Like a diamond in the sky
5
When the blazing sun is gone
|
删除第2行到第四行:
1
2
3
4
5
6
7
8
|
[root@localhost test-delete]# sed '=;2,4d' word
1
Twinkle, twinkle, little star
2
3
4
5
When the blazing sun is gone
|
添加行:
向前插入:
1
2
3
|
[root@localhost test-delete]# echo "hello" | sed 'i\kitty'
kitty
hello
|
向后插入:
1
2
3
|
[root@localhost test-delete]# echo "kitty" | sed 'i\hello'
hello
kitty
|
修改行:
替换第二行为hello kitty
1
2
3
4
5
6
|
[root@localhost test-delete]# sed '2c\hello kitty' word
Twinkle, twinkle, little star
hello kitty
Up above the world so high
Like a diamond in the sky
When the blazing sun is gone
|
替换第二行到最后一行为hello kitty
1
2
3
|
[root@localhost test-delete]# sed '2,$c\hello kitty' word
Twinkle, twinkle, little star
hello kitty
|
写入行
把带star的行写入c文件中,c提前创建
1
2
3
|
[root@localhost test-delete]# sed -n '/star/w c' word
➜ happy cat c
Twinkle, twinkle, little star
|
退出
打印3行后,退出sed
1
2
3
4
|
[root@localhost test-delete]# sed '3q' word
Twinkle, twinkle, little star
How I wonder what you are
Up above the world so high
|
awk
名字由来
创始人 Alfred Aho 、Peter Weinberger 和 Brian Kernighan 姓氏的首个字母。
强大的文本处理工具
比起sed和grep,awk不仅仅是一个小工具,也可以算得上一种小型的编程语言了,支持if判断分支和while循环语句还有它的内置函数等,是一个要比grep和sed更强大的文本处理工具,但也就意味着要学习的东西更多了。
下面来说一下awk的一些基础概念以及实际操作。
语法
Usage: awk [POSIX or GNU style options] -f progfile [–] file …
Usage: awk [POSIX or GNU style options] [–] ‘program’ file …
域
类似数据库列的概念,但它是按照序号来指定的,比如我要第一个列就是2,依此类推。$0就是输出整个文本的内容。默认用空格作为分隔符,当然你可以自己通过-F设置适合自己情况的分隔符。
提前自己编了一段数据,学生以及学生成绩数据表。
“Name Math Chinese English History Sport grade” 输出整个文本
1
2
3
4
5
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '{print $0}' students_store
Xiaoka 60 80 40 90 77 class-1
Yizhihua 70 66 50 80 90 class-1
kerwin 80 90 60 70 60 class-2
Fengzheng 90 78 62 40 62 class-2
|
输出第一列(姓名列)
1
2
3
4
5
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '{print $1}' students_store
Xiaoka
Yizhihua
kerwin
Fengzheng
|
模式&动作
awk '{[pattern] action}' {filenames}
模式
pattern 可以是
模式的两个特殊字段 BEGIN 和 END (不指定时匹配或打印行数)
- BEGIN : 一般用来打印列名称。
- END : 一般用来打印总结性质的字符。
动作
action 在{}内指定,一般用来打印,也可以是一个代码段。
示例
给上面的文本加入标题头:
1
2
3
4
5
6
7
8
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print "Name Math Chinese English History Sport grade\n----------------------------------------------"} {print $0}' students_store
Name Math Chinese English History Sport grade
----------------------------------------------------------
Xiaoka 60 80 40 90 77 class-1
Yizhihua 70 66 50 80 90 class-1
kerwin 80 90 60 70 60 class-2
Fengzheng 90 78 62 40 62 class-2
|
仅打印姓名、数学成绩、班级信息,再加一个文尾(再接再厉):
1
2
3
4
5
6
7
8
9
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print "Name Math grade\n---------------------"} {print $1 2 "\t" $7} END {print "continue to exert oneself"}' students_store
Name Math grade
---------------------
Xiaoka 60 class-1
Yizhihua 70 class-1
kerwin 80 class-2
Fengzheng 90 class-2
continue to exert oneself
|
结合正则
像grep和sed也是支持正则表达式的。这边就不介绍正则表达式了,如果有兴趣,我单出一个文章。
使用方法:
符号 ~ 后接正则表达式
此时我们再加入一条后来的新同学,并且没有分班。
先来看下现在的数据
1
2
3
4
5
6
|
[root@iz2ze76ybn73dvwmdij06zz ~]# cat students_store
Xiaoka 60 80 40 90 77 class-1
Yizhihua 70 66 50 80 90 class-1
kerwin 80 90 60 70 60 class-2
Fengzheng 90 78 62 40 62 class-2
xman - - - - - -
|
模糊匹配|查询已经分班的学生
1
2
3
4
5
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '$0 ~/class/' students_store
Xiaoka 60 80 40 90 77 class-1
Yizhihua 70 66 50 80 90 class-1
kerwin 80 90 60 70 60 class-2
Fengzheng 90 78 62 40 62 class-2
|
精准匹配|查询1班的学生
1
2
3
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '$7=="class-1" {print $0}' students_store
Xiaoka 60 80 40 90 77 class-1
Yizhihua 70 66 50 80 90 class-1
|
反向匹配|查询不是1班的学生
1
2
3
4
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '$7!="class-1" {print $0}' students_store
kerwin 80 90 60 70 60 class-2
Fengzheng 90 78 62 40 62 class-2
xman - - - - - -
|
比较操作
查询数学大于80的
1
2
3
4
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '$2>60 {print $0}' students_store
Yizhihua 70 66 50 80 90 class-1
kerwin 80 90 60 70 60 class-2
Fengzheng 90 78 62 40 62 class-2
|
查询数学大于英语成绩的
1
2
3
4
5
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '$2 > $4 {print $0}' students_store
Xiaoka 60 80 40 90 77 class-1
Yizhihua 70 66 50 80 90 class-1
kerwin 80 90 60 70 60 class-2
Fengzheng 90 78 62 40 62 class-2
|
匹配指定字符中的任意字符
在加一列专业,让我们来看看憨憨们的专业,顺便给最后一个新来的同学分个班吧。
然后再来看下此时的数据。
1
2
3
4
5
6
|
[root@iz2ze76ybn73dvwmdij06zz ~]# cat students_store
Xiaoka 60 80 40 90 77 class-1 Java
Yizhihua 70 66 50 80 90 class-1 java
kerwin 80 90 60 70 60 class-2 Java
Fengzheng 90 78 62 40 62 class-2 java
xman - - - - - class-3 php
|
或关系匹配|查询1班和3班的学生
1
2
3
4
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '$0 ~/(class-1|class-3)/' students_store
Xiaoka 60 80 40 90 77 class-1 Java
Yizhihua 70 66 50 80 90 class-1 java
xman - - - - - class-3 php
|
任意字符匹配|名字第二个字母是
字符解释:
^ : 字段或记录的开头。
. : 任意字符。
1
2
3
4
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '$0 ~/(class-1|class-3)/' students_store
Xiaoka 60 80 40 90 77 class-1 Java
Yizhihua 70 66 50 80 90 class-1 java
xman - - - - - class-3 php
|
复合表达式
&& AND
的关系,必同时满足才行哦~
查询数学成绩大于60并且语文成绩也大于60的童鞋。
1
2
3
4
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '{ if ($2 > 60 && $3 > 60) print $0}' students_store
Yizhihua 70 66 50 80 90 class-1 java
kerwin 80 90 60 70 60 class-2 Java
Fengzheng 90 78 62 40 62 class-2 java
|
|| OR
查询数学大于80或者语文大于80的童鞋。
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '{ if ($2 > 80 || $4 > 80) print $0}' students_store
Fengzheng 90 78 62 40 62 class-2 java
|
printf 格式化输出
除了能达到功能以外,一个好看的格式也是必不可少的,因此格式化的输出看起来会更舒服哦~
语法
1
2
|
printf ([格式],参数)
printf %x(格式) 具体参数 x代表具体格式
|
| 符号 |
说明 |
| - |
左对齐 |
| Width |
域的步长 |
| .prec |
最大字符串长度或小数点右边位数 |
格式转化符
其实和其他语言大同小异的
常用格式
| 符号 |
描述 |
| %c |
ASCII |
| %d |
整数 |
| %o |
八进制 |
| %x |
十六进制数 |
| %f |
浮点数 |
| %e |
浮点数(科学记数法) |
| %s |
字符串 |
| %g |
决定使用浮点转化e/f |
具体操作示例
ASCII码🐎
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# echo "66" | awk '{printf "%c\n",$0}'
B
|
浮点数
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {printf "%f\n",100}'
100.000000
|
16进制数
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {printf "%x",996}'
3e4
|
内置变量
频率较高常用内置变量
NF : 记录浏览域的个数,在记录被读后设置。
NR : 已读的记录数。
FS : 设置输入域分隔符
A R G C : 命令行参数个数,支持命令行传入。
RS : 控制记录分隔符
FIlENAME : awk当前读文件的名称
操作
输出学生成绩表和域个数以及已读记录数。
1
2
3
4
5
6
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk '{print $0, NF , NR}' students_store
Xiaoka 60 80 40 90 77 class-1 Java 8 1
Yizhihua 70 66 50 80 90 class-1 java 8 2
kerwin 80 90 60 70 60 class-2 Java 8 3
Fengzheng 90 78 62 40 62 class-2 java 8 4
xman - - - - - class-3 php 8 5
|
内置函数
常用函数
length(s) 返回s长度
index(s,t) 返回s中字符串t第一次出现的位置
match (s,r) s中是否包含r字符串
split(s,a,fs) 在fs上将s分成序列a
gsub(r,s) 用s代替r,范围全文本
gsub(r,s,t) 范围t中,s代替r
substr(s,p) 返回字符串s从第p个位置开始后面的部分(下标是从1 开始算的,大家可以自己试试)
substr(s,p,n) 返回字符串s从第p个位置开始后面n个字符串的部分
操作
length
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print length(" hello,im xiaoka")}'
16
|
index
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print index("xiaoka","ok")}'
4
|
match
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print match("Java小咖秀","va小")}'
3
|
gsub
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'gsub("Xiaoka","xk") {print $0}' students_store
xk 60 80 40 90 77 class-1 Java
|
substr(s,p)
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print substr("xiaoka",3)}'
aoka
|
substr(s,p,n)
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN {print substr("xiaoka",3,2)}'
ao
|
split
1
2
3
4
5
6
|
[root@iz2ze76ybn73dvwmdij06zz ~]# str="java,xiao,ka,xiu"
[root@iz2ze76ybn73dvwmdij06zz ~]# awk 'BEGIN{split('"\"$str\""',ary,","); for(i in ary) {if(ary[i]>1) print ary[i]}}'
xiu
java
xiao
ka
|
awk脚本
前面说过awk是可以说是一个小型编程语言。如果命令比较短我们可以直接在命令行执行,当命令行比较长的时候,可以使用脚本来处理,比命令行的可读性更高,还可以加上注释。
写一个完整的awk脚本并执行步骤
1.先创建一个awk文件
1
|
[root@iz2ze76ybn73dvwmdij06zz ~]# vim printname.awk
|
2.脚本第一行要指定解释器
3.编写脚本内容,打印一下名称
1
2
3
4
|
[root@iz2ze76ybn73dvwmdij06zz ~]# cat printname.awk
#!/usr/bin/awk -f
#可以加注释了,哈哈
BEGIN { print "my name is Java小咖秀"}
|
4.既然是脚本,必不可少的可执行权限安排上~
1
2
3
|
[root@iz2ze76ybn73dvwmdij06zz ~]# chmod +x printname.awk
[root@iz2ze76ybn73dvwmdij06zz ~]# ll printname.awk
-rwxr-xr-x 1 root root 60 7月 1 15:23 printname.awk
|
5.有了可执行权限,我们来执行下看结果
1
2
|
[root@iz2ze76ybn73dvwmdij06zz ~]# ./printname.awk
my name is Java小咖秀
|
作者:Java小咖秀
链接:https://juejin.cn/post/6845166891493785613
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。