-2

I have a column in my table with entries like:

this/is/my/dir/file
this/is/my/another/dir/file

I want to display the string without the filename:

this/is/my/dir/

This is the query which I am using:

select regexp_replace('this/is/my/another/dir/file','[^/]+','');
Incognito
  • 131
  • 2
  • 11
  • I am not sure how to achieve that objectives in Hive, but in SQL-server, you can do something like [this](https://stackoverflow.com/a/10600077/7882621), in which your expression should be '/'. Hope it helps. – LEE Hau Chon May 31 '19 at 01:25

1 Answers1

1

OK, you can use regexp_replace to remove the file and only reserve the dir path, as we know the file name does not contain the character '/' and is always located at the end of the dir path, so the regexp can be written as '[^/]+$', the examples as below, it means that replace the substring with regexp '[^/]+$' to an empty ''.

select regexp_replace('/this/is/my/dir/file','[^/]+$','') as dir;
+-------------------+
|        dir        |
+-------------------+
| /this/is/my/dir/  |
+-------------------+

select regexp_replace('this/is/my/another/dir/file','[^/]+$','') as dir;
+--------------------------+
|           dir            |
+--------------------------+
| this/is/my/another/dir/  |
+--------------------------+
Shawn.X
  • 1,161
  • 4
  • 15