Home > Archive > AWK > November 2005 > Extract filename from path
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Extract filename from path
|
|
| Jeroen Gouma 2005-11-11, 7:55 am |
| Hi,
How can I extract only the filename from the FILENAME variable,
containing the complete path?
i.e.:
FILENAME = c:\test\input\data.txt
I would like to have only data.txt in a variable.
I was thinking on something like find the last \ in FILENAME and take
a substr from there. But how to find the last \ if the lenght of the
path can vary?
Thanks a lot,
Jeroen
| |
| Ed Morton 2005-11-11, 7:55 am |
| Jeroen Gouma wrote:
> Hi,
>
> How can I extract only the filename from the FILENAME variable,
> containing the complete path?
>
> i.e.:
> FILENAME = c:\test\input\data.txt
>
> I would like to have only data.txt in a variable.
>
> I was thinking on something like find the last \ in FILENAME and take
> a substr from there. But how to find the last \ if the lenght of the
> path can vary?
>
> Thanks a lot,
>
> Jeroen
either this:
c = split(FILENAME,arr,"\\"}
f = arr[c]
print f
or:
f = FILENAME
sub(/.*[\\]/,"",f)
print f
Regards,
Ed.
| |
| William James 2005-11-11, 6:56 pm |
| Jeroen Gouma wrote:
> Hi,
>
> How can I extract only the filename from the FILENAME variable,
> containing the complete path?
>
> i.e.:
> FILENAME = c:\test\input\data.txt
>
> I would like to have only data.txt in a variable.
>
> I was thinking on something like find the last \ in FILENAME and take
> a substr from there. But how to find the last \ if the lenght of the
> path can vary?
>
> Thanks a lot,
>
> Jeroen
fname=FILENAME; sub(/.*\\/, "",fname)
| |
|
| fnam=ary(split(FILENAME,ary,"\\"));
print fnam;
--
pop is Mark
Doing nothing makes me tired 'cause I can't take a break.
--
"Jeroen Gouma" <Jeroen@somewhere.else> wrote in message
news:f149n152b8ndjtcua9rusj6eatno2ake19@
4ax.com...
> Hi,
>
> How can I extract only the filename from the FILENAME variable,
> containing the complete path?
>
> i.e.:
> FILENAME = c:\test\input\data.txt
>
> I would like to have only data.txt in a variable.
>
> I was thinking on something like find the last \ in FILENAME and take
> a substr from there. But how to find the last \ if the lenght of the
> path can vary?
>
> Thanks a lot,
>
> Jeroen
| |
| martin cohen 2005-11-11, 9:55 pm |
| Jeroen Gouma wrote:
> Hi,
>
> How can I extract only the filename from the FILENAME variable,
> containing the complete path?
>
> i.e.:
> FILENAME = c:\test\input\data.txt
>
> I would like to have only data.txt in a variable.
>
> I was thinking on something like find the last \ in FILENAME and take
> a substr from there. But how to find the last \ if the lenght of the
> path can vary?
>
> Thanks a lot,
>
> Jeroen
To find the last "\" in a string str, I do this:
k = match(str, /\\[^\]+$/) # with appropriate quoting for \\
last = (k == 0 ? str : substr(str, k+1))
The match specifies a "\" followed by at least one non-"\" followed
by the end of the string.
I often use variants of this, when I look for a character followed by
any number of characters different from the original character.
Martin Cohen
|
|
|
|
|