方法
两种方法:grep和sed
echo "libgcc-4.8.5-4.h5.x86_64.rpm" | grep -Eo "[0-9]+\.[0-9]+.*x86_64"
echo "libgcc-4.8.5-4.h5.x86_64.rpm" | sed -r "s/libgcc-([0-9]+\.[0-9]+.*)\.rpm/\1/g"
转载:https://www.cnblogs.com/jmliao/p/11808592.html
实验
但是grep试了半天,没找到能输出分组的办法,所以只能选择使用sed
假设有个文件a.txt
$ cat a.txt
xxxx ddd zzz fff
jdbc.url=jdbc:mysql://www.xxxx.com:3308/db_xxx?abcdefgh
jdbc.user=admin
jdbc.password=123456
nnn hhh
想要获取host、port和db,我们可以写成下面的脚本
#!/bin/bash
host=`cat a.txt | grep "jdbc:mysql" | sed -r "s/.*:mysql:\/\/(.*):(.*)\/(.*)\?.*/\1/g"`
port=`cat a.txt | grep "jdbc:mysql" | sed -r "s/.*:mysql:\/\/(.*):(.*)\/(.*)\?.*/\2/g"`
dbname=`cat a.txt | grep "jdbc:mysql" | sed -r "s/.*:mysql:\/\/(.*):(.*)\/(.*)\?.*/\3/g"`
echo $host
echo $port
echo $dbname
结果:
$ sh help.sh
www.xxxx.com
3308
db_xxx