数组、正则表达式、排序

数组

定义:在集合当中指定多个元素,元素的类型可以是整数也可以是字符串,也可以是浮点。

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

数组的定义方法:

数组名= (a b c) 数组名不能重复
方法1
[root@test2 opt]# test1=(a b c d)
[root@test2 opt]# echo ${test1[*]}
a b c d
​
方法2
[root@test2 opt]# test2[0]=1          素组内部元素值从零开始
[root@test2 opt]# test2[1]=2
[root@test2 opt]# test2[2]=3
[root@test2 opt]# echo ${test2[@]}
1 2 3

数组内部的元素值从零开始

数组的长度指的是数组内部包含了几个元素。
数组内部一共有几个元素
[root@test2 opt]# echo ${#test2[*]}
3
指定查看第几位元素【位置数-1】
[root@test2 opt]# test3=(aaa bbb 123 456)
[root@test2 opt]# echo ${test3[0]}
aaa
[root@test2 opt]# echo ${test3[3]}
456

数组的遍历

模块
[root@test2 opt]# vim test1.sh
​
test1=(1 2 3 4 5)
for sum in ${test1[*]}
do
echo $sum
done
结果
[root@test2 opt]# sh test1.sh 
1
2
3
4
5
数组的切片
[root@test2 opt]# test5=(1 2 3 4 5)
[root@test2 opt]# echo ${test5[*]}
1 2 3 4 5
[root@test2 opt]# echo ${test5[*]:0:2}
1 2       起始位置0开始,包括0,移两位
​
:0  表示起始位置
:2  表示步长
数组替换
临时替换
[root@test2 opt]# echo ${test5[*]}
1 2 3 4 5
[root@test2 opt]# echo ${test5[*]/4/99}
1 2 3 99 5
​
永久替换
[root@test2 opt]# echo ${test5[*]}
1 2 3 4 5
[root@test2 opt]# test5[3]=99
[root@test2 opt]# echo ${test5[*]}
1 2 3 99 5

永久修改可以通过修改元素下标的值实现

删除数组
删除整个数组
[root@test2 opt]# unset test1
[root@test2 opt]# echo ${test1[*]}
​
删除数组指定元素
[root@test2 opt]# echo ${test5[*]}
1 2 3 4 5
[root@test2 opt]# unset test5[3]
[root@test2 opt]# echo ${test5[*]}
1 2 3 5

把元素当中的值删掉后其他元素的位置不变

在数组中元素追加
[root@test2 opt]# test5[3]=4
[root@test2 opt]# echo ${test5[3]}
4

自动追加

[root@test2 opt]# test5=(1 2 3 4)
[root@test2 opt]# echo ${test5[*]}
1 2 3 4
[root@test2 opt]# test5+=(5 6)
[root@test2 opt]# echo ${test5[*]}
1 2 3 4 5 6

例题

[root@test2 opt]# vim test1.sh 
#定义一个数组,元素都是整数,实现数组内的整数累加求求和。
sum=0
test1=(10 20 30 40)
for i in ${test1[*]}
do
   sum=$(($i+$sum))
done
echo $sum
​
[root@test2 opt]# sh test1.sh 
100
#定义一个数组,元素都是整数,实现数组内的整数累加求求和。
#奇数和奇数相加,偶数和偶数相加
sum=0
sum1=0
test1=(10 21 30 41)
for i in ${test1[*]}
do
if [[ $i%2 -eq 0 ]]
then
   sum=$(($i+$sum))
else
   sum1=$(($i+$sum))
fi
done
echo "偶数和" $sum
echo "奇数和" $sum1
结果
[root@test2 opt]# sh test1.sh 
偶数和 40
奇数和 62
[root@test2 opt]# vim test1.sh 
#定义一个数组,使用判断条件找出最大值和最小值并打印
#定义一个数组,使用判断条件找出最大值和最小值并打印
test1=(3 5 7 9 1 45 20)
max=${test1[0]}
min=${test1[0]}
for i in ${test1[*]}
do
if [[ $i -gt $max ]]
then
max=$i
fi
if [[ $i -lt $min ]]
then
min=$i
fi
done
echo "最大值" $max
echo "最小值" $min
结果
[root@test2 opt]# sh test1.sh 
最大值 45
最小值 1

[root@test2 opt]# vim test1.sh
test1=($(df -h | awk 'NR>1 {print $5}' | tr -d '%'))
a=${#test1[*]}
for ((i=1; i<$a; i++))
do
    for ((b=0; b<$a-i; b++))
     do
      a1=${test1[$b]}
      c=$(($b+1))
      b1=${test1[$c]}
     if [ $a1 -lt $b1 ]
      then
        temp=$a1
        test1[$b]=$b1
        test1[$c]=$temp
      fi
    done
done
echo "修改结果" ${test1[*]}
#拓展
df -h | awk 'NR>1 {print $0 $5}' | tr -d '%' | sort -nr -k5
结果
[root@test2 opt]# sh test1.sh 
修改结果 100 18 14 1 1 1 1 0 0 0
/dev/sr0                 4.3G  4.3G     0  100 /run/media/root/CentOS 7 x86_64100
/dev/sda1               1014M  179M  836M   18 /boot18
/dev/mapper/centos-root   36G  4.8G   31G   14 /14
tmpfs                    781M  4.0K  781M    1 /run/user/421
tmpfs                    781M   36K  781M    1 /run/user/01
tmpfs                    3.9G   13M  3.8G    1 /run1
/dev/mapper/centos-home   18G   33M   18G    1 /home1
tmpfs                    3.9G     0  3.9G    0 /sys/fs/cgroup0
tmpfs                    3.9G     0  3.9G    0 /dev/shm0
devtmpfs                 3.8G     0  3.8G    0 /dev0

面试题

冒号排序

类似于气泡商用的工作,会将数组当中的元素从小到大或者从大到校的顺序进行一个重新排列

[root@test2 opt]# vim test2.sh 
#类似于气泡商用的工作,会将数组当中的元素从小到大或者从大到校的顺序进行一个重新排列
test2=(20 30 40 60 10 100)
#从小到达排序
#思路:对比两个相邻的元素,从小到大为列。满足条件的元素小的往左大的往右
#数组的位置发生变化(下标对应的元素值发生变化)
length=${#test2[*]}
for ((a=1; a<$length; a++))
do
    for ((b=0; b<$length-a; b++))
     do
first=${test2[$b]} 0=10
j=$(($b+1))
second=${test2[$j]} 1=5
if [ $first -gt $second ] 10 > 5
then
temp=$first
test1[$b]=$second
test1[$j]=$temp
fi
done
done
echo "排序" ${test2[*]}
​

正则表达式

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

grep 过滤文本内容

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

awk 按行取列

文本三剑客

都是按行进行匹配

grep

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

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

[root@test2 opt]# grep -m 1 root /etc/passwd
root:x:0:0:root:/root:/bin/bash

grep -v 取反

grep -n 显示匹配的行号

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

grep -c 只统计匹配行数

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

grep -o 仅显示匹配的结果

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

grep -q 不输出任何信息 没有任何结果

grep -A after 数字 匹配到当前行包括后几行

[root@test2 opt]# grep -A 3 root /etc/passwd
root:x:0:0:root:/root:/bin/bash
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
--
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
​

grep -B 数字 之间的行

grep -C 数字 前后各几行

[root@test2 opt]# grep -C 3 wbl /etc/passwd
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
wbl:x:1000:1000:wbl:/home/wbl:/bin/bash
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
nginx:x:987:981:Nginx web server:/var/lib/nginx:/sbin/nologin
dhcpd:x:177:177:DHCP server:/:/sbin/nologin

grep -e 过滤

grep -E 匹配扩展正则表达式

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

[root@testl opt]# vim 123.txt
[root@testl opt]## vim 456.txt
[root@testl opt]# cat 123.txt
123456
aaa
CCC
VVV
[root@testl opt]# cat 456.txt
123
456
VVV
uuu
[root@testl opt]# grep -f 123.txt 456.txt
123
456
VVV

grep -r 递归目录 目录下的文件内容

[root@test2 opt]# grep -r 123 /opt/
/opt/test1.txt:123
/opt/test2.txt:123

grep -R 递归目录 目录下的文本内容,包括软连接

排序

sort

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

sort 选项 参数

cat file | sort 选项

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

-b 忽略每行之前的空格

[root@test2 opt]# sort -b test1.txt
123
    456
aaa
CCCC
ddd

-n 按照数字进行排序

-r 反向排序

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

[root@test2 opt]# sort -u test1.txt
​
123
456
    456
aaa
CCCC
ddd

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

uniq

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

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

-u 显示仅出现一次的行,同时包括不是连续出现的重复行

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

  • 9
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值