Re: How to extract string from filename

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Linux has or can have apache server running to give access to PHP, and I can 
see that working better using the explode(string, separator-char) command 
using the _ as separator,

zz=explode(string, char);

this gives an array of sub-string without the _'s,  and you need the second 
to last.  There's another function to count elements in an array,  so x-1 
where x is the number of elements.  Might be x-2 as first element is 0.

I oculd get more specific if I dipped into all my old, but still used, PHP 
scripts.

implode(array, separator) like zz2=implode(array, char), can put it back as 
a long string separated by the char.

I love the relative simplicity of PHP for all this.

It can suck in an entire file into a string then save the modified string to 
a new, or back to the old, file.

You'd use .mp3 as separator in this first instance.

Hth, BobH.

----- Original Message ----- 
From: "Tony Baechler" <tony@xxxxxxxxxxxx>
To: "Linux for blind general discussion" <blinux-list@xxxxxxxxxx>
Sent: Wednesday, July 29, 2015 12:13 PM
Subject: How to extract string from filename


Hi all,

The recent discussion on shell scripts got me thinking.  A couple of posters
invited people to post problems they're having with scripts to the list, so
here goes.

I have not actually written a script for this because I'm not sure how to go
about it.  I would normally use cut, but I need to cut from right to left.
The cut help doesn't indicate a way to do this.  You can only cut from the
beginning of the line or a range of bytes.  The problem is each line
(filenames, to be exact) are of different lengths, so it's impossible to
know what range of bytes I need.

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

As you can see, they all follow a similar format.  If I could go from right
to left, I would simply cut "_default.mp3" and extract the preceeding 8
bytes, but I can't figure out how.  What I'm trying to do is first extract
the PIDs, hopefully preserving the filenames in the process.  Once they are
extracted (or printed to stdout), I want to use wget to download the BBC
programme page.  If you go to www.bbc.co.uk/programme/bXXXXXXX, you'll get a
web page displaying the broadcast date, description and notes.  I would like
to download those pages.

Any help with this would be greatly appreciated.  Thanks in advance.

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

_______________________________________________
Blinux-list mailing list
Blinux-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/blinux-list 

_______________________________________________
Blinux-list mailing list
Blinux-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/blinux-list



[Index of Archives]     [Linux Speakup]     [Fedora]     [Linux Kernel]     [Yosemite News]     [Big List of Linux Books]