Home > Archive > AWK > July 2006 > EASY QUESTION, how to delete the dir name but keep filename
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 |
EASY QUESTION, how to delete the dir name but keep filename
|
|
| KamilCzauz@gmail.com 2006-07-19, 6:57 pm |
| Hey guys, ive got a quick question for you, ive been working on this
all day wrong, trying to set FS='"/" and then keeping only the last
field, but i dont know how to do it.
basically i want to get the string:
/tortPXR/Implementation/Target/data/SimStim/DbMgr/Sounds/Sensors/shallow_00p.wav
to only say
shallow_00p.wav
My theory would be to split the fields by "/" and keep the last field,
but i have different directory names with different number of "/" so i
guess that wouldnt work. My next theory is to use sub or gsub, but i
dont know how to use it well enough to even begin this.
Someone, please help me:(
ps, im using gawk on linux FC2, and this has to be included in my
script, where the dir+filename is in a variable called DirAndFileName.
THANK YOU!!!
Kamil Czauz
please help
| |
| Xicheng Jia 2006-07-19, 6:57 pm |
| KamilCzauz@gmail.com wrote:
> Hey guys, ive got a quick question for you, ive been working on this
> all day wrong, trying to set FS='"/" and then keeping only the last
> field, but i dont know how to do it.
>
> basically i want to get the string:
>
> /tortPXR/Implementation/Target/data/SimStim/DbMgr/Sounds/Sensors/shallow_00p.wav
>
> to only say
>
> shallow_00p.wav
>
>
> My theory would be to split the fields by "/" and keep the last field,
> but i have different directory names with different number of "/" so i
> guess that wouldnt work. My next theory is to use sub or gsub, but i
> dont know how to use it well enough to even begin this.
>
> Someone, please help me:(
>
> ps, im using gawk on linux FC2, and this has to be included in my
> script, where the dir+filename is in a variable called DirAndFileName.
>
there are several ways, I assumed you have pre-defined the variable
'str' like:
str="/tortPXR/Implementation/Target/data/SimStim/DbMgr/Sounds/Sensors/shallow_00p.wav"
then
{ n = split(str, f, "/"); print f[n]; }
or
{ sub(".*/", "", str); print str; }
BTW. since awk's array is associative array, I am not sure if 'f[n]'
here can hold the last element of the array. Can someone here comfirm
this information? Many thanks..
Xicheng
| |
| Chris F.A. Johnson 2006-07-19, 6:57 pm |
| On 2006-07-19, KamilCzauz@gmail.com wrote:
> Hey guys, ive got a quick question for you, ive been working on this
> all day wrong, trying to set FS='"/" and then keeping only the last
> field, but i dont know how to do it.
>
> basically i want to get the string:
>
> /tortPXR/Implementation/Target/data/SimStim/DbMgr/Sounds/Sensors/shallow_00p.wav
>
> to only say
>
> shallow_00p.wav
>
>
> My theory would be to split the fields by "/" and keep the last field,
> but i have different directory names with different number of "/" so i
> guess that wouldnt work. My next theory is to use sub or gsub, but i
> dont know how to use it well enough to even begin this.
>
> Someone, please help me:(
>
> ps, im using gawk on linux FC2, and this has to be included in my
> script, where the dir+filename is in a variable called DirAndFileName.
If you have the string in a variable, why would you use awk? YOu
can do it with parameter expansion:
filename=${pathname##*/}
It makes sense to use awk if you have a file of such strings:
awk -F/ '{ print $NF }'
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| KamilCzauz@gmail.com 2006-07-19, 6:57 pm |
| Xicheng Jia wrote:
>
> { sub(".*/", "", str); print str; }
>
Thank you so much Xicheng, this works perfectly, i wish i knew how to
use the sub function better (and awk programming alltogether).
Thank you so much
Kamil
| |
| Cesar Rabak 2006-07-19, 9:56 pm |
| KamilCzauz@gmail.com escreveu:
> Hey guys, ive got a quick question for you, ive been working on this
> all day wrong, trying to set FS='"/" and then keeping only the last
> field, but i dont know how to do it.
>
> basically i want to get the string:
>
> /tortPXR/Implementation/Target/data/SimStim/DbMgr/Sounds/Sensors/shallow_00p.wav
>
> to only say
>
> shallow_00p.wav
>
since you report using Linux, would it be a solution to you just calling
basename?
$ basename
/tortPXR/Implementation/Target/data/SimStim/DbMgr/Sounds/Sensors/shallow_00p.wav
shallow_00p.wav
HTH
--
Cesar Rabak
GNU/Linux User 52247.
Get counted: http://counter.li.org/
|
|
|
|
|