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.shtest1=(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.shtest1=(3 5 8 4 56 34 76 53)
max=${test1[0]}
min=${test1[0]}
for i in ${test1[*]}
doif [ $i -gt $max ]then         max=$i           ##取数组里面大于的话,赋值给max,小于等于不用管fiif [ $i -lt $min ]thenmin=$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++))
doc=${test1[$a]}d=${test1[(($a+1))]}if [ $c -gt $d ]thene=$ctest1[$a]=$dtest1[(($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.txt123
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.txt111
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.txt111
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
345345
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
sds123
345
345345
567
987

-r 反向排序

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

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

[root@test1 opt]# sort -u 123.txt123
345345
567
987
aaa
bbb
BD
bfvf
SD
sdfd
sds

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

[root@test1 opt]# sort -u 123.txt123
345345
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
567345
345
345123
sds
sdfd
SD
bfvf
BD
bbb
bbb
aaa

2.3、uniq 去重

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

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

[root@test1 opt]# uniq -c 123.txt1  3451 5671 9871  1232 3451 bbb1 bfvf2 1 sdfd1 SD1 BD2 1 	1 sds2 aaa1 bbb

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

[root@test1 opt]# uniq -u 123.txt345
567
987123
bbb
bfvf
sdfd
SD
BDsds
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++))
dofor ((k=0;k<$length-j;k++))doa=${test1[$k]}b=${test1[$(($k+1))]}if [ $a -lt $b ]thenc=$btest1[$k]=$ctest1[$(($k+1))]=$afi
done
done
echo "数组从大到小的排序为:${test1[*]}"
df -h | awk 'NR>1 {print $0, $5}' | sort -k5  -nr

在这里插入图片描述

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

dockercompose

安装dockerconpose #上传docker-compose安装包 chmod x docker-compose mv docker-compose /usr/bin/ [rootlocalhost ~]# docker-compose --version docker-compose version 1.24.1, build 4667896b文件格式以及编写注意事项 YAML 是一种标记语言&#xff0c;它可以很直观的…

数据分析BI仪表盘搭建

BI仪表盘搭建六个原则&#xff1a; 1.仪表盘搭建符合业务的阅读&#xff0c;思考和操作逻辑。 2.明确仪表盘主题&#xff0c;你的用户对什么感兴趣。 普通业务人员&#xff1a;销售&#xff1a;注册&#xff0c;激活&#xff0c;成交投放&#xff1a;消耗&#xff0c;转化率…

代码随想录算法训练营第四十六天| 121. 买卖股票的最佳时机、122.买卖股票的最佳时机II、123.买卖股票的最佳时机III

LeetCode 121. 买卖股票的最佳时机 题目链接&#xff1a;https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/description/ 文章链接&#xff1a;https://programmercarl.com/0121.%E4%B9%B0%E5%8D%96%E8%82%A1%E7%A5%A8%E7%9A%84%E6%9C%80%E4%BD%B3%E6%97%B6%E6%…

只有一个鸿蒙好?还是鸿蒙、安卓、IOS并存好?

这个话题&#xff0c;现在很敏感&#xff0c;为了防止被喷&#xff0c;我提前且清楚的交待我的观点&#xff1a;我双手欢迎鸿蒙、欢迎仓颉&#xff0c;而且我已经用行动来支持&#xff0c;比如2021年刚发布ArkUI时&#xff0c;我就第一时间上手了&#xff0c;且这几年一直在跟进…

教程:LVM操作讲解

LVM简介 在系统运维过程中&#xff0c;对磁盘扩缩容是常见的操作。如何高效的管理磁盘容量&#xff0c;lvm提供了很好的解决方案。 LVM将磁盘抽象成PV、VG、LV&#xff0c;方便用户进行磁盘管理&#xff0c;简单来讲&#xff0c;是由物理磁盘划分成PV&#xff0c;PV加入到具体…

LeetCode.51N皇后详解

问题描述 按照国际象棋的规则&#xff0c;皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。 n 皇后问题 研究的是如何将 n 个皇后放置在 nn 的棋盘上&#xff0c;并且使皇后彼此之间不能相互攻击。 给你一个整数 n &#xff0c;返回所有不同的 n 皇后问题 的解决方案…

荣耀社招 测试工程师 技术一面

面经哥只做互联网社招面试经历分享&#xff0c;关注我&#xff0c;每日推送精选面经&#xff0c;面试前&#xff0c;先找面经哥 1、自我介绍 2、具体介绍做过的项目&#xff0c;支撑的事什么业务 3、防火墙测试时、平时有写脚本或者使用第三方工具吗 4、对互联网的安全测试规…

BarTender中文版安装包下载及安装教程

​根据大数据结果显示可扩充的大容量卷标数据库&#xff1a;利用大量已设计好的标签库,从数以千计的现成标签尺寸中进行选择,也能够定义并加入自己的标签库尺寸。习惯上来说操作简单&#xff1a;BarTender条码打印软件是目前功能最强大、便捷的标签设计打印软件,在150 多个国家…

力扣-两数之和

文章目录 题目题解方法1-暴力方法2-哈希 题目 原题链接&#xff1a;两数之和 题解 方法1-暴力 我最先想到的方法就是暴力&#xff0c;两层for循环&#xff0c;也能通过。&#xff08;拿到算法题在没有思路的时候暴力就是思路&#xff0c;哈哈哈&#xff09; public class T…

深度解析RocketMq源码-高可用存储组件(一) raft协议详解

1.绪论 前面的文章已经分析过&#xff0c;以前rocketmq通过主从复制的思想实现系统的高可用&#xff0c;即在搭建集群的时候会手动的设置一个主节点和从节点&#xff0c;在写入数据的时候&#xff0c;会先写入到主broker&#xff0c;然后再同步到从节点中。但是这样会有一个问…

坎德拉candela3d光伏电站三维设计软件【无标题】

Candela3D 是一款基于 SketchUp&#xff08;草图大师&#xff09;开发的新一代光伏电站三维设计软件。它适用于复杂地形、平坦地形光伏电站的建设项目&#xff0c;同时适用于可研、初设、施工图、项目运营等阶段。这款软件具有多项功能&#xff0c;例如&#xff1a; • 能够突…

【云原生】Docker可视化工具Portainer使用详解

目录 一、前言 二、docker可视化管理概述​​​​​​​ 2.1 什么是docker可视化管理 2.1.1 Docker可视化管理常用功能 2.2 为什么需要docker可视化管理工具 2.3 docker可视化工具带来的好处 三、常用的docker容器可视化管理工具解决方案 3.1 Portainer 3.2 Rancher 3…

【Python机器学习】k均值聚类——矢量量化,或者将k均值看作分解

虽然k均值是一种聚类算法&#xff0c;但在k均值和分解方法之间存在一些相似之处。k均值尝试利用簇中心来表示每个数据点&#xff0c;可以看作仅用一个分量来表示每个数据点&#xff0c;该分量由簇中心给出。这种观点将k均值看作是一种分解方法&#xff0c;其中每个点用单一分量…

可视化大屏开发系列——DataV的使用

以下内容为近期个人学习总结&#xff0c;若有错误之处&#xff0c;欢迎指出&#xff01; 可视化大屏开发系列——DataV的使用 一、介绍二、注意事项1、技术支持2、兼容性3、状态更新 三、实现效果四、使用&#xff08;在vue2项目中&#xff09;1.npm安装2.main.js中引入3.开启愉…

QNX OS微内核系统

微内核架构 微内核(Microkernel)架构是一种操作系统架构模式,其核心思想是尽量将操作系统的基本功能压缩在最小的核心中,而将其他服务(如设备驱动、文件系统、网络协议等)放在用户空间中运行,从而增加系统的灵活性和安全性,这种架构有几个主要特点和优势: 最小化核心…

WordPress软件下载主题Inpandora

Inpandora&#xff08;中文名为潘多拉&#xff09;是一款基于软件下载站定制的WordPress主题&#xff0c;帮助站长使用WordPress快速搭建一个专业的WordPress软件博客。Inpandora这款WordPress主题可以说是因软件而生&#xff0c;从UI设计到后台设置功能&#xff0c;都充分体现…

设计软件有哪些?贴图插件篇(1),渲染100邀请码1a12

设计师经常要处理贴图&#xff0c;这里介绍一些贴图所用到的插件。 1、Substance 3D Painter Substance 3D Painter是Substance 3D软件套件中的一部分&#xff0c;是一款专业的纹理绘制软件。它提供了直观的界面和强大的工具&#xff0c;用于在3D模型上进行高质量的纹理绘制和…

基于51单片机数字频率计的设计资料

题目&#xff1a;基于51单片机数字频率计的设计 系 部&#xff1a; 专 业&#xff1a; 学 号&#xff1a; …

【七】【QT开发应用】跨UI发送信号,跨线程发送信号

跨UI发送信号 基本框架 新建窗口 自定义信号 跨线程发送信号 新建线程 查看线程号 完整代码 跨UI发送信号 setdialog.h #ifndef SETDIALOG_H #define SETDIALOG_H#include <QDialog>namespace Ui { class setdialog; }class setdialog : public QDialog {Q_OBJECTpub…

1panel OpenResty 设置网站重定向

当我们部署网站时需要&#xff0c;输入"cheshi.com"域名回车&#xff0c;希望他自动跳转https://cheshi.com/indx/&#xff0c;而不是直接跳转https://cheshi.com时可以利用重定向来实现&#xff0c; 这里演示的是 1panel 如何设置&#xff08;nginx 貌似也是这样配…