34、shell数组+正则表达式

0、课前补充

在这里插入图片描述

jiafa () {
result=$(echo " $1 + $2 " | bc )
print "%.2f\n" "$result"
}                   ##保留小数点两位 

薄弱加强点

a=$(df -h | awk 'NR>1 {print $5}' | tr -d '%')
echo "$a"

在这里插入图片描述

一、数组

1.1、定义

数组的定义:在集合种指定多个元素。

1.2、元素类型

元素的类型:整数,字符串,可以是浮点。

1.3、数组的作用

数组的作用:一次性的定义多个元素,可以为变量赋值提供便利。

1.4、数组的定义方法:

1.4.1、数组的格式

数组名= (a b c d)

数组名不能重复

在这里插入图片描述

[root@test1 opt]# vim test56.sh 
[root@test1 opt]# test1=(a b c d)
[root@test1 opt]# echo ${test1[*]}
a b c d
[root@test1 opt]# echo ${test1[@]}
a b c d
[root@test1 opt]# test2[0]=1
[root@test1 opt]# test2[1]=2
[root@test1 opt]# test2[2]=3
[root@test1 opt]# echo ${test2[@]}
1 2 3

1.4.2、数组的长度

#数组的长度指的是:数组内包含了几个元素。

打印出数组的长度:

echo ${#数组名[*]} 

例子:

echo ${#test[*]}  ##打印数组内包含了几个元素

[root@test1 opt]# echo ${#test1[*]}
4
[root@test1 opt]# echo ${#test2[*]}
3

1.4.3、查看数组指定位置元素—开始为0

echo ${数组名[下标]}

例子:

[root@test1 opt]# test3=(abc 123 456 789 dahanqi)
[root@test1 opt]# echo ${test3[0]}
abc
[root@test1 opt]# echo ${test3[2]}
456
[root@test1 opt]# echo ${test3[3]}
789

1.4.4、数组遍历

[root@test1 opt]# vim shuzu.sh

#数组遍历
test=(1 2 3 4 5)
for num in ${test[*]}
do
echo -ne "$num\t"
done



[root@test1 opt]# sh shuzu.sh
1	2	3	4	5	

1.4.5、数组的切片

echo ${数组名[*]:下标:打印长度}

例子:

[root@test1 opt]# test5=(1 2 3 4 5)
[root@test1 opt]# echo ${test5[*]}
1 2 3 4 5
[root@test1 opt]# echo ${test5[*]:0:2}
1 2

在这里插入图片描述

在这里插入图片描述

#0表示起始位置,2表示步长,起始位置θ开始,包括0,移2个。

[root@test1 opt]# echo ${test5[*]:1:3}
2 3 4

1.4.6、数组的替换

#临时替换

echo ${数组名[*]/下标/数值}

例子:

[root@test1 opt]# echo ${test5[*]/4/99}
1 2 3 99 5    ##临时替换

#永久修改

通过修改元素下标的值可以实现----赋值覆盖。

[root@test1 opt]# echo ${test5[*]}
1 2 3 4 5
[root@test1 opt]# echo ${test5[*]/4/99}
1 2 3 99 5    ##临时替换
[root@test1 opt]# echo ${test5[*]}
1 2 3 4 5
[root@test1 opt]# test5[3]=99  ##重新赋值覆盖替换
[root@test1 opt]# echo ${test5[*]}
1 2 3 99 5

1.4.7、删除数组

#删除整个数组

unset 数组名

例子:

[root@test1 opt]# echo ${test1[*]}
a b c d
[root@test1 opt]# unset test1
[root@test1 opt]# echo ${test1[*]}

[root@test1 opt]# echo ${test1[*]}

[root@test1 opt]#

1.4.8、删除元素

#删除数组当中的元素—通过单个下标删除

unset 数组名[下标]

例子:

[root@test1 opt]# echo ${test5[*]}
1 2 3 99 5
[root@test1 opt]# unset test5[2]
[root@test1 opt]# echo ${test5[*]}
1 2 99 5
[root@test1 opt]# echo ${test5[3]}
99
[root@test1 opt]# echo ${test5[2]}

1.4.9、数组追加,追加元素

数组名[下标]=数值

根据下标追加

[root@test1 opt]# test5[4]=5
[root@test1 opt]# echo ${test5[*]}
1 2 3 99 5
[root@test1 opt]# test5[5]=6
[root@test1 opt]# echo ${test5[*]}
1 2 3 99 5 6

在尾部追加

数组名+=(x  y)

例子:

[root@test1 opt]# test5+=(7 8)
[root@test1 opt]# echo ${test5[*]}
1 2 3 99 5 6 7 8

#现在定义一个数组,元素都是整数,实现数组内整数的累加求和

在这里插入图片描述

test1=(10 20 30 40 50 60)
b=0
for ((i=0;i<6;i++))
do
a=`echo ${test1[i]}`
b=$(($a+$b))
done
echo $b

[root@test1 opt]# vim shuzu1.sh
[root@test1 opt]# sh shuzu1.sh
210

在这里插入图片描述

[root@test1 opt]# vim shuzu1.sh



test1=(10 43 45 47 50 60)
sum1=0
sum2=0
for i in ${test1[*]}
do
if [[ $i%2 -eq 0 ]]
then
sum1=$(($sum1+$i))
else
sum2=$(($sum2+$i))
fi
done
echo $sum1
echo $sum2



[root@test1 opt]# sh shuzu1.sh
120
135

在这里插入图片描述

root@test1 opt]# vim shuzu2.sh

test1=(3 5 8 4 56 34 76 53)
max=${test1[0]}
min=${test1[0]}
for i in ${test1[*]}
do
 if [ $i -gt $max ]
  then         
  max=$i           ##取数组里面大于的话,赋值给max,小于等于不用管
  fi
 if [ $i -lt $min ]
 then
  min=$i              ##取数组里面小于的话,赋值给min,大于等于不用管
  fi
done
echo "$max"
echo "$min"



[root@test1 opt]# sh shuzu2.sh
76
3

1.5、冒泡排序

在这里插入图片描述


#冒泡排序:#类似气泡上涌的工作,会将数组当中的元素按照从小到大,或>者从大道小的顺序进行一个重新排列。test1=(20 10 60 40 50 30)
#从小到大排列。
#思路:对比两个相邻的元素,以从小到大为例。满足交换条件的元素,小的
往左移,大的往右移动。
#数组的位置发生变化(下标对应的元素的值发生变化)
#双层循环,外部循环控制排序的轮次。内循环比较两个元素的大小,决定>时候互换位置
#对比和交换的次数随着排序轮次而减少


[root@test1 opt]# vim shuzu3.sh
test1=(20 10 60 40 50 30)
length=${#test1[*]}
for ((b=1;b<length;b++))
do
for ((a=0;a<=length-b;a++))
do
  c=${test1[$a]}
  d=${test1[(($a+1))]}
  if [ $c -gt $d ]
  then
   e=$c
   test1[$a]=$d
   test1[(($a+1))]=$c
fi
done
done
echo "${test1[*]}"



[root@test1 opt]# sh shuzu3.sh
10 20 30 40 50 60

总结:取最大值,内循环,第一次循环需要,length-1次内循环,最大值在最后,a++后,b++,外循环第二次,这时候最大值已经在最后一位,那么内循环此时需要进行length-2次,依次类推

详见下:

  • [root@test1 opt]# bash -x shuzu3.sh
    
    + test1=(20 10 60 40 50 30)
    + length=6
    + (( b=1 ))
    + (( b<length ))
    + (( a=0 ))
    + (( a<length-b ))
    + c=20
    + d=10
    + [[ 20 -gt 10 ]]
    + e=20
    + test1[$a]=10
    + test1[(($a+1))]=20
    + (( a++ ))
    + (( a<length-b ))
    + c=20
    + d=60
    + [[ 20 -gt 60 ]]
    + (( a++ ))
    + (( a<length-b ))
    + c=60
    + d=40
    + [[ 60 -gt 40 ]]
    + e=60
    + test1[$a]=40
    + test1[(($a+1))]=60
    + (( a++ ))
    + (( a<length-b ))
    + c=60
    + d=50
    + [[ 60 -gt 50 ]]
    + e=60
    + test1[$a]=50
    + test1[(($a+1))]=60
    + (( a++ ))
    + (( a<length-b ))
    + c=60
    + d=30
    + [[ 60 -gt 30 ]]
    + e=60
    + test1[$a]=30
    + test1[(($a+1))]=60
    + (( a++ ))
    + (( a<length-b ))
    + (( b++ ))
    + (( b<length ))
    + (( a=0 ))
    + (( a<length-b ))
    + c=10
    + d=20
    + [[ 10 -gt 20 ]]
    + (( a++ ))
    + (( a<length-b ))
    + c=20
    + d=40
    + [[ 20 -gt 40 ]]
    + (( a++ ))
    + (( a<length-b ))
    + c=40
    + d=50
    + [[ 40 -gt 50 ]]
    + (( a++ ))
    + (( a<length-b ))
    + c=50
    + d=30
    + [[ 50 -gt 30 ]]
    + e=50
    + test1[$a]=30
    + test1[(($a+1))]=50
    + (( a++ ))
    + (( a<length-b ))
    + (( b++ ))
    + (( b<length ))
    + (( a=0 ))
    + (( a<length-b ))
    + c=10
    + d=20
    + [[ 10 -gt 20 ]]
    + (( a++ ))
    + (( a<length-b ))
    + c=20
    + d=40
    + [[ 20 -gt 40 ]]
    + (( a++ ))
    + (( a<length-b ))
    + c=40
    + d=30
    + [[ 40 -gt 30 ]]
    + e=40
    + test1[$a]=30
    + test1[(($a+1))]=40
    + (( a++ ))
    + (( a<length-b ))
    + (( b++ ))
    + (( b<length ))
    + (( a=0 ))
    + (( a<length-b ))
    + c=10
    + d=20
    + [[ 10 -gt 20 ]]
    + (( a++ ))
    + (( a<length-b ))
    + c=20
    + d=30
    + [[ 20 -gt 30 ]]
    + (( a++ ))
    + (( a<length-b ))
    + (( b++ ))
    + (( b<length ))
    + (( a=0 ))
    + (( a<length-b ))
    + c=10
    + d=20
    + [[ 10 -gt 20 ]]
    + (( a++ ))
    + (( a<length-b ))
    + (( b++ ))
    + (( b<length ))
    + echo '10 20 30 40 50 60'
      10 20 30 40 50 60
    

二、正则表达式

正则表达式:匹配的是文本内容,linux的文本三剑客都是针对文本内容。

grep 过滤文本内容

sed 针对文本内容进行增删改查

awk 按行取列

文本三剑客----都是按照行进行匹配。

2.1、grep筛选:

grep的作用就是使用正则表达式来匹配文本内容。

选项:

-m 数字 匹配几次之后停止

[root@test1 opt]# grep -m 1 root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@test1 opt]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

-v 取反

grep -v root /etc/passwd   ##除了root,筛选所有

-n 显示匹配的内容及行号

[root@test1 opt]# grep -n root /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
10:operator:x:11:0:operator:/root:/sbin/nologin

-c 只统计匹配的行数

[root@test1 opt]# grep -c root /etc/passwd 
2

-o 仅显示匹配的结果

[root@test1 opt]# grep -o root /etc/passwd 
root
root
root
root

-q 静默模式。不输出任何信息。

[root@test1 opt]# grep -q root /etc/passwd > /opt/123.txt
[root@test1 opt]# cat /opt/123.txt
[root@test1 opt]# grep -m 1 root /etc/passwd > /opt/123.txt
[root@test1 opt]# cat /opt/123.txt
root:x:0:0:root:/root:/bin/bash

-A after 数字,后几行

grep -A 3 root /etc/passwd

-B before 数字 ,前几行

grep -B 3 root /etc/passwd

-C 数字,前后各几行

grep -C 3 root /etc/passwd

-e 或者

 grep -e root -e xy102 /etc/passwd

-E 匹配扩展正则表达式

-f 匹配两个文件相同的内容,以第一个文件为准

[root@test1 opt]# vim kl1.txt
abc
acv
abf123
234
456
aaa
bbb
ccc
abc
acv
abf123
234
456
aaa
bbb
ccc
abc
acv
abf


[root@test1 opt]# vim kl1.txt

123
345
qqq
aaa
abf
avg
afh


[root@test1 opt]# grep -f kl.txt kl1.txt
123
aaa
abf


-r 递归目录 目录下的文件内容,软连接不包含在内。

[root@test1 opt]# grep -r 123 /opt/
/opt/test41.sh:  echo 123 | passwd --stdin $user
/opt/test41.sh:  echo 123 | passwd --stdin $user
匹配到二进制文件 /opt/.123.swp

-R 递归目录 目录下的文件内容,软连接包含在内。

[root@test1 opt]# grep -R abf /opt/
/opt/nginx-1.22.0/src/core/ngx_crc32.c:    0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
/opt/999.txt:abf123

2.2、sort排序:

sort

以行为单位,对文件的内容进行排序

sort 选项 参数

[root@test1 opt]# sort 123.txt





111
112
123
222
333
555
aaa
aaa
aaa
bbb
bbb
cc
ddd
DDD
EEE
nnn

cat file | sort 选项

-f 忽略大小写,默认会把大写字母排在前面

[root@test1 opt]# sort -f 123.txt





111
112
123
222
333
555
aaa
aaa
aaa
bbb
bbb
cc
DDD
ddd
EEE
nnn

-b 忽略每行之前的空格(不是把空格删除,只是按照数字和字母的顺序排列)

[root@test1 opt]# sort -b 123.txt




​	
 123
345
345
 345
567
987
aaa
bbb
bbb
BD
bfvf
SD
sdfd
sds

-n 按照数字进行排序

[root@test1 opt]# sort -n 123.txt




​	
aaa
bbb
bbb
BD
bfvf
SD
sdfd
sds
 123
345
345
 345
567
987

-r 反向排序

[root@test1 opt]# sort -r 123.txt
sds
sdfd
SD
bfvf
BD
bbb
bbb
aaa
987
567
 345
345
345
 123
	

-u 相同的数据只显示一行

[root@test1 opt]# sort -u 123.txt

	
 123
345
 345
567
987
aaa
bbb
BD
bfvf
SD
sdfd
sds

-o 把排序后结果转存到指定的文件

[root@test1 opt]# sort -u 123.txt


 123
345
 345
567
987
aaa
bbb
BD
bfvf
SD
sdfd
sds
[root@test1 opt]# cat 123.txt | sort -rno 234.txt
[root@test1 opt]# cat 234.txt
987
567
 345
345
345
 123
sds
sdfd
SD
bfvf
BD
bbb
bbb
aaa

2.3、uniq 去重

uniq 去除连续重复的行,只显示一行

-c 统计连续行的次数,合并连续重复的行

[root@test1 opt]# uniq -c 123.txt
      1  345
      1 567
      1 987
      1  123
      2 345
      1 bbb
      1 bfvf
      2 
      1 sdfd
      1 SD
      1 BD
      2 
      1 	
      1 sds
      2 aaa
      1 bbb

-u 显示仅出现一次的行(包括不适合连续出现的重复行)

[root@test1 opt]# uniq -u 123.txt
 345
567
987
 123
bbb
bfvf
sdfd
SD
BD
	
sds
bbb


-d 仅显示连续重复的行(不包括非连续出现的内容)

[root@test1 opt]# uniq -d 123.txt   ##显示重复的行
345                          


aaa

作业

[root@test1 ~]# df -h | awk 'NR>1 {print  $5}' | tr -d '%'
15
0
0
1
0
100
18
1
0
1

#按照从大到小排列

#附加题从小到大,整行排序

test1=($(df -h | awk 'NR>1 {print $5}' | tr -d '%'))
echo "原数组的排序为:${test1[*]}"
length=${#test1[*]}
for ((j=1;j<$length;j++))
do
 for ((k=0;k<$length-j;k++))
 do
  a=${test1[$k]}
  b=${test1[$(($k+1))]}
  if [ $a -lt $b ]
   then
    c=$b
    test1[$k]=$c
    test1[$(($k+1))]=$a
    fi
done
done
echo "数组从大到小的排序为:${test1[*]}"
df -h | awk 'NR>1 {print $0, $5}' | sort -k5  -nr

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/726452.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

朝阳医院2018年销售数据 数据分析与可视化

代码及数据集下载传送门 数据分析与可视化-朝阳医院2018销售数据-ipynbcsv 实践内容 以朝阳医院2018年销售数据为例&#xff0c;目的是了解朝阳医院在2018年里的销售情况&#xff0c;这就需要知道几个业务指标&#xff0c;本次的分析目标是从销售数据中分析出以下业务指标&am…

避雷!紧急停止投稿,毕业神刊Aging危险了,被数据库“On Hold“!

本周投稿推荐 SSCI • 中科院2区&#xff0c;6.0-7.0&#xff08;录用友好&#xff09; EI • 各领域沾边均可&#xff08;2天录用&#xff09; CNKI • 7天录用-检索&#xff08;急录友好&#xff09; SCI&EI • 4区生物医学类&#xff0c;0.5-1.0&#xff08;录用…

2004年上半年软件设计师【下午题】试题及答案

文章目录 2004年上半年软件设计师下午题--试题2004年上半年软件设计师下午题--答案2004年上半年软件设计师下午题–试题

部署RAC到单实例ADG(11G)

服务器信息 主库RAC环境信息 主库RAC基本环境 节点1 节点2 OS centos 7.9 centos 7.9 数据库版本 11.2.0.4 11.2.0.4 规格 1C4G 1C4G 主机名 racdb01 racdb02 public ip 192.168.40.135 192.168.40.145 vip 192.168.40.13 192.168.40.14 private ip 192…

netcore 生成验证码

安装依赖 Install-Package Lazy.Captcha.Core 注册服务 builder.Services.AddCaptcha(); 自定义注册服务 // 注册服务的时候增加配置 services.AddCaptcha(Configuration, option > {option.CaptchaType CaptchaType.WORD; // 验证码类型option.CodeLength 6; // 验证…

广州化工厂可燃气体报警器检定检验:安全生产新举措显成效

随着科技的不断发展&#xff0c;可燃气体报警器的检定检验技术也在不断进步。 广州的一些化工厂开始采用先进的智能检测系统和数据分析技术&#xff0c;对报警器的性能进行更加精准和全面的评估。 这些新技术不仅能够提高检定检验的效率和准确性&#xff0c;还能够为化工厂的…

Python测试框架--Allure

严格意义上讲 Allure 不算是测试框架&#xff0c;但是它是生成漂亮测试报告的开源工具&#xff0c;搭配 Pytest 测试框架食用更搭。 也就是说 Allure 是在 Pytest 执行完生成的测试数据的基础上&#xff0c;对测试数据进行处理统计&#xff0c;生成格式统一、美观的测试报告。 …

Java中OOP的概念及示例

Java中OOP的概念及示例 在本指南中&#xff0c;您将学习Java中的OOP概念。面向对象编程系统&#xff08;OOP&#xff09;是一种基于“对象”的编程概念。面向对象编程的主要目的是提高程序的可读性、灵活性和可维护性。 面向对象编程将数据及其行为集中在一个称为对象的实体中…

反射机制详解

✅作者简介&#xff1a;大家好&#xff0c;我是Leo&#xff0c;热爱Java后端开发者&#xff0c;一个想要与大家共同进步的男人&#x1f609;&#x1f609; &#x1f34e;个人主页&#xff1a;Leo的博客 &#x1f49e;当前专栏&#xff1a;Java从入门到精通 ✨特色专栏&#xff…

XSS+CSRF组合拳

目录 简介 如何进行实战 进入后台创建一个新用户进行接口分析 构造注入代码 寻找XSS漏洞并注入 小结 简介 &#xff08;案例中将使用cms靶场来进行演示&#xff09; 在实战中CSRF利用条件十分苛刻&#xff0c;因为我们需要让受害者点击我们的恶意请求不是一件容易的事情…

企业为什么要进行数据资产管理工作:价值与案例剖析

在数字化浪潮席卷全球的今天&#xff0c;数据已经成为企业不可或缺的重要资产。数据资产管理&#xff0c;作为确保数据资产价值得以最大化利用的关键环节&#xff0c;正逐渐成为企业战略规划中的核心议题。本文将深入剖析企业进行数据资产管理工作的必要性&#xff0c;并结合实…

[YOLOv10:注意力机制的轻量化创新,MLCA在目标检测中的卓越表现]

本文改进:一种轻量级的Mixed Local Channel Attention (MLCA)模块,该模块考虑通道信息和空间信息,并结合局部信息和全局信息以提高网络的表达效果。 1.YOLOv10介绍 论文:[https://arxiv.org/pdf/2405.14458] 代码: https://gitcode.com/THU-MIG/yolov10?utm_source=csdn…

Visual Studio2022+cuda环境配置及代码调试

环境配置 下载并安装CUDA Toolkit 打开Visual Studio&#xff0c;新建项目。如下图所示&#xff0c;已经包含CUDA编程选项 代码调试 1、打开cu文件的属性页&#xff0c;按下图所示&#xff0c;将Host中的Generate Host Debug Information设置为“是" 2、不可勾选Nsight…

塑造化工行业新格局:探索无锡哲讯智能化定制ERP系统的关键特点

在当今科技迅猛发展的时代背景下&#xff0c;化工行业面临着前所未有的机遇与挑战。传统的管理模式已经难以满足企业日益增长的需求&#xff0c;而企业资源规划&#xff08;ERP&#xff09;系统的引入&#xff0c;为化工行业带来了全新的变革。无锡哲讯&#xff0c;作为行业领先…

如何防止三重勒索勒索软件?

您的数据被加密后&#xff0c;定期备份数据是一个很好的策略&#xff0c;可以避免支付赎金&#xff0c;但这并不意味着攻击者仍然无法占得上风。一些攻击者现在正转向三重勒索勒索软件攻击&#xff0c;扬言不仅要劫持您的数据&#xff0c;还要将这些信息泄露给公众。 这类勒索…

ffmpeg的安装教程

1.官网下载ffmpeg 进入Download FFmpeg网址&#xff0c;点击下载windows版ffmpeg&#xff08;点击左下第一个绿色的行&#xff09; 在release builds第一个绿框里面选择一个版本下载。 2.配置 下载完成后解压该压缩包单击进入ffmpeg\bin&#xff0c;会出现如下界面&#xff1…

Linux驱动开发(二)--字符设备驱动开发提升 LED驱动开发实验

1、地址映射 在编写驱动之前&#xff0c;需要知道MMU&#xff0c;也就是内存管理单元&#xff0c;在老版本的 Linux 中要求处理器必须有 MMU&#xff0c;但是现在Linux 内核已经支持无 MMU 的处理器了。 MMU的功能如下&#xff1a; 完成虚拟空间到物理空间的映射 内存保护&…

瑞尼克RNK聚四氟乙烯注射器刻度清晰纯净

四氟注射器用于抽取或者注入气体或者液体&#xff0c;四氟注射器由前端带有小孔的针筒以及与之匹配的活塞芯杆组成&#xff0c;用来将少量的液体或其注入到其它方法无法接近的区域或者从那些地方抽出&#xff0c;在芯杆拔出的时候液体或者气体从针筒前端小孔吸入&#xff0c;在…

0.5 逐行扫描(Progressive scan)简介

0.5 逐行扫描简介 逐行扫描&#xff08;Progressive scan&#xff09;是一种将图像显示在扫描式的显示设备上的方法。 逐行扫描常被用在计算机显示器上。 逐行扫描按照从左到右&#xff0c;从上到下的顺序扫描图像的所有行。如下图&#xff1a; 下图粗略的将逐行扫描与隔行…