Reputation: 17467
Here is the example
SF_Library/example/Platform/Analyses-PLATFORM.part0.xml
SF_Library/example/Platform/Models-PLATFORM.part0.xml
SF_Library/example/Platform/Models-PLATFORM.car
SF_Library/example/Platform/DS-PLATFORM.car
I want to grab base path which is following.
SF_Library/example/Platform/
Anybody know what regex should i use for it?
Upvotes: 0
Views: 3234
Reputation: 182000
Alright, I'll indulge you.
^(.*/).*$
Dissection:
^ beginning of string
( start of capture group
.* series of any number of any character
/ a slash
) end of capture group
.* series of any number of characters that are not slashes
$ end of string
This works because *
is greedy: it matches as many characters as it can (so it will include all the slashes right up to the last one).
But as the other answers have pointed out, a regex is probably not the best way to do this.
Upvotes: 1
Reputation:
Regexes ain't for extracting substrings. Why not use the dirname
command?
$ dirname /home/foo/whatever.txt
/home/foo
$
If you need it in a variable:
DIRECTORY=`basename "SF_Library/example/Platform/DS-PLATFORM.car"`
Upvotes: 3
Reputation: 121407
You can use dirname command:
dirname SF_Library/example/Platform/DS-PLATFORM.car
It'll give you: SF_Library/example/Platform
Upvotes: 2
Reputation: 18864
You don't need a regex:
#!/bin/bash
fullpath="SF_Library/example/Platform/Analyses-PLATFORM.part0.xml"
# or if you read them then: while read fullpath; do
basename=${fullpath%/*}
# or if you read them then: done < input_file.txt
Upvotes: 8