在提取猛砸过去两年间斜线的字符串 [英] Extracting a string between last two slashes in Bash

查看:124
本文介绍了在提取猛砸过去两年间斜线的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这可以使用正则表达式像我回答了 http://stackoverflow.com/a/33379831/3962126 <轻松完成/ A>,但是我必须这样做在bash。

所以,我发现#2最接近的问题是这个庆典:提取最后两个迪尔斯的路径,但是不同的是,如果

  DIRNAME = / A / B / C / D / E

然后我需要提取

  D


解决方案

OMG,也许这是显而易见的,但不是我的最初。我得到正确的结果:

  DIR = $(基名 - $(目录名称 - $海峡))
回声$目录

I know this can be easily done using regex like I answered on http://stackoverflow.com/a/33379831/3962126, however I need to do this in bash.

So the closest question on Stackoverflow I found is this one bash: extracting last two dirs for a pathname, however the difference is that if

DIRNAME = /a/b/c/d/e

then I need to extract

d

解决方案

OMG, maybe this was obvious, but not to me initially. I got the right result with:

dir=$(basename -- "$(dirname -- "$str")")
echo "$dir"

这篇关于在提取猛砸过去两年间斜线的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆