Portal Home > Knowledgebase > Articles Database > bash long file names and stripping
bash long file names and stripping
Posted by ti_nhatrang, 07-14-2012, 08:58 PM |
Hi all,
I am having issues trying to strip out words, symbols from long file names. I got 80% of it work but not really, here's what I have, please help shed some light on me so I can learn this once and for all.
The above is i'm trying to replace the symbols, mp4s, avis, apples with a " " space... it doesn't seem to like it.
|
Posted by MattF, 07-15-2012, 03:36 AM |
Note sure what you want but I have:
item="WebHostingTalk_The_Movie.avi"
friendly=${item//(.avi|.mov|.mp4|-|_|.)/ }
There are more resilent ways of handling this though, Im too lazy to google but there's probably a regex meta symbol to match symbols such as [^\w] and you might want to do it in two steps using $ (end of string) to remove file extension first.
|
Posted by ti_nhatrang, 07-15-2012, 03:38 AM |
Yes, there are so many ways to do this, im trying to do this with one line - i will try your method and get back to you. Thanks for your help.
|
Posted by MattF, 07-15-2012, 03:42 AM |
doh, just noticed Im in zsh, the above wont work in bash, bear with me
|
Posted by MattF, 07-15-2012, 03:48 AM |
item="WebHostingTalk_The_Movie.avi"
friendly=$(echo $item | sed -e 's/\(.avi$\|.mp4$\|.mov$\|-\|_\)/ /g')
g just makes global - like // in bash
the $ signifies end of string (unless multiline mode)
|
Posted by RoseHosting, 07-15-2012, 09:01 AM |
In bash, you need to set the extglob option, which gives you access to regular expressions in glob patterns.
http://mywiki.wooledge.org/glob
|
Posted by ti_nhatrang, 07-15-2012, 08:27 PM |
this doesn't work as it still keeps the / and . for some apparent reason
|
Add to Favourites Print this Article
Also Read