Linux命令之 grep、sed、awk

目录

一、grep

1、参数:

2、示例:

3、grep 正则匹配示例

二、sed

示例:

三、awk

一、语法:

二、常用参数

 三、示例

四、条件与循环语句

五、awk调用函数


1、grep:过滤文本,过滤出文本中符合要求的行

2、sed:修改文本,对文本中的行增,删,改,查

3、awk:处理文本,文本中的行和列进行处理

grep 经常与sed、awk合用

grep、sed、awk、经常与sort、uniq、wc等命令一起使用

文本三剑客支持正则表达式,因此学习之前建议先学一下正则表达式

一、grep

        作用:文本搜索工具,根据用户指定的“模式(过滤条件)”对目标文本逐行进行匹配检查,打印匹配到的行。

1、参数:

-i:忽略大小写
-n:显示行号
-c:显示行数
-e:支持基础的正则表达式 多条件过滤 or  and 
-E:支持扩展的
-v:反转查找,输出与模式不相符的行 
-r:递归搜索所有文件
-d:过滤文件
-o:只显示匹配的内容
-A: 找到匹配行以及后几行 比如-A2(后两行)-A5(后5行)
-B:输出匹配行以及前几行 比如:-B2(前两行) -B5(前5行)
-C:既要显示前几行 又要显示后几行 比如:-C5(当前行的前后5行)
-a:不要忽略二进制数据
-l:列出所包含的文件
-L:列出不包含的文件

2、示例:

[root@k8s-lb-backup tmp]# cat grepdemo.txt 
hello word
Hi wuz\haobo
hello "hahaha" hehehe
L%ala'l'a
123/45/6

1、过滤 h 开头的行,忽略大小写:-i、显示行号 -n
    [root@k8s-lb-backup tmp]# grep '^h' -i -n grepdemo.txt 
    1:hello word
    2:Hi wuzhaobo
    3:hello hahaha hehehe

2、使用正则表达式的转义字符,过滤有双引号的行,使用单引号 '' 或者转义符 \ 或者使用 -F
    [root@k8s-lb-backup tmp]# grep '"' grepdemo.txt 
    hello "hahaha" hehehe
    [root@k8s-lb-backup tmp]# grep \" grepdemo.txt 
    hello "hahaha" hehehe
    [root@k8s-lb-backup tmp]# grep '/' grepdemo.txt 
    123/45/6
    [root@k8s-lb-backup tmp]# grep '\\' grepdemo.txt 
    Hi wuz\haobo
    [root@k8s-lb-backup tmp]# grep -F 'z\h' grepdemo.txt 
    Hi wuz\haobo
    [root@k8s-lb-backup tmp]# grep \'l\' grepdemo.txt 
    L%ala'l'a

3、过滤二进制文件 使用 -a 参数
    [root@k8s-lb-backup tmp]# printf 'hello word' |grep hello
    hello word
    [root@k8s-lb-backup tmp]# 
    [root@k8s-lb-backup tmp]# printf 'hello word\0' |grep hello
    匹配到二进制文件 (标准输入)

    [root@k8s-lb-backup tmp]# printf 'hello word\0' |grep -a hello
    hello word

4、过滤文件的前两行、后两行、前后各两行
    [root@k8s-lb-backup tmp]# grep -B2 'hah' grepdemo.txt 
    hello word
    Hi wuz\hao\bo
    hello "hahaha" hehehe
    [root@k8s-lb-backup tmp]# grep -A2 'hah' grepdemo.txt 
    hello "hahaha" hehehe
    L%ala'l'a
    123/45/6
    [root@k8s-lb-backup tmp]# grep -C2 'hah' grepdemo.txt 
    hello word
    Hi wuz\hao\bo
    hello "hahaha" hehehe
    L%ala'l'a
    123/45/6

5、过条件过滤 使用-e 参数 既包含 hello 也包含 wuz
    [root@k8s-lb-backup tmp]# grep -i -e 'hello' -e 'wuz' grepdemo.txt 
    hello word
    Hi wuz\hao\bo
    hello "hahaha" hehehe
    [root@k8s-lb-backup tmp]# grep -i hello grepdemo.txt |grep heh
    hello "hahaha" hehehe

6、反向查找,ps aux 过滤bash  使用 -v 参数
    [root@k8s-lb-backup tmp]# ps aux | grep bash
    root        739  0.0  0.0 115404   964 ?        S    09:35   0:00 /bin/bash /usr/sbin/ksmtuned
    root       1994  0.0  0.0 116332  3064 pts/0    Ss   09:36   0:00 -bash
    root     103695  0.0  0.0 112828   988 pts/0    S+   14:09   0:00 grep --color=auto bash
    [root@k8s-lb-backup tmp]# ps aux | grep bash | grep -v grep | grep -v ksmtuned
    root       1994  0.0  0.0 116332  3064 pts/0    Ss   09:36   0:00 -bash

7、列出所包含的文件:使用-l参数,列出不包含的文件 使用-L 参数 使用 -r 可以递归过滤目录
    [root@k8s-lb-backup tmp]# grep -l hello *.txt
    1.txt
    grepdemo.txt
    [root@k8s-lb-backup tmp]# grep -L hello *.txt

8、grep 管道合用
    [root@k8s-lb-backup tmp]# ps aux | grep bash

9、grep 与 wc (统计数量)和uniq(相邻去重)和sort(排序)合用
    [root@k8s-lb-backup tmp]# echo 'hello world' |wc
          1(行)       2(个单词)      12(字符)
    [root@k8s-lb-backup tmp]# echo 'hello wrold' |wc -c 
    12(直接统计字符)
    [root@k8s-lb-backup tmp]# echo 'hello wrold' |wc -l
    1(统计行数)
    [root@k8s-lb-backup tmp]# echo 'hello wrold' |wc -w
    2(统计单词)
    [root@k8s-lb-backup tmp]# grep -i -o hello grepdemo.txt | uniq  (去重)
    hello 
    [root@k8s-lb-backup tmp]# grep -i -o hello grepdemo.txt | sort -u    排序并去重使用 -u 参数
    hello

10、xargs的使用:格式化输出
    [root@k8s-lb-backup tmp]# cat 2.txt 
    a b c d e f g
    h i g k l m n
    o p q
    r s t u v w
    x y z
    [root@k8s-lb-backup tmp]# cat 2.txt | xargs
    a b c d e f g h i g k l m n o p q r s t u v w x y z
    [root@k8s-lb-backup tmp]# cat 2.txt | xargs | wc
          1      26      52
    [root@k8s-lb-backup tmp]# cat 2.txt | xargs -n 5  每行以5列输出
    a b c d e
    f g h i g
    k l m n o
    p q r s t
    u v w x y
    z

11、grep 过滤进程 并与awk合用 kill 杀死进程
    [root@k8s-lb-backup tmp]# ps aux | grep bash
    root        762  0.0  0.0 115404   956 ?        S    09:25   0:00 /bin/bash /usr/sbin/ksmtuned
    root       1897  0.0  0.0 116332  3032 pts/0    Ss   09:26   0:00 -bash
    root       6444  0.0  0.0 112828   988 pts/0    S+   09:38   0:00 grep --color=auto bash
    [root@k8s-lb-backup tmp]# ps aux | grep bash | grep -v grep | grep -v ksmtuned 
    root       1897  0.0  0.0 116332  3032 pts/0    Ss   09:26   0:00 -bash
    [root@k8s-lb-backup tmp]# ps aux | grep bash | grep -v grep | grep -v ksmtuned | awk '{print $2}'
    1897
    [root@k8s-lb-backup tmp]# ps aux | grep bash | grep -v grep | grep -v ksmtuned | awk '{print "kill -9" $2} | sh
    [root@k8s-lb-backup tmp]# kill -9 `ps aux | grep bash | grep -v grep | grep -v ksmtuned | awk '{print $2}'`

12、grep 与 sed 合用,查找本机IP地址

    先[root@k8s-lb-backup tmp]# ifconfig   
    在一层一层过滤 
    [root@k8s-lb-backup tmp]# ifconfig ens33
    [root@k8s-lb-backup tmp]# ifconfig ens33 | grep inet
        inet 192.168.134.141  netmask 255.255.255.0  broadcast 192.168.134.255
        inet6 fe80::e8e1:227e:c151:d3b6  prefixlen 64  scopeid 0x20<link>

    [root@k8s-lb-backup tmp]# ifconfig ens33 | grep inet | grep -v inet6
        inet 192.168.134.141  netmask 255.255.255.0  broadcast 192.168.134.255

    # 把inet之前的替换成空
    [root@k8s-lb-backup tmp]# ifconfig ens33 | grep inet | grep -v inet6 | sed 's/^.*inet//g'
        192.168.134.141  netmask 255.255.255.0  broadcast 192.168.134.255
    #再把netmask之后的替换成空
    [root@k8s-lb-backup tmp]# ifconfig ens33 | grep inet | grep -v inet6 | sed 's/^.*inet//g' | sed 's/netmask.*$//g'
        192.168.134.141

3、grep 正则匹配示例

1、匹配以 'he' 开头的行
[root@k8s-lb-backup tmp]# grep ^he grepdemo.txt 
hello word
hello "hahaha" hehehe

2、匹配以'6'结尾的行
[root@k8s-lb-backup tmp]# grep 6$ grepdemo.txt 
123/45/6

3、匹配一个非换行字符 使用  . .表示任意字符
[root@k8s-lb-backup tmp]# grep w.rd grepdemo.txt 
hello word

4、匹配前一个字符0次或者1次以上
[root@k8s-lb-backup tmp]# grep hel* grepdemo.txt 
hello word
hello "hahaha" hehehe

5、匹配任意字符
[root@k8s-lb-backup tmp]# cat grepdemo.txt 
hello word
Hi wuz\hao\bo
hello "hahaha" hehehe
L%ala'l'a
mysqla
mysqlb
mysqlA
mysqlB
mysql3
mysql5

[root@k8s-lb-backup tmp]# grep mysql[a-zA-Z0-9] grepdemo.txt 
mysqla
mysqlb
mysqlA
mysqlB
mysql3
mysql5

6、某一行以wo开头 和以rd结尾的字符
[root@k8s-lb-backup tmp]# grep '\<wo' grepdemo.txt 
hello word
[root@k8s-lb-backup tmp]# grep 'rd\>' grepdemo.txt 
hello word

二、sed

        sed 是对文本文件中的某一行进行增删改查等操作,尝尝使用sed对文件的指定内容进行批量替换。

语法:
sed [options] 'asd [flags]' filename
命令参数
-f:后面跟一个文本文件,表示将sed的动作卸载一个文件内
-i:直接修改文件内容,sed加上此参数后对文件的修改会生效
-r:支持扩展正则表达式 
-n:只打印匹配到的行,经常与 -p 一起使用
-e:逻辑和 默认选项
sed 常用的内部命令:
 d:删除匹配的行,并立即启用下一轮循环
 p:打印当前模式空间内容,追加到默认输出之后
 a:在指定行后面追加文本,支持使用\n实现多行追加
 i:在行前面插入文本,支持使用\n实现多行追加
 c:替换行为单行或多行文本,支持使用\n实现多行追加
 w:保存模式匹配的行至指定文件
 r:读取指定文件的文本至模式空间中匹配到的行后
 =:为模式空间中的行打印行号
 !:模式空间中匹配行取反处理
 s///:查找替换,支持使用其它分隔符,如:s@@@,s###;
     加g表示行内全局替换;
     在替换时,可以加一下命令,实现大小写转换
     \l:把下个字符转换成小写。
     \L:把replacement字母转换成小写,直到\U或\E出现。
     \u:把下个字符转换成大写。
     \U:把replacement字母转换成大写,直到\L或\E出现。
     \E:停止以\L或\U开始的大小写转换
flags:
    数字:表示新文本替换的模式
    g:全局  全部
    p:表示打印原始的内容
    w filename :表示将替换的结果写入文件
示例:
sed -e '1s/test/trial/g' test.txt
1:表示 test.txt 的第一行
s:表示 替换
test 是原内容
trial 是替换成的内容
g 整行 替换

示例:

一、替换
[root@k8s-lb-backup tmp]# cat seddemo.txt 
This is a test of the trial script.
This is the second test of the trial script.

1、将文本中第一行的 test 替换成 trial,并打印出来,但是不会修改文件内容,如果要修改文件内容 加上 -i 参数
    [root@k8s-lb-backup tmp]# sed -e '1s/test/trial/g' seddemo.txt 
    This is a trial of the trial script.
    This is the second test of the trial script.

2、全局替换并写入文件 
    [root@k8s-lb-backup tmp]# sed -i 's/test/trial/g' seddemo.txt

3、把第二行的第一个替换
    [root@k8s-lb-backup tmp]# sed '2s/test/asd/1' seddemo.txt 
    This is a trial of the trial script.
    This is the second asd of the trial script.

二、删除(d)和添加(a,i)
1、删除所有
    [root@k8s-lb-backup tmp]# sed 'd' seddemo.txt 

2、删除1行
    [root@k8s-lb-backup tmp]# sed '1d' seddemo.txt 
    This is the second test of the trial script.

3、删除1-2行
    [root@k8s-lb-backup tmp]# sed '1,2d' seddemo.txt 

4、删除2-4行
    [root@k8s-lb-backup tmp]# sed '2,4d' seddemo.txt

5、在第2行前面插入
    [root@k8s-lb-backup tmp]# sed '2i my name is wuzhaobo' seddemo.txt 
    This is a trial of the trial script.
    my name is wuzhaobo
    This is the second test of the trial script.

6、在第2行后面插入
    [root@k8s-lb-backup tmp]# sed '2a my name is wuzhaobo' seddemo.txt 
    This is a trial of the trial script.
    This is the second test of the trial script.
    my name is wuzhaobo

7、在第一行前面插入2行
    [root@k8s-lb-backup tmp]# sed '1i my name is wuzhaobo.\nthis is two hang' seddemo.txt 
    my name is wuzhaobo.
    this is two hang
    This is a trial of the trial script.
    This is the second test of the trial script.

三、sed 替换 c 参数
1、把第二行替换
    [root@k8s-lb-backup tmp]# sed '2c this is line number 2.' seddemo.txt 
    This is a trial of the trial script.
    this is line number 2.

四、sed 转换 y 参数
1、把文件里的a转换成大写A 参数是一一  对应的。
    [root@k8s-lb-backup tmp]# sed 'y/a/A/' seddemo.txt 
    This is A triAl of the triAl script.
    This is the second test of the triAl script.
    [root@k8s-lb-backup tmp]# echo 'this is 1' | sed 'y/123/456/'
    this is 4

五、sed p 打印命令
[root@k8s-lb-master tmp]# vim demo
aaa
bbbb
AABBCCDD
1、只打印匹配到的行
    [root@k8s-lb-master tmp]# sed -n "/aaa/p" demo 
    aaa
2、打印第2行
    [root@k8s-lb-master tmp]# sed -n "2p" demo 
    Bbbb
3、先把包含3的这一行打印出来,然后在把包含3的这一行中的bbbb替换成1111 ,在打印出来
    [root@k8s-lb-backup tmp]# sed -n '/3/{
    > p
    > s/bbbb/1111/p
    > }' seddemo.txt
    bbbb3
    11113

六、sed q 退出脚本
1、查找文件中 aaa 这一行 ,并把aaa替换成111,然后退出。
    [root@k8s-lb-backup tmp]# sed '/aaa/{s/aaa/111/;q}' seddemo.txt 
    111
把TXT中的h1、h2、h3加上尖括号
[root@k8s-lb-backup tmp]# cat test.txt 
<html>
<title>First Web</title>
<body>
h1Hello1h1
h2Hello2h2
h3hello3h3
</body>
</html>
1、
[root@k8s-lb-backup tmp]# sed '{s/h[0-9]/\<&\>/1;s/h[0-9]/\<\/&\>/2}' test.txt 
<html>
<title>First Web</title>
<body>
<h1>Hello1</h1>
<h2>Hello2</h2>
<h3>hello3</h3>
</body>
</html>
2、或者,把/h[0-9] 公共的 提到前边
[root@k8s-lb-backup tmp]# sed '/h[0-9]/{s//\<&\>/1;s//\<\/&\>/2}' test.txt 
<html>
<title>First Web</title>
<body>
<h1>Hello1</h1>
<h2>Hello2</h2>
<h3>hello3</h3>
</body>
</html>
讲解:使用替换的放是 s/a/b/1:第一次把a替换成b,多条命令用;隔开,&表示:当前匹配到的内容。2:表示发生在第二个匹配位置
3、-或者使用调用脚本参数 -f 来执行
[root@k8s-lb-backup tmp]# cat a.sh 
/h[0-9]/{
  s//\<&\>/1
  s//\<\/&\>/2
}
[root@k8s-lb-backup tmp]# sed -f a.sh test.txt 
<html>
<title>First Web</title>
<body>
<h1>Hello1</h1>
<h2>Hello2</h2>
<h3>hello3</h3>
</body>
</html>

三、awk

AWK是一种编程语言,用于在linux下对文本和数据进行处理。可以处理每一行的每一列

一、语法:

awk 'BEGIN {commands;……} pattern {commands;……} END {commands}' filename
- BEGIN:处理数据前执行的命令
- END:处理数据后执行的命令
- pattern:模式,每一行都执行的命令
- BEGIN和END里的命令只是执行一次
- pattern:里的命令会匹配每一行去处理

二、常用参数

-F fs:fs指定输入分隔符,fs可以是字符串或正则表达式,如-F: 默认使用 空格
-v var=value:赋值一个用户定义变量,将外部变量传递给awk
-f scripfile:从脚本文件中读取awk命令

内置变量:
    FS :输入字段分隔符,默认为空白字符
    OFS :输出字段分隔符,默认为空白字符
    RS :输入记录分隔符,指定输入时的换行符,原换行符仍有效
    ORS :输出记录分隔符,输出时用指定符号代替换行符
    NF :字段数量,共有多少字段, $NF引用最后一列,$(NF-1)引用倒数第2列
    NR :行号,后可跟多个文件,第二个文件行号继续从第一个文件最后行号开始
    FNR :各文件分别计数, 行号,后跟一个文件和NR一样,跟多个文件,第二个文件行号从1开始
    FILENAME :当前文件名
    ARGC :命令行参数的个数
    ARGV :数组,保存的是命令行所给定的各参数,查看参数
操作符:
    算数操作符:
        x+y, x-y, x*y, x/y, x^y, x%y
        -x:  转换为负数
        +x:  转换为数值
    比较操作符:
        ==, !=, >, >=, <, <=
    模式匹配符:~ :左边是否和右边匹配包含 !~ :不包含
    逻辑操作符:与&& ,或|| ,非!
    函数调用: function_name(argu1, argu2, ...)
    条件表达式(三目表达式):selector?if-true-expression:if-false-expression
     注释:先判断selector,如果符合执行 ? 后的操作;否则执行 : 后的操作

 三、示例

创建awkdemo目录,准备两个测试文件
[root@k8s-lb-backup awkdemo]# cp /etc/passwd ./
[root@k8s-lb-backup awkdemo]# ps aux > test.txt 

1、打印每一行 $:代表是行 $0:表示所有行 $1:表示第一个字段的值 $2:表示第二个字段的值,$NF:表示最后一个字段的值 $(NF-1):表示倒数第二个字段的值
[root@k8s-lb-backup awkdemo]# awk '{print $0}' test.txt

2、打印第一列 $1
[root@k8s-lb-backup awkdemo]# awk '{print $1}' test.txt

3、打印文件前5行 NR<5
    [root@k8s-lb-backup awkdemo]# awk 'NR<5' test.txt 
    USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root          1  0.0  0.1 125396  3912 ?        Ss   09:14   0:01 /usr/lib/sy
    root          2  0.0  0.0      0     0 ?        S    09:14   0:00 [kthreadd]
    root          3  0.0  0.0      0     0 ?        S    09:14   0:00 [ksoftirqd/0]
    打印文件前5行,如果第一个文件行数不够 就从第二个开始
    [root@k8s-lb-backup awkdemo]# awk 'NR<5' test1.txt passwd 
    USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
    root          3  0.0  0.0      0     0 ?        S    09:14   0:00 [ksoftirqd/0]
    root:x:0:0:root:/root:/bin/bash
    bin:x:1:1:bin:/bin:/sbin/nologin

4、打印最后一列字段的值、和倒数第二行的值 $NF $(NF-1)
    [root@k8s-lb-backup awkdemo]# awk '{print $NF}' test.txt
    [root@k8s-lb-backup awkdemo]# awk '{print $(NF-1)}' test.txt

5、指定分隔符,并打印第一个字段的值 -F ':'
    [root@k8s-lb-backup awkdemo]# awk -F ':' '{print $1}' passwd 

6、指定分隔符,并打印包含job的行,或者某个值
    [root@k8s-lb-backup awkdemo]# awk -F : '/job/{print $0}' passwd 
    job:x:1000:1000:job:/home/job:/bin/bash

7、begin 和 end 的使用,以冒号分割,先打印 hello 在打印第一列的值,每一行+1,最后打印一共多少行,和输出 goodbye
    [root@k8s-lb-backup awkdemo]# awk -F : 'BEGIN{ print "hello" }{ print $1;i+=1 } END { print i;print "goodbye"}' passwd 
    或者从脚本中执行   -f
    [root@k8s-lb-backup awkdemo]# vim a.awk
        BEGIN{FS=":"; print "hello" }
        { print $1;i+=1 }
        END { print i;print "goodbye"}
    [root@k8s-lb-backup awkdemo]# awk -f a.awk passwd   
 
8、添加条件,比如查找 passwd 中 pid>uid的行,并打印
    [root@k8s-lb-backup awkdemo]# awk -F: '$4 > $3 {print $0}' passwd

二、操作符示例
1、查询以dev开头的磁盘信息 匹配使用 ~ 符号
    [root@k8s-lb-backup awkdemo]# df -h | awk '$0 ~ /^\/dev/'
    /dev/mapper/centos-root   36G  3.8G   32G   11% /
    /dev/sda1               1014M  179M  836M   18% /boot

2、只打印磁盘名和使用状况 使用 $ 和 NF过滤
    [root@k8s-lb-backup awkdemo]# df -h | awk '$0 ~ /^\/dev/{print $1 "-----"$(NF-1)}'
    /dev/mapper/centos-root-----11%
    /dev/sda1-----18%

3、查找磁盘大于15%的
    [root@k8s-lb-backup awkdemo]# df -h | awk '$0 ~ /^\/dev/{print $1 "="$(NF-1)}' | awk -F= '$2 > 15'
    /dev/sda1=18%

三、逻辑操作符 并且&& 或|| 非!
1、查看passwd 中第三列的值 大于10 并且小于1000 的值
    [root@k8s-lb-backup awkdemo]# awk -F : '$3 > 10 && $3 < 1000 { print $3 }' passwd 

2、查看passwd 中第三列的值 大于10 或者小于1000 的值
    [root@k8s-lb-backup awkdemo]# awk -F : '$3 > 10 || $3 < 1000 { print $3 }' passwd 

3、查看passwd 中第三列的值 不等于1000 的值
    [root@k8s-lb-backup awkdemo]# awk -F : '$3 !=1000 { print $3 }' passwd 

4、查看第四列之和
    [root@k8s-lb-backup awkdemo]# awk -F : '{ sum+=$4 } END {print sum}' passwd 

    76412
5、查看系统 CPU和内存和
    [root@k8s-lb-backup awkdemo]# ps -aux | grep -v USER | awk '{ cpu+=$3;mem+=$4 } END { print cpu;print mem }'
    0
    8.3

四、三目表达式
[root@k8s-lb-backup awkdemo]# awk -F : '{ $3 >= 1000 ? usertype="common user" : usertype="sysadmin user"; print usertype,$1,$3}' passwd

四、条件与循环语句

一、控制语句if-else语法
if(condition){statement;…}[elsestatement]  双分支
if(condition1){statement1}else if(condition2){statement2}else{statement3}  多分支
示例:对passwd 判断 当$3 的UID 当UID=0时,就是超级用户,当UID大于1 小于等于999,就是系统用户,其余的就是普通用户
    [root@k8s-lb-backup awkdemo]# vim a.awk
    BEGIN{
    FS=":"
    }
    {
            if ($3==0){
                print $1 "is super user";
            } else if($3>1 && $3<=999 ){
                print $1 "is system user";
            }else{
                print $1 "is common user";
            }

    }
    [root@k8s-lb-backup awkdemo]# awk -f a.awk passwd
2、每隔5行打印分隔符,就是5的倍数 使用NR%5==0
[root@k8s-lb-backup awkdemo]# awk '{if (NR%5==0){ print "----------"}print $0 }' passwd 
二、awk 循环语句 for
1、对每一行打印5次
    [root@k8s-lb-backup awkdemo]# awk -F : '{for (i=5;i>0;i--){print $0} }' passwd 
2、使用for 循环 1至100的和
    [root@k8s-lb-backup awkdemo]# awk 'BEGIN {sum=0; for(i=1;i<=100;i++){sum+=i}print sum}'
    5050
3、使用while 循环 1至100的和
    [root@k8s-lb-backup awkdemo]# awk 'BEGIN {sum=0;i=1; while(i<=100){sum+=i;i++};print sum}'
    5050
4、使用 do while 循环 1至100的和
    [root@k8s-lb-backup awkdemo]# awk 'BEGIN {sum=0;i=1; do{sum+=i;i++}while(i<=100);print sum}'
    5050
5、使用for循环打印99乘法表
[root@k8s-lb-backup awkdemo]# awk 'BEGIN{for(i=1;i<=9;i++){for(j=1;j<=i;j++)printf i "*" j "=" i*j "\t";print("\n")}}'
1*1=1

2*1=2   2*2=4

3*1=3   3*2=6   3*3=9

4*1=4   4*2=8   4*3=12  4*4=16

5*1=5   5*2=10  5*3=15  5*4=20  5*5=25

6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36

7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49

8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64

9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81
6、while 循环打印99乘法表
[root@k8s-lb-backup awkdemo]# vim t.sh 
BEGIN {
  i=1
  while (i<=9){
    j=1
  while(j<=i){
    printf i "*" j "=" i*j
    printf "\t"
    j++
  }
    i++
    print "\n"
  }
}
[root@k8s-lb-backup awkdemo]# awk -f t.sh 
1*1=1

2*1=2   2*2=4

3*1=3   3*2=6   3*3=9

4*1=4   4*2=8   4*3=12  4*4=16

5*1=5   5*2=10  5*3=15  5*4=20  5*5=25

6*1=6   6*2=12  6*3=18  6*4=24  6*5=30  6*6=36

7*1=7   7*2=14  7*3=21  7*4=28  7*5=35  7*6=42  7*7=49

8*1=8   8*2=16  8*3=24  8*4=32  8*5=40  8*6=48  8*7=56  8*8=64

9*1=9   9*2=18  9*3=27  9*4=36  9*5=45  9*6=54  9*7=63  9*8=72  9*9=81

五、awk调用函数

1、截取字符串 使用 substr函数 substr(string,第几位开始,截取几位)
[root@k8s-lb-backup awkdemo]# awk -f t.sh > substr.txt

[root@k8s-lb-backup awkdemo]# awk '{if(substr($3,5,2)==12){print $0}}' substr.txt 
4*1=4   4*2=8   4*3=12  4*4=16

2、使用length 计算字符串长度
[root@k8s-lb-backup awkdemo]# awk '{if(substr($3,5,2)==12){print $0}{print length($3)}}' substr.txt 

[root@k8s-lb-backup awkdemo]# vim a.sh
function test(t1,t2){
  t1 > t2 ? var=t1 : var=t2
  return var
}
BEGIN{
  a=3;
  b=2;
  print test(a,b)
}

[root@k8s-lb-backup awkdemo]# awk -f a.sh 
3

  • 12
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

繁华依在

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值