How to extract string from filename

Tony Baechler tony at baechler.net
Thu Jul 30 08:44:35 UTC 2015


Thanks, but that doesn't work.  First, what do I pass to cut?  Since each 
file has a different number of words (I apologise if that wasn't obvious) I 
can't use -f or -b because the field and byte offsets are different.  Using 
basename to remove "_default.mp3" and "cut -d_ -f6" I get some of the PIDs, 
but I also get random words, like "Mars" and "Earthquake" since those words 
are in the original filenames.  What next?

On 7/29/2015 4:30 AM, Geoff Shang wrote:
> 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.
>
> _______________________________________________
> Blinux-list mailing list
> Blinux-list at redhat.com
> https://www.redhat.com/mailman/listinfo/blinux-list

-- 

--------------------
Tony Baechler, Baechler Access Technology Services
Putting accessibility at the forefront of technology
mailto:bats at batsupport.com
Phone: 1-619-746-8310   Fax: 1-619-449-9898




More information about the Blinux-list mailing list