How to extract string from filename

Geoff Shang geoff at QuiteLikely.com
Wed Jul 29 11:30:04 UTC 2015


On Wed, 29 Jul 2015, Tony Baechler wrote:

> What I'm trying to do is extract the BBC PID from the downloaded files. It's 
> a lower case alphanumeric string which starts with a letter and is eight 
> characters.  In my case, the first letter is always "b" or "p," so if I could 
> use something like grep to just extract the first lower case letter followed 
> by a number up to the next underscore, that would be good.  I don't think 
> grep will just print a matching phrase, only the matching line.  Here are 
> some example filenames:
>
> 5_live_Science_-_Coding_and_Computers_b062dj5j_default.mp3
> Witness_-_The_Sinking_of_the_USS_Indianapolis_p02wdykn_default.mp3
> Discovery_-_A_Scientific_View_of_Agriculture_p0053gbd_default.mp3
> Click_-_05_10_2010_p00b18gp_default.mp3

You can use basename to get rid of the end.

$ basename Click_-_05_10_2010_p00b18gp_default.mp3 _default.mp3
Click_-_05_10_2010_p00b18gp

You could then use cut with this youtput to get what ou want.

HTH,
Geoff.




More information about the Blinux-list mailing list