shell函数及正则表达式

50 篇文章 3 订阅
15 篇文章 0 订阅

shell函数

shell脚本中的代码是按照执行的优先级的顺序从上往下抒写的,代码量越大,在脚本调试的时候就越难排错,当因执行需要调整代码执行顺序的时候就需要不断的复制粘贴,或者删除部分代码来完成,这和从写一个脚本花费的时候相比甚至需要更长的时间。

代码量大后遇到的问题:

  • 单个脚本代码量大 (300-500行)
  • 阅读修改耗时费力
  • 排错困难
  • 改变执行顺序困难

为了解决这些问题,我们可以把代码模块化,按需调用。

一、函数

1.1、函数介绍

shell中允许将一组命令集合语句形成一段可用代码,这些代码块称为shell函数。给这段代码起个名字称为函数名,后续可以直接调用该段代码的功能。

将完成一个功能的一段代码进行命名、封装

函数的优点:

  1. 代码模块化,调用方便,节省内存
  2. 代码模块化,代码量少,排错简单
  3. 代码模块化,可以改变代码的执行顺序

1.2、函数定义

语法一:

函数名 () {
    代码块
    return N
    }


语法二:
function 函数名 {
      代码块
      return N
      }
      
      
函数中return说明:
1.return可以结束一个函数,类似于前面讲的循环控制语句break(结束当前循环,执行循环体后面的代码)
2.return默认返回函数中最后一个命令的退出状态,也可以给定参数值,该参数值的范围是0-256之间。
3.如果没有return命令,函数将返回最后一个Shell的退出值。

1.3、函数调用

  • 当前命令行调用
[root@zutuanxue shell04]# cat fun1.sh 
#!/bin/bash
hello(){
echo "hello zutuanxue $1"
hostname
}
menu(){
cat <<-EOF
1. mysql
2. web
3. app
4. exit
EOF
}

[root@zutuanxue shell04]# source fun1.sh 
[root@zutuanxue shell04]# . fun1.sh 

[root@zutuanxue shell04]# hello 888
hello zutuanxue 888
MissHou.zutuanxue.cc
[root@zutuanxue shell04]# menu
1. mysql
2. web
3. app
4. exit
  • 定义到用户的环境变量中
/etc/profile	/etc/bashrc		~/.bash_profile	~/.bashrc

[root@zutuanxue shell04]# cat ~/.bashrc 
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

hello(){
echo "hello zutuanxue $1"
hostname
}
menu(){
cat <<-EOF
1. mysql
2. web
3. app
4. exit
EOF
}

注意:
当用户打开bash的时候会读取该文件
  • 脚本中调用
#!/bin/bash
#打印菜单
source ./fun1.sh
menu(){
cat <<-END
	h	显示命令帮助
	f	显示磁盘分区
	d	显示磁盘挂载
	m	查看内存使用
	u	查看系统负载
	q	退出程序
	END
}
menu		//调用函数

流程控制-case语句

一、条件循环语句-case

在生产环境中,我们总会遇到一个问题需要根据不同的状况来执行不同的预案,那么我们要处理这样的问题就要首先根据可能出现的情况写出对应预案,根据出现的情况来加载不同的预案。

1.1、case介绍

特点:根据给予的不同条件执行不同的代码块

比如你去相亲:你会在脑子里出现以下的预案:
第一眼看到对方父亲,你应该说:伯父好
第一眼看到对方母亲,你应该说:伯母好
第一眼看到对方奶奶,你应该说:奶奶好
。。。。


而这个例子中触发就是你第一眼看到了对方的谁,预案则是叫什么称呼。

再来说一个计算机的相关例子---监控内存使用率
内存使用率低于80%,脚本输出: 绿色字体的Memory use xx%
内存使用率大于80%小于90%,脚本输出: 黄色字体的Memory use xx%
内存使用大于90%,脚本输出: 红色字体的Memory use xx%

1.2、case语法

case $var in             定义变量;var代表是变量名
pattern 1)              模式1;用 | 分割多个模式,相当于or
    command1            需要执行的语句
    ;;                  两个分号代表命令结束
pattern 2)
    command2
    ;;
pattern 3)
    command3
    ;;
		  *)              default,不满足以上模式,默认执行*)下面的语句
    command4
    ;;
esac							esac表示case语句结束

二、案例

案例需求
写一个nginx启动管理脚本,可以实现/etc/init.d/nginx start|stop|restart|status|reload
或者
systemctl start nginx

#!/bin/bash
#Description: 
#Author: www.zutuanxue.com
#Created Time: 
#nginx service manage script

#variables
nginx_install_doc=/usr/local/nginx
proc=nginx
nginxd=$nginx_install_doc/sbin/nginx
pid_file=$nginx_install_doc/logs/nginx.pid

# Source function library.
if [ -f /etc/init.d/functions ];then
   . /etc/init.d/functions
else
   echo "not found file /etc/init.d/functions"
   exit
fi

#假如pid文件存在,那么统计一下nginx进程数量
if [ -f $pid_file ];then
   nginx_process_id=`cat $pid_file` 
   nginx_process_num=`ps aux |grep $nginx_process_id|grep -v "grep"|wc -l`
fi


#function
start () {
#如果nginx 没有启动直接启动,否则报错 已经启动
if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
   echo "nginx running..."
else
   #如果pid文件存在,但是没有进程,说明上一次非法关闭了nginx,造成pid文件没有自动删除,所以启动nginx之前先删除旧的pid文件
   if [ -f $pid_file ] && [ $nginx_process_num -lt 1 ];then
        rm -f $pig_file
	#可以使用两个函数,两种方法来执行命令,并返回执行结果
        #1)daemon
        #2)action   建议这个,简单易用
        
	#echo " nginx start `daemon $nginxd` "
        action "nginx start" $nginxd
  fi
  #echo " nginx start `daemon $nginxd` "
  action "nginx start" $nginxd
fi

}

stop () {
#判断nginx启动的情况下才会执行关闭,如果没启动直接报错,或者提示用户服务没启动,这里我直接报错的原因是为了给大家演示失败的输出
if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
     action "nginx stop" killall -s QUIT $proc
     rm -f $pid_file
else
     action "nginx stop" killall -s QUIT $proc 2>/dev/null
fi
}

restart () {
  stop
  sleep 1
  start
}

reload () {
#重载的目的是让主进程重新加载配置文件,但是前提是服务必须开启
#这里先判断服务是否开启,开启就执行加载,没有开启直接报加载错误
if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
    action "nginx reload" killall -s HUP $proc
else
    action "nginx reload" killall -s HUP $proc 2>/dev/null
fi
}

status () {
if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
 echo "nginx running..."
else
 echo "nginx stop"
fi 
}

#callable
case $1 in 
start) start;;
stop) stop;;
restart) restart;;
reload) reload;;
status) status;;
*) echo "USAGE: $0 start|stop|restart|reload|status";;
esac

正则表达式

处理海量日志对每一个运维来说都非常的头疼,日志分析我们首先需要把需要的数据从海量的日志中匹配出来,降低数据量,然后在分析这些日志。那么从海量的日志中把我们需要的日志找出来就需要我们写一个公式来匹配,那么如何才能写一个这样的公式呢?

这节课我就给大家说说正则表达式,看起来比较高大上,其实就是通过给定的符号生成一个字符串匹配的公式,通过该公式把需要的数据匹配出来。

比如

  • 匹配正确的IP地址
  • 匹配正确的e-mail地址

正则表达式

1、正则表达式介绍

正则表达式(Regular Expression、regex或regexp,缩写为RE),也译为正规表示法、常规表示法,是一种字符模式,用于在查找过程中匹配指定的字符。

许多程序设计语言都支持利用正则表达式进行字符串操作。例如,在Perl中就内建了一个功能强大的正则表达式引擎。

正则表达式这个概念最初是由Unix中的工具软件(例如sed和grep)普及开的。

支持正则表达式的程序如:locate |find| vim| grep| sed |awk

正则表达式是一个三方产品,被常用计算机语言广泛使用,比如:shell、PHP、python、java、js等!

[root@manage01 ~]# locate sko
locate: 无法执行 stat () `/var/lib/mlocate/mlocate.db': 没有那个文件或目录

使用updatedb生成数据文件
[root@manage01 ~]# updatedb

2、正则表达式特殊字符

定位符使用技巧:同时锚定开头和结尾,做精确匹配;单一锚定开头或结尾或者不锚定的,做模糊匹配。

定位符说明
^锚定开头 ^a 以a开头 默认锚定一个字符
$锚定结尾 a$ 以a结尾 默认锚定一个字符

测试案例

1)精确匹配  以a开头c结尾的字符串
[root@zutuanxue ~]# egrep "^ac$" file 
ac

2)模糊匹配  以a开头
[root@zutuanxue ~]# egrep "^a" file 
ac
ab
abbc
abcc
aabbcc
abbbc
abbbbbc
acc
abc
asb
aa
a_c
aZc
aAAAAc
a c
abababab
a3c


3)模糊匹配 以c结尾的字符串
[root@zutuanxue ~]# egrep "c$" file 
ac
abbc
abcc
aabbcc
abbbc
abbbbbc
acc
abc
a_c
aZc
aAAAAc
a c
ccc
a3c

匹配符:匹配字符串

匹配符说明
.匹配除回车以外的任意一个字符
( )字符串分组
[ ]定义字符类,匹配括号中的一个字符
[ ^ ]表示否定括号中出现字符类中的字符,取反。
\转义字符
|

测试案例

1)精确匹配  以a开头c结尾  中间任意  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a.c$" file 
acc
abc
a_c
aZc
a c
a3c

2)模糊匹配  以cc结尾的字符串   因为$只能锚定单个字符,如果是一个字符串就需要用()来做定义
[root@zutuanxue ~]# egrep "(cc)$" file 
abcc
aabbcc
acc
ccc

3)精确匹配  以a开头c结尾  中间是a-z,0-9  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[a-z0-9]c$" file 
acc
abc
a3c

4)精确匹配  以a开头c结尾  中间不包含a-z,0-9  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[^a-z0-9]c$" file 
a_c
aZc
a c

5)精确匹配  以e开头f结尾  中间是*号  长度为三个字节的字符串  e*f
[root@zutuanxue ~]# egrep "^e\*f$" file 
e*f

6)精确匹配 以a开头b或c结尾  中间是任意  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a.(b|c)$" file 
acc
abc
asb
a_c
aZc
a c
a3c

限定符:对前面的字符或者(字符串)出现的次数做限定说明

限定符说明
*某个字符之后加星号表示该字符不出现或出现多次 a* (ab)*
与星号相似,但略有变化,表示该字符出现一次或不出现
+与星号相似,表示其前面字符出现一次或多次,但必须出现一次
{n,m}某个字符之后出现,表示该字符最少n次,最多m次
{m}正好出现了m次

测试案例

1)精确匹配 以a开头 c结尾 中间是有b或者没有b 长度不限的字符串
[root@zutuanxue ~]# egrep "^ab*c$" file 
ac
abbc
abbbc
abbbbbc
abc

2)精确匹配 以a开头 c结尾 中间只出现一次b或者没有b的字符串
[root@zutuanxue ~]# egrep "^ab?c$" file 
ac
abc

3)精确匹配 以a开头 c结尾 中间是有b且至少出现一次 长度不限的字符串
[root@zutuanxue ~]# egrep "^ab+c$" file 
abbc
abbbc
abbbbbc
abc

4)精确匹配 以a开头 c结尾 中间是有b且至少出现两次最多出现四次 长度不限的字符串
[root@zutuanxue ~]# egrep "^ab{2,4}c$" file 
abbc
abbbc

5)精确匹配 以a开头 c结尾 中间是有b且正好出现三次的字符串
[root@zutuanxue ~]# egrep "^ab{3}c$" file 
abbbc

6) 精确匹配 以a开头 c结尾 中间是有b且至少出现一次的字符串
[root@zutuanxue ~]# egrep "^ab{1,}c$" file 
abbc
abbbc
abbbbbc
abc

3、正则表达式POSIX字符

posix字符一次只匹配一个范围中的一个字节

特殊字符说明
[:alnum:]匹配任意字母字符0-9 a-z A-Z
[:alpha:]匹配任意字母,大写或小写
[:digit:]数字 0-9
[:graph:]非空字符( 非空格控制字符)
[:lower:]小写字符a-z
[:upper:]大写字符A-Z
[:cntrl:]控制字符
[:print:]非空字符( 包括空格)
[:punct:]标点符号
[:blank:]空格和TAB字符
[:xdigit:]16 进制数字
[:space:]所有空白字符( 新行、空格、制表符)

测试案例

注意[[ ]]  双中括号的意思:  第一个中括号是匹配符[] 匹配中括号中的任意一个字符,第二个[]是格式 如[:digit:]

1)精确匹配  以a开头c结尾  中间a-zA-Z0-9任意字符  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[[:alnum:]]c$" file
acc
abc
aZc
a3c

2)精确匹配  以a开头c结尾  中间是a-zA-Z任意字符  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[[:alpha:]]c$" file
acc
abc
aZc


3)精确匹配  以a开头c结尾  中间是0-9任意字符  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[[:digit:]]c$" file
a3c

4)精确匹配  以a开头c结尾  中间是a-z任意字符  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[[:lower:]]c$" file
acc
abc


4)精确匹配  以a开头c结尾  中间是A-Z任意字符  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[[:upper:]]c$" file
aZc

5)精确匹配  以a开头c结尾  中间是非空任意字符  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[[:print:]]c$" file
acc
abc
a_c
aZc
a c
a3c

6)精确匹配  以a开头c结尾  中间是符号字符  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[[:punct:]]c$" file
a_c

7)精确匹配  以a开头c结尾  中间是空格或者TAB符字符  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[[:blank:]]c$" file
a c

类似
[root@zutuanxue ~]# egrep "^a[[:space:]]c$" file
a c

8)精确匹配  以a开头c结尾  中间是十六进制字符  长度为三个字节的字符串
[root@zutuanxue ~]# egrep "^a[[:xdigit:]]c$" file
acc
abc
a3c

说明:特殊字符和POSIX字符是两套字符,都可以完成需要的匹配,大家学习的时候最少要记住一套字符并熟练应用。

shell对文件的操作

在shell脚本编写中,时常会用到对文件的相关操作,比如增加内容,修改内容,删除部分内容,查看部分内容等,但是上述举例的这些操作一般都是需要在文本编辑器中才能操作,常用的文本编辑器如:gedit、vim、nano等又是交互式文本编辑器,脚本无法自己独立完成,必须有人参与才可以完成。如果这样的话又违背了我们编写脚本的意愿(全部由机器来完成,减少人的工作压力,提升工作效率)。emm…如何才能让这些操作全部脚本自己就搞定,而不需要人的参与,而且又能按照我们的脚本预案来完成呢?

为了解决上述问题,linux为大家提供了一些命令,比如Perl、sed等命令,今天我就着重为大家介绍一下sed命令。

一、sed介绍

sed是linux中提供的一个外部命令,它是一个行(流)编辑器,非交互式的对文件内容进行增删改查的操作,使用者只能在命令行输入编辑命令、指定文件名,然后在屏幕上查看输出。它和文本编辑器有本质的区别。

区别是:

文本编辑器: 编辑对象是文件

行编辑器:编辑对象是文件中的行

也就是前者一次处理一个文本,而后者是一次处理一个文本中的一行。这个是我们应该弄清楚且必须牢记的,否者可能无法理解sed的运行原理和使用精髓。

sed数据处理原理

sed处理数据原理.png

二、sed语法

sed 命令语法:

sed [options] ‘{command}[flags]’ [filename]

#命令选项
-e script 将脚本中指定的命令添加到处理输入时执行的命令中  多条件,一行中要有多个操作
-f script 将文件中指定的命令添加到处理输入时执行的命令中
-n        抑制自动输出
-i        编辑文件内容
-i.bak    修改时同时创建.bak备份文件。
-r        使用扩展的正则表达式
!         取反 (跟在模式条件后与shell有所区别)

#command   对文件干什么
sed常用内部命令
a   在匹配后面添加
i   在匹配前面添加

d   删除

s   查找替换  字符串
c   更改
y   转换   N D P 


p   打印



#flags
数字             表示新文本替换的模式
g:             表示用新文本替换现有文本的全部实例
p:             表示打印原始的内容
w filename:     将替换的结果写入文件

2.1)sed内部命令说明

演示实例文档

[root@zutuanxue ~]# cat data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

文件内容增加操作,将数据追加到某个位置之后,使用命令a。

演示案例

在data1的每行后追加一行新数据内容: append data "haha"
[root@zutuanxue ~]# sed 'a\append data "haha"' data1
1 the quick brown fox jumps over the lazy dog.
append data "haha"
2 the quick brown fox jumps over the lazy dog.
append data "haha"
3 the quick brown fox jumps over the lazy dog.
append data "haha"
4 the quick brown fox jumps over the lazy dog.
append data "haha"
5 the quick brown fox jumps over the lazy dog.
append data "haha"


在第二行后新开一行追加数据: append data "haha"
[root@zutuanxue ~]# sed '2a\append data "haha"' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
append data "haha"
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

在第二到四行每行后新开一行追加数据: append data "haha"
[root@zutuanxue ~]# sed '2,4a\append data "haha"' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
append data "haha"
3 the quick brown fox jumps over the lazy dog.
append data "haha"
4 the quick brown fox jumps over the lazy dog.
append data "haha"
5 the quick brown fox jumps over the lazy dog.

匹配字符串追加: 找到包含"3 the"的行,在其后新开一行追加内容: append data "haha"
[root@zutuanxue ~]# sed '/3 the/a\append data "haha"' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
append data "haha"
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

//开启匹配模式  /要匹配的字符串/

文件内容增加操作,将数据插入到某个位置之前,使用命令i。

演示案例

在data1的每行前插入一行新数据内容: insert data "haha"
[root@zutuanxue ~]# sed 'i\insert data "haha"' data1
insert data "haha"
1 the quick brown fox jumps over the lazy dog.
insert data "haha"
2 the quick brown fox jumps over the lazy dog.
insert data "haha"
3 the quick brown fox jumps over the lazy dog.
insert data "haha"
4 the quick brown fox jumps over the lazy dog.
insert data "haha"
5 the quick brown fox jumps over the lazy dog.


在第二行前新开一行插入数据: insert data "haha"
[root@zutuanxue ~]# sed '2i\insert data "haha"' data1
1 the quick brown fox jumps over the lazy dog.
insert data "haha"
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.


在第二到四行每行前新开一行插入数据: insert data "haha"
[root@zutuanxue ~]# sed '2,4i\insert data "haha"' data1
1 the quick brown fox jumps over the lazy dog.
insert data "haha"
2 the quick brown fox jumps over the lazy dog.
insert data "haha"
3 the quick brown fox jumps over the lazy dog.
insert data "haha"
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.


匹配字符串插入: 找到包含"3 the"的行,在其前新开一行插入内容: insert data "haha"
[root@zutuanxue ~]# sed '/3 the/i\insert data "haha"' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
insert data "haha"
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

文件内容修改操作–替换,将一行中匹配的内容替换为新的数据,使用命令s。

演示案例

从标准输出流中做替换,将test替换为text
[root@zutuanxue ~]# echo "this is a test" |sed 's/test/text/'
this is a text

将data1中每行的dog替换为cat
[root@zutuanxue ~]# sed 's/dog/cat/' data1
1 the quick brown fox jumps over the lazy cat.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy cat.
5 the quick brown fox jumps over the lazy cat.

将data1中第二行的dog替换为cat
[root@zutuanxue ~]# sed '2s/dog/cat/' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

将data1中第二到第四行的dog替换为cat
[root@zutuanxue ~]# sed '2,4s/dog/cat/' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy cat.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy cat.
5 the quick brown fox jumps over the lazy dog.

匹配字符串替换:将包含字符串"3 the"的行中的dog替换为cat
[root@zutuanxue ~]# sed '/3 the/s/dog/cat/' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy cat.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

文件内容修改操作–更改,将一行中匹配的内容替换为新的数据,使用命令c。

演示案例

将data1文件中的所有行的内容更改为: change data "data"
[root@zutuanxue ~]# sed 'c\change data "haha"' data1
change data "haha"
change data "haha"
change data "haha"
change data "haha"
change data "haha"

将data1文件第二行的内容更改为: change data "haha"
[root@zutuanxue ~]# sed '2c\change data "haha"' data1
1 the quick brown fox jumps over the lazy dog.
change data "haha"
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

将data1文件中的第二、三、四行的内容更改为:change data "haha"
[root@zutuanxue ~]# sed '2,4c\change data "haha"' data1
1 the quick brown fox jumps over the lazy dog.
change data "haha"
5 the quick brown fox jumps over the lazy dog.

将data1文件中包含"3 the"的行内容更改为: change data "haha"
[root@zutuanxue ~]# sed '/3 the/c\change data "data"' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
change data "data"
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

文件内容修改操作–字符转换,将一行中匹配的内容替换为新的数据,使用命令y。

演示案例

将data1中的a b c字符转换为对应的 A  B  C字符
[root@zutuanxue ~]# sed 'y/abc/ABC/' data1
1 the quiCk Brown fox jumps over the lAzy dog.
2 the quiCk Brown fox jumps over the lAzy dog.
3 the quiCk Brown fox jumps over the lAzy dog.
4 the quiCk Brown fox jumps over the lAzy dog.
5 the quiCk Brown fox jumps over the lAzy dog.

文件内容删除,将文件中的指定数据删除,使用命令d。

演示案例

删除文件data1中的所有数据
[root@zutuanxue ~]# sed 'd' data1

删除文件data1中的第三行数据
[root@zutuanxue ~]# sed '3d' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

删除文件data1第三到第四行的数据
[root@zutuanxue ~]# sed '3,4d' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

删除文件data1中包含字符串"3 the"的行
[root@zutuanxue ~]# sed '/3 the/d' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

文件内容查看,将文件内容输出到屏幕,使用命令p。

演示案例

打印data1文件内容
[root@zutuanxue ~]# sed 'p' data1
1 the quick brown fox jumps over the lazy dog.
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

打印data1文件第三行的内容
[root@zutuanxue ~]# sed '3p' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

打印data1文件第二、三、四行内容
[root@zutuanxue ~]# sed '2,4p' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

打印data1文件包含字符串"3 the"的行
[root@zutuanxue ~]# sed '/3 the/p' data1
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog. 


可以看得出,打印内容是重复的行,原因是打印了指定文件内容一次,又将读入缓存的所有数据打印了一次,所以会看到这样的效果,
如果不想看到这样的结果,可以加命令选项-n抑制内存输出即可。

2.2)命令选项说明

sed语法
在sed命令中,命令选项是对sed中的命令的增强

在命令行中使用多个命令 -e

将brown替换为green dog替换为cat
[root@zutuanxue ~]# sed -e 's/brown/green/;s/dog/cat/' data1
1 the quick green fox jumps over the lazy cat.
2 the quick green fox jumps over the lazy cat.
3 the quick green fox jumps over the lazy cat.
4 the quick green fox jumps over the lazy cat.
5 the quick green fox jumps over the lazy cat.

从文件读取编辑器命令 -f 适用于日常重复执行的场景

1)将命令写入文件
[root@zutuanxue ~]# vim abc
s/brown/green/
s/dog/cat/
s/fox/elephant/

2)使用-f命令选项调用命令文件
[root@zutuanxue ~]# sed -f abc data1 
1 the quick green elephant jumps over the lazy cat.
2 the quick green elephant jumps over the lazy cat.
3 the quick green elephant jumps over the lazy cat.
4 the quick green elephant jumps over the lazy cat.
5 the quick green elephant jumps over the lazy cat.

抑制内存输出 -n

打印data1文件的第二行到最后一行内容  $最后的意思
[root@zutuanxue ~]# sed -n '2,$p' data1 
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

使用正则表达式 -r

打印data1中以字符串"3 the"开头的行内容
[root@zutuanxue ~]# sed -n  -r '/^(3 the)/p' data1
3 the quick brown fox jumps over the lazy dog.

从上述的演示中,大家可以看出,数据处理只是在缓存中完成的,并没有实际修改文件内容,如果需要修改文件内容可以直接使用-i命令选项。在这里我需要说明的是-i是一个不可逆的操作,一旦修改,如果想复原就很困难,几乎不可能,所以建议大家在操作的时候可以备份一下源文件。-i命令选项提供了备份功能,比如参数使用-i.bak,那么在修改源文件的同时会先备份一个以.bak结尾的源文件,然后再进行修改操作。

1)查看文件列表,没有发现data1.bak
[root@zutuanxue ~]# ls
abc  apache  data1  Dobby  file  node-v10.14.1  Python-3.7.1  soft1  vimset

2)执行替换命令并修改文件
[root@zutuanxue ~]# sed -i.bak 's/brown/green/' data1

3)发现文件夹中多了一个data1.bak文件
[root@zutuanxue ~]# ls
abc     data1      Dobby  node-v10.14.1  soft1
apache  data1.bak  file   Python-3.7.1   vimset

4)打印比较一下,发现data1已经被修改,data1.bak是源文件的备份。
[root@zutuanxue ~]# cat data1
1 the quick green fox jumps over the lazy dog.
2 the quick green fox jumps over the lazy dog.
3 the quick green fox jumps over the lazy dog.
4 the quick green fox jumps over the lazy dog.
5 the quick green fox jumps over the lazy dog.
[root@zutuanxue ~]# cat data1.bak 
1 the quick brown fox jumps over the lazy dog.
2 the quick brown fox jumps over the lazy dog.
3 the quick brown fox jumps over the lazy dog.
4 the quick brown fox jumps over the lazy dog.
5 the quick brown fox jumps over the lazy dog.

2.3)标志

在sed命令中,标志是对sed中的内部命令做补充说明

演示文档
[root@zutuanxue ~]# cat data2
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy dog . dog
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog

数字标志:此标志是一个非零正数,默认情况下,执行替换的时候,如果一行中有多个符合的字符串,如果没有标志位定义,那么只会替换第一个字符串,其他的就被忽略掉了,为了能精确替换,可以使用数字位做定义。

替换一行中的第二处dog为cat
[root@zutuanxue ~]# sed 's/dog/cat/2' data2
1 the quick brown fox jumps over the lazy dog . cat
2 the quick brown fox jumps over the lazy dog . cat
3 the quick brown fox jumps over the lazy dog . cat
4 the quick brown fox jumps over the lazy dog . cat
5 the quick brown fox jumps over the lazy dog . cat

g标志:将一行中的所有符合的字符串全部执行替换

将data1文件中的所有dog替换为cat
[root@zutuanxue ~]# sed 's/dog/cat/g' data2
1 the quick brown fox jumps over the lazy cat . cat
2 the quick brown fox jumps over the lazy cat . cat
3 the quick brown fox jumps over the lazy cat . cat
4 the quick brown fox jumps over the lazy cat . cat
5 the quick brown fox jumps over the lazy cat . cat

p标志:打印文本内容,类似于-p命令选项

[root@zutuanxue ~]# sed  '3s/dog/cat/p' data2
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog

w filename标志:将修改的内容存入filename文件中

[root@zutuanxue ~]# sed  '3s/dog/cat/w text' data2
1 the quick brown fox jumps over the lazy dog . dog
2 the quick brown fox jumps over the lazy dog . dog
3 the quick brown fox jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy dog . dog
5 the quick brown fox jumps over the lazy dog . dog

可以看出,将修改的第三行内容存在了text文件中
[root@zutuanxue ~]# cat text 
3 the quick brown fox jumps over the lazy cat . dog

三、练习案例

3.1、写一个初始化系统的脚本
案例需求
1)自动修改主机名(如:ip是192.168.0.88,则主机名改为server88.zutuanxue.cc)

a. 更改文件非交互式 sed

/etc/sysconfig/network

b.将本主机的IP截取出来赋值给一个变量ip;再然后将ip变量里以.分割的最后一位赋值给另一个变量ip1

2)自动配置可用的yum源

3)自动关闭防火墙和selinux

1、关闭防火墙
2、关闭selinux
3、配置yum源
4、ntp时间服务器 
5、更新系统软件包

3.2、写一个搭建ftp服务的脚本,要求如下:
案例需求
1)不支持本地用户登录 local_enable=NO
2) 匿名用户可以上传 新建 删除 anon_upload_enable=YES anon_mkdir_write_enable=YES
3) 匿名用户限速500KBps anon_max_rate=500000

仅供参考:
#!/bin/bash
ipaddr=`ifconfig eth0|sed -n '2p'|sed -e 's/.*inet addr:\(.*\) Bcast.*/\1/g'`
iptail=`echo $ipaddr|cut -d'.' -f4`
ipremote=192.168.1.10
#修改主机名
hostname server$iptail.zutuanxue.com
sed -i "/HOSTNAME/cHOSTNAME=server$iptail.zutuanxue.com" /etc/sysconfig/network
echo "$ipaddr server$iptail.zutuanxue.cc" >>/etc/hosts
#关闭防火墙和selinux
service iptables stop
setenforce 0 >/dev/null 2>&1
sed -i '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
#配置yum源(一般是内网源)
#test network
ping -c 1 $ipremote > /dev/null 2>&1
if [ $? -ne 0 ];then
	echo "你的网络不通,请先检查你的网络"
	exit 1
else
	echo "网络ok."
fi
cat > /etc/yum.repos.d/server.repo << end
[server]
name=server
baseurl=ftp://$ipremote
enabled=1
gpgcheck=0
end

#安装软件
read -p "请输入需要安装的软件,多个用空格隔开:" soft
yum -y install $soft &>/dev/null

#备份配置文件
conf=/etc/vsftpd/vsftpd.conf
\cp $conf $conf.default
#根据需求修改配置文件
sed -ir '/^#|^$/d' $conf
sed -i '/local_enable/c\local_enable=NO' $conf
sed -i '$a anon_upload_enable=YES' $conf
sed -i '$a anon_mkdir_write_enable=YES' $conf
sed -i '$a anon_other_write_enable=YES' $conf
sed -i '$a anon_max_rate=512000' $conf
#启动服务
service vsftpd restart &>/dev/null && echo"vsftpd服务启动成功"

#测试验证
chmod 777 /var/ftp/pub
cp /etc/hosts /var/ftp/pub
#测试下载
cd /tmp
lftp $ipaddr <<end
cd pub
get hosts
exit
end

if [ -f /tmp/hosts ];then
	echo "匿名用户下载成功"
	rm -f /tmp/hosts
else
	echo "匿名用户下载失败"
fi
#测试上传、创建目录、删除目录等
cd /tmp
lftp $ipaddr << end
cd pub
mkdir test1
mkdir test2
put /etc/group
rmdir test2
exit
end

if [ -d /var/ftp/pub/test1 ];then
    echo "创建目录成功"
	if [ ! -d /var/ftp/pub/test2 ];then
    	echo "文件删除成功"
        fi
else
	if [ -f /var/ftp/pub/group ];then
	echo "文件上传成功"
        else
        echo "上传、创建目录删除目录部ok"
        fi 
fi   
[ -f /var/ftp/pub/group ] && echo "上传文件成功"

shell数据筛选与处理

阅读 (15820)

分享

聊聊大家常说的数据分析:

  • 数据收集:负责数据的收集
  • 数据清洗:负责数据的筛选
  • 数据分析:数据运算、整理
  • 数据展示:图表或表格方式输出结果

shell脚本数据的处理

1)数据检索:grep tr cut

2)数据处理:uniq sort tee paste xargs

之前的脚本中我们都是通过grep、cut、tr、uniq、sort等命令通过管道组合在一起将字符串检索出来,然后在通过shell中对应的运算得到结果,在数据检索过程中大家可能也体会到了其中的辛苦和蹩脚。没办法,会的就这么多,还需要完成任务。

缺点:复杂的命令组合

多次运算

上手难

解决办法

好了,学完这节课大家的所有之前的痛苦就都能解决了,今天要给大家介绍一个更加厉害的命令awk。他可以让大家从输出流中检索出自己需要的数据而不需要再向以前那样通过大量命令组合来完成,只需一个命令awk就能完成。并且还能够通过awk对数据进行处理,而不再需要额外的shell运算。

awk的应用场景

字符串截取

数据运算

比如内存使用率脚本

shell对输出流的处理-awk

1、awk介绍

在日常计算机管理中,总会有很多数据输出到屏幕或者文件,这些输出包含了标准输出、标准错误输出。默认情况下,这些信息全部输出到默认输出设备—屏幕。然而,大量的数据输出中,只有一小部分是我们需要重点关注的,我们需要把我们需要的或者关注的这些信息过滤或者提取以备后续需要时调用。早先的学习中,我们学过使用grep来过滤这些数据,使用cut、tr命令提出某些字段,但是他们都不具备提取并处理数据的能力,都必须先过滤,再提取转存到变量,然后在通过变量提取去处理,比如:

内存使用率的统计步骤

1) 通过free -m提取出内存总量,赋值给变量 memory_totle

2)通过free -m提取出n内存使用量,赋值给变量memory_use

3)通过数学运算计算内存使用率

需要执行多步才能得到内存使用率,那么有没有一个命令能够集过滤、提取、运算为一体呢?当然,就是今天我要给大家介绍的命令:awk

平行命令还有gawk、pgawk、dgawk

awk是一种可以处理数据、产生格式化报表的语言,功能十分强大。awk 认为文件中的每一行是一条记录 记录与记录的分隔符为换行符,每一列是一个字段 字段与字段的分隔符默认是一个或多个空格或tab制表符.

awk的工作方式是读取数据,将每一行数据视为一条记录(record)每条记录以字段分隔符分成若干字段,然后输出各个字段的值.

2、awk语法

awk [options] ‘[BEGIN]{program}[END]’ [FILENAME]

常用命令选项
-F fs 指定描绘一行中数据字段的文件分隔符  默认为空格
-f file 指定读取程序的文件名
-v var=value 定义awk程序中使用的变量和默认值

注意:awk 程序由左大括号和右大括号定义。 程序命令必须放置在两个大括号之间。由于awk命令行假定程序是单文本字符串,所以必须将程序包括在单引号内。
1)程序必须放在大括号内
2)程序必须要用单引号引起来

awk程序运行优先级是:
    1)BEGIN: 在开始处理数据流之前执行,可选项
    2)program: 如何处理数据流,必选项
    3)END: 处理完数据流后执行,可选项

3、awk基本应用

能够熟练使用awk对标准输出的行、列、字符串截取

学习用例
[root@zutuanxue ~]# cat test 
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox         jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog

3.1)awk对字段(列)的提取

字段提取:提取一个文本中的一列数据并打印输出

字段相关内置变量

$0 表示整行文本

$1 表示文本行中的第一个数据字段

$2 表示文本行中的第二个数据字段

$N 表示文本行中的第N个数据字段

$NF 表示文本行中的最后一个数据字段

读入test每行数据并把每行数据打印出来
[root@zutuanxue ~]# awk '{print $0}' test 
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox         jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog

打印test第六个字段
[root@zutuanxue ~]# awk '{print $6}' test
jumps
jumps
jumps
jumps
jumps


打印test最后一个字段
[root@zutuanxue ~]# awk '{print $NF}' test
dog
dog
dog
dog
dog
3.2)命令选项详解

-F: 指定字段与字段的分隔符

当输出的数据流字段格式不是awk默认的字段格式时,我们可以使用-F命令选项来重新定义数据流字段分隔符。比如:

处理的文件是/etc/passwd,希望打印第一列、第三列、最后一列

[root@zutuanxue ~]# awk -F ':' '{print $1,$3,$NF}' /etc/passwd
root 0 /bin/bash
bin 1 /sbin/nologin
daemon 2 /sbin/nologin
adm 3 /sbin/nologin
lp 4 /sbin/nologin
sync 5 /bin/sync
shutdown 6 /sbin/shutdown
halt 7 /sbin/halt
mail 8 /sbin/nologin
operator 11 /sbin/nologin
games 12 /sbin/nologin
ftp 14 /sbin/nologin
nobody 99 /sbin/nologin
systemd-network 192 /sbin/nologin
dbus 81 /sbin/nologin
polkitd 999 /sbin/nologin
postfix 89 /sbin/nologin
chrony 998 /sbin/nologin
sshd 74 /sbin/nologin
ntp 38 /sbin/nologin
tcpdump 72 /sbin/nologin
nscd 28 /sbin/nologin
mysql 997 /sbin/nologin
www 996 /sbin/nologin
apache 48 /sbin/nologin
tss 59 /sbin/nologin
zabbix 995 /sbin/nologin
saslauth 994 /sbin/nologin
grafana 993 /sbin/nologin

可以看的出,awk输出字段默认的分隔符也是空格

-f file: 如果awk命令是日常重复工作,而又没有太多变化,可以将程序写入文件,每次使用-f调用程序文件就好,方便,高效。

[root@zutuanxue ~]# cat abc 
{print $1,$3,$NF}

[root@zutuanxue ~]# awk -f abc test
1 quick dog
2 quick dog
3 quick dog
4 quick dog
5 quick dog

-v 定义变量,既然作者写awk的时候就是按着语言去写的,那么语言中最重要的要素—变量肯定不能缺席,所以可以使用-v命令选项定义变量

[root@zutuanxue ~]# awk -v name='baism' 'BEGIN{print name}'
baism

定义了一个变量 name=baism,然后调用变量读出数据。
3.3)awk对记录(行)的提取

记录提取:提取一个文本中的一行并打印输出

记录的提取方法有两种:a、通过行号 b、通过正则匹配

记录相关内置变量

NR: 指定行号 number row

提取test第三行数据

指定行号为3
[root@zutuanxue ~]# awk 'NR==3{print $0}' test 
3 the quick brown fox         jumps over the lazy cat . dog

指定行的第一个字段精确匹配字符串为3
[root@zutuanxue ~]# awk '$1=="3"{print $0}' test 
3 the quick brown fox         jumps over the lazy cat . dog
3.4)awk对字符串提取

记录和字段的汇合点就是字符串

打印test第三行的第六个字段
[root@zutuanxue ~]# awk 'NR==3{print $6}' test
jumps

4、awk程序的优先级

awk代码块的优先级

关于awk程序的执行优先级,BEGIN是优先级最高的代码块,是在执行PROGRAM之前执行的,不需要提供数据源,因为不涉及到任何数据的处理,也不依赖与PROGRAM代码块;PROGRAM是对数据流干什么,是必选代码块,也是默认代码块。所以在执行时必须提供数据源;END是处理完数据流后的操作,如果需要执行END代码块,就必须需要PROGRAM的支持,单个无法执行。

BEGIN:处理数据源之前干什么 不需要数据源就可以执行

PROGRAM: 对数据源干什么 【默认必须有】 需要数据源

END:处理完数据源后干什么 需要program 需要数据源

优先级展示
[root@zutuanxue ~]# awk 'BEGIN{print "hello zutuanxue"}{print $0}END{print "bye zutuanxue"}' test
hello zutuanxue
1 the quick brown fox jumps over the lazy cat . dog
2 the quick brown fox jumps over the lazy cat . dog
3 the quick brown fox         jumps over the lazy cat . dog
4 the quick brown fox jumps over the lazy cat . dog
5 the quick brown fox jumps over the lazy cat . dog
bye zutuanxue

不需要数据源,可以直接执行
[root@zutuanxue ~]# awk 'BEGIN{print "hello world"}'
hello world

没有提供数据流,所以无法执行成功
[root@zutuanxue ~]# awk '{print "hello world"}'
[root@zutuanxue ~]# awk 'END{print "hello world"}'

5、awk高级应用

awk是一门语言,那么就会符合语言的特性,除了可以定义变量外,还可以定义数组,还可以进行运算,流程控制,我们接下来看看吧。

5.1)awk定义变量和数组

定义变量

[root@zutuanxue ~]# awk -v name='baism' 'BEGIN{print name}'
baism

[root@zutuanxue ~]# awk 'BEGIN{name="baism";print name}'
baism

数组定义方式: 数组名[索引]=值

定义数组array,有两个元素,分别是100,200,打印数组元素。
[root@zutuanxue ~]# awk 'BEGIN{array[0]=100;array[1]=200;print array[0],array[1]}'
100 200

[root@zutuanxue ~]# awk 'BEGIN{a[0]=100;a[1]=200;print a[0]}'
100
[root@zutuanxue ~]# awk 'BEGIN{a[0]=100;a[1]=200;print a[1]}'
200
5.2)awk运算
  1. 赋值运算 =
  2. 比较运算 > >= == < <= !=
  3. 数学运算 + - * / % ** ++ –
  4. 逻辑运算 && || !
  5. 匹配运算 ~ !~ 精确匹配 == !=

a.赋值运算:主要是对变量或者数组赋值,如:

变量赋值 name=‘baism’ school=‘zutuanxue’

数组赋值 array[0]=100

[root@zutuanxue ~]# awk -v name='baism' 'BEGIN{print name}'
baism

[root@zutuanxue ~]# awk 'BEGIN{school="zutuanxue";print school}'
zutuanxue

[root@zutuanxue ~]# awk 'BEGIN{array[0]=100;print array[0]}'
100

b.比较运算,如果比较的是字符串则按ascii编码顺序表比较。如果结果返回为真则用1表示,如果返回为假则用0表示

ascii

[root@zutuanxue ~]# awk 'BEGIN{print "a" >= "b" }'
0
[root@zutuanxue ~]# awk 'BEGIN{print "a" <= "b" }'
1

[root@zutuanxue ~]# awk '$1>4{print $0}' test
5 the quick brown fox jumps over the lazy cat . dog
[root@zutuanxue ~]# awk 'BEGIN{print 100 >= 1 }'
1
[root@zutuanxue ~]# awk 'BEGIN{print 100 == 1 }'
0
[root@zutuanxue ~]# awk 'BEGIN{print 100 <= 1 }'
0
[root@zutuanxue ~]# awk 'BEGIN{print 100 < 1 }'
0
[root@zutuanxue ~]# awk 'BEGIN{print 100 != 1 }'
1

c.数学运算

[root@zutuanxue ~]# awk 'BEGIN{print 100+3 }'
103
[root@zutuanxue ~]# awk 'BEGIN{print 100-3 }'
97
[root@zutuanxue ~]# awk 'BEGIN{print 100*3 }'
300
[root@zutuanxue ~]# awk 'BEGIN{print 100/3 }'
33.3333
[root@zutuanxue ~]# awk 'BEGIN{print 100**3 }'
1000000
[root@zutuanxue ~]# awk 'BEGIN{print 100%3 }'
1
[root@zutuanxue ~]# awk -v 'count=0' 'BEGIN{count++;print count}'
1
[root@zutuanxue ~]# awk -v 'count=0' 'BEGIN{count--;print count}'
-1

d.逻辑运算

与运算:真真为真,真假为假,假假为假
[root@zutuanxue ~]# awk 'BEGIN{print 100>=2 && 100>=3 }'
1
[root@zutuanxue ~]# awk 'BEGIN{print 100>=2 && 1>=100 }'
0

或运算:真真为真,真假为真,假假为假
[root@zutuanxue ~]# awk 'BEGIN{print 100>=2 || 1>=100 }'
1
[root@zutuanxue ~]# awk 'BEGIN{print 100>=200 || 1>=100 }'
0

非运算
[root@manage01 resource]# awk 'BEGIN{print ! (100>=2)}'
0

e.匹配运算

[root@zutuanxue ~]# awk -F ':' '$1 ~ "^ro" {print $0}' /etc/passwd
root:x:0:0:root:/root:/bin/bash

[root@zutuanxue ~]# awk -F ':' '$1 !~ "^ro" {print $0}' /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:997:User for polkitd:/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
mysql:x:997:995::/home/mysql:/sbin/nologin
www:x:996:994::/home/www:/sbin/nologin
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
zabbix:x:995:993:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin
saslauth:x:994:76:Saslauthd user:/run/saslauthd:/sbin/nologin
grafana:x:993:992:grafana user:/usr/share/grafana:/sbin/nologin
5.3)awk 环境变量
变量描述
FIELDWIDTHS以空格分隔的数字列表,用空格定义每个数据字段的精确宽度
FS输入字段分隔符号 数据源的字段分隔符 -F
OFS输出字段分隔符号
RS输入记录分隔符
ORS输出记录分隔符号
FIELDWIDTHS:重定义列宽并打印,注意不可以使用$0打印所有,因为$0是打印本行全内容,不会打印你定义的字段
[root@zutuanxue ~]# awk 'BEGIN{FIELDWIDTHS="5 2 8"}NR==1{print $1,$2,$3}' /etc/passwd
root: x: 0:0:root

FS:指定数据源中字段分隔符,类似命令选项-F
[root@zutuanxue ~]# awk 'BEGIN{FS=":"}NR==1{print $1,$3,$NF}' /etc/passwd
root 0 /bin/bash

OFS:指定输出到屏幕后字段的分隔符
[root@zutuanxue ~]# awk 'BEGIN{FS=":";OFS="-"}NR==1{print $1,$3,$NF}' /etc/passwd
root-0-/bin/bash

RS:指定记录的分隔符
[root@zutuanxue ~]# awk 'BEGIN{RS=""}{print $1,$13,$25,$37,$49}' test
1 2 3 4 5

将记录的分隔符修改为空行后,所有的行会变成一行,所以所有字段就在一行了。

ORS:输出到屏幕后记录的分隔符,默认为回车
[root@zutuanxue ~]# awk 'BEGIN{RS="";ORS="*"}{print $1,$13,$25,$37,$49}' test
1 2 3 4 5*[root@zutuanxue ~]# 

可以看出,提示符和输出在一行了,因为默认回车换成了*
5.4)流程控制

if判断语句

学习用例
[root@zutuanxue ~]# cat num
1
2
3
4
5
6
7
8
9

单if语句
打印$1大于5的行
[root@zutuanxue ~]# awk '{if($1>5)print $0}' num
6
7
8
9

if...else语句
假如$1大于5则除以2输出,否则乘以2输出
[root@zutuanxue ~]# awk '{if($1>5)print $1/2;else print $1*2}' num
2
4
6
8
10
3
3.5
4
4.5

for循环语句

学习用例
[root@zutuanxue ~]# cat num2
60 50 100
150 30 10
70 100 40

将一行中的数据都加起来  $1+$2+$3
[root@zutuanxue ~]# awk '{sum=0;for (i=1;i<4;i++){sum+=$i}print sum}' num2
210
190
210

如果看的不明白可以看下面格式
[root@zutuanxue ~]# awk '{
> sum=0
> for (i=1;i<4;i++) {
>     sum+=$i
> }
> print sum
> }' num2
210
190
210

while循环语句–先判断后执行

学习用例
[root@zutuanxue ~]# cat num2
60 50 100
150 30 10
70 100 40

将文件中的每行的数值累加,和大于或等于150就停止累加
[root@zutuanxue ~]# awk '{sum=0;i=1;while(sum<150){sum+=$i;i++}print sum}' num2
210
150
170



如果看的不明白可以看下面格式
[root@zutuanxue ~]# awk '{                                                 
sum=0
i=1
while (sum<150) {
   sum+=$i
   i++
}
print sum
}' num2
210
150
170

do…while循环语句–先执行后判断

学习用例
[root@zutuanxue ~]# cat num2
60 50 100
150 30 10
70 100 40

将文件中的每行的数值累加,和大于或等于150就停止累加
[root@zutuanxue ~]# awk '{sum=0;i=1;do{sum+=$i;i++}while(sum<150);print sum}' num2
210
150
170
如果看的不明白可以看下面格式
[root@zutuanxue ~]# awk '{
> sum=0
> i=1
> do {
> sum+=$i
> i++
> }while (sum<150)
> print sum
> }' num2
210
150
170

循环控制语句

break 跳出循环,继续执行后续语句

学习用例
[root@zutuanxue ~]# cat num2
60 50 100
150 30 10
70 100 40

累加每行数值,和大于150停止累加
[root@zutuanxue ~]# awk '{
> sum=0
> i=1
> while (i<4){
>    sum+=$i
>    if (sum>150){
>       break
>    }
>    i++
> }
> print sum
> }' num2
210
180
170

在一行了,因为默认回车换成了*


#### 5.4)流程控制

**if判断语句**

学习用例
[root@zutuanxue ~]# cat num
1
2
3
4
5
6
7
8
9

单if语句
打印$1大于5的行
[root@zutuanxue ~]# awk ‘{if($1>5)print $0}’ num
6
7
8
9

if…else语句
假如$1大于5则除以2输出,否则乘以2输出
[root@zutuanxue ~]# awk ‘{if($1>5)print $1/2;else print $1*2}’ num
2
4
6
8
10
3
3.5
4
4.5


**for循环语句**

学习用例
[root@zutuanxue ~]# cat num2
60 50 100
150 30 10
70 100 40

将一行中的数据都加起来 $1+$2+KaTeX parse error: Expected 'EOF', got '#' at position 21: …ot@zutuanxue ~]#̲ awk '{sum=0;fo…i}print sum}’ num2
210
190
210

如果看的不明白可以看下面格式
[root@zutuanxue ~]# awk '{

sum=0
for (i=1;i<4;i++) {
sum+=$i
}
print sum
}’ num2
210
190
210


**while循环语句**–先判断后执行

学习用例
[root@zutuanxue ~]# cat num2
60 50 100
150 30 10
70 100 40

将文件中的每行的数值累加,和大于或等于150就停止累加
[root@zutuanxue ~]# awk ‘{sum=0;i=1;while(sum<150){sum+=$i;i++}print sum}’ num2
210
150
170

如果看的不明白可以看下面格式
[root@zutuanxue ~]# awk ‘{
sum=0
i=1
while (sum<150) {
sum+=$i
i++
}
print sum
}’ num2
210
150
170


**do…while循环语句**–先执行后判断

学习用例
[root@zutuanxue ~]# cat num2
60 50 100
150 30 10
70 100 40

将文件中的每行的数值累加,和大于或等于150就停止累加
[root@zutuanxue ~]# awk ‘{sum=0;i=1;do{sum+=$i;i++}while(sum<150);print sum}’ num2
210
150
170
如果看的不明白可以看下面格式
[root@zutuanxue ~]# awk '{

sum=0
i=1
do {
sum+=$i
i++
}while (sum<150)
print sum
}’ num2
210
150
170


**循环控制语句**

break 跳出循环,继续执行后续语句

学习用例
[root@zutuanxue ~]# cat num2
60 50 100
150 30 10
70 100 40

累加每行数值,和大于150停止累加
[root@zutuanxue ~]# awk '{

sum=0
i=1
while (i<4){
sum+=$i
if (sum>150){
break
}
i++
}
print sum
}’ num2
210
180
170

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

MagnumOvO

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

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

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

打赏作者

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

抵扣说明:

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

余额充值