当前位置: 技术问答>linux和unix
小弟shell新手,求一截取字符串方法
来源: 互联网 发布时间:2017-05-03
本文导语: /home/.../..../version/version_20131128100829.ini中的201311281008截取出来。 | 写一个脚本: #!/bin/sh foo=/home/.../..../version/version_20131128100829.ini var1=`echo ${foo##*_}` var2=`echo ${var1%.*} echo $var2` | ...
/home/.../..../version/version_20131128100829.ini中的201311281008截取出来。
|
写一个脚本:
#!/bin/sh
foo=/home/.../..../version/version_20131128100829.ini
var1=`echo ${foo##*_}`
var2=`echo ${var1%.*}
echo $var2`
#!/bin/sh
foo=/home/.../..../version/version_20131128100829.ini
var1=`echo ${foo##*_}`
var2=`echo ${var1%.*}
echo $var2`
|
没有其他要求?
#!/bin/bash
a="/home/.../..../version/version_20131128100829.ini"
echo ${a:31:45}
|
echo /home/.../..../version/version_20131128100829.ini | sed 's/.*_//;s/....$//'
echo /home/.../..../version/version_20131128100829.ini | sed -r 's/.*_//;s/[^0-9]+$//'
echo /home/.../..../version/version_20131128100829.ini | sed -r 's/.*_([0-9]+).*/1/'
echo /home/.../..../version/version_20131128100829.ini | awk -F_ '{gsub(/[^0-9]/,"",$NF);print $NF}'
basename /home/.../..../version/version_20131128100829.ini | cut -d "_" -f 2 | cut -d "." -f 1
str="/home/.../..../version/version_20131128100829.ini"; str=${str#*_}; echo ${str%.ini}