如何删除字符串末尾的指定字符?

how to remove specify characters at the end of string?

我有一些路径是这样的:

/dir
/dir//
/dir///
/path/to/dir
/path/to/dir/
/path/to/dir///
/path/to/dir/////////

我想使用 bash 使其更清楚。我想删除路径末尾的所有 / 个字符并具有: /path/to/dir/dir 末尾没有 /

这可能是一个可能的解决方案:

sed -e 's/[/]*$//g'

例如:

echo '/path/to/dir/////////' | sed -e 's/[/]*$//g'

给你:

/path/to/dir

关注 awk 可能会对您有所帮助。

awk '{sub(/\/+$/,"")} 1'  Input_file