我有:
/var/cache/pacman/pkg/vim-runtime-7.3.754-1-x86_64.pkg.tar.xz
/var/cache/pacman/pkg/vim-7.3.918-2-x86_64.pkg.tar.xz
格式为:
cachedir package packageVersion packageRelease architecture extesnion
/var/cache/pacman/pkg/ vim-runtime 7.3.754 1 x86_64 pkg.tar.xz
/var/cache/pacman/pkg/ vim 7.3.918 2 x86_64 pkg.tar.xz
所以我想最终能够从原始字符串中提取任何内容 *包名称中可以包含连字符
任何帮助都会被认定
答案 0 :(得分:1)
将basename
与sed
一起使用:
basename "/var/cache/pacman/pkg/vim-runtime-7.3.754-1-x86_64.pkg.tar.xz" \
| sed 's/\([a-z\-]*\)\-\(.*\)/\1/i'
输出:
vim-runtime # or ..
vim
答案 1 :(得分:0)
提示:
$ cachedir=$(dirname /var/cache/pacman/pkg/vim-runtime-7.3.754-1-x86_64.pkg.tar.xz)
$ printf '%s\n' "$cachedir"
/var/cache/pacman/pkg
$ basename=$(basename /var/cache/pacman/pkg/vim-runtime-7.3.754-1-x86_64.pkg.tar.xz)
$ printf '%s\n' "$basename"
vim-runtime-7.3.754-1-x86_64.pkg.tar.xz
$ IFS=- read -a components <<< "$basename"
$ printf '%s\n' "${components[@]}"
vim
runtime
7.3.754
1
x86_64.pkg.tar.xz
$ printf '%s\n' "${components[-1]}"
x86_64.pkg.tar.xz
$ extension="${components[-1]#*.}"
$ printf '%s\n' "$extension"
pkg.tar.xz
$ architecture="${components[-1]%%.*}"
$ printf '%s\n' "$architecture"
x86_64
$ packageRelease="${components[-2]%%.*}"
$ printf '%s\n' "$packageRelease"
1
$ packageVersion="${components[-3]}"
$ printf '%s\n' "$packageVersion"
7.3.754
$ package=$(echo "${components[@]:0:$((${#components[@]} - 3))}" | tr -s ' ' '-')
$ printf '%s\n' "$package"
vim-runtime
答案 2 :(得分:0)
var='/var/cache/pacman/pkg/vim-runtime-7.3.754-1-x86_64.pkg.tar.xz'; basename ${var%%-[0-9]*}