Linux: 仅查找字符串中最后一个 '/' 的位置

Linux: finding the position of the last '/' in a string only

我有这个字符串:

/sandbox/US_MARKETING/COMMON_DATA/BAU/FILES/2020/08/dnb_mi_081420.gz

在不知道其中有多少个“/”的情况下,我希望能够只将文件读入变量。

我希望能够进行搜索,从行中最后一个“/”开始并找到文件名“dnb_mi_081420.gz”。

我基本上想说“找到字符串中的最后一个'/',然后读取它后面的子字符串直到最后并存储它。

所以我知道它看起来像这样:

filename=substr(<position of the last'/'>,<position of first character in last string>)

那么如何找到最后一个'/'的索引位置我猜我在找什么

有人知道那是什么吗?

我也尝试过使用 basename,不幸的是,我通过 'hdfs dfs' 来访问 hadoop shell。所以一些非标准的 Linux 命令如 basename 不在那个词汇表中。我基本上必须将整个字符串存储在一个变量中并对该变量值进行操作。

您可以使用 -state 子命令以指定格式提取有关文件的信息和统计信息。由于您只需要文件名,因此格式只需 "%n"

hdfs dfs -stat "%n" /path/to/file

这可能比基于原始索引的解决方案更昂贵,但不会对性能造成有意义或明显的影响。

在bash中可以使用parameter expansion

${parameter##word}

The word is expanded to produce a pattern and matched according to the rules described below (see Pattern Matching). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the ‘#’ case) or the longest matching pattern (the ‘##’ case) deleted

示例:

$ s="/sandbox/US_MARKETING/COMMON_DATA/BAU/FILES/2020/08/dnb_mi_081420.gz" && echo ${s##*/}
dnb_mi_081420.gz
$