Programming and Scripting :: How to write this small script



piccolo, I'd try using the wget command to get the contents of the web page and then use grep and possibly awk to trim it down into the parts you want.  I would use pipes to feed the output of one command into the input for another and string them together to do what you want.

wget can send a web page's source to the standard output if you use the -O - switch like so:

wget http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm -O -

Now you can look at the text and see what the lines you want have on them.  I noticed they contain a time, so we can use grep to print only lines that have a time on them by "piping" the output from the wget command into the grep command using a | symbol:

wget http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm -O -  | grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"

You can further narrow down the lines by using sed, a stream editor, to print only the parts you want (the \ at the end of the first two lines allows you to continue a line if the command gets too long):

wget http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm -O -  | \
    grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
    sed 's/.*>\(.*\)<br><\/font>/\1/'

This prints out two lines, and the first one is the one you want, so use head to print only the first line (using the -n 1 switch):

wget http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm -O -  | \
    grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
    sed 's/.*>\(.*\)<br><\/font>/\1/' | \
    head -n 1

Now you can see that the title of the song is after the comma, so only print out what is after the comma and that following space:

wget http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm -O -  | \
    grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
    sed 's/.*>\(.*\)<br><\/font>/\1/' | \
    head -n 1| \
    sed 's/.*, \(.*\)/\1/'

And there is the title of the current song.  A bash script could take the above code and set a variable to its value:

Code Sample
#!/bin/bash

SONGTITLE=`wget http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm -O -  | \
    grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
    sed 's/.*>\(.*\)<br><\/font>/\1/' | \
    head -n 1| \
    sed 's/.*, \(.*\)/\1/'`

echo "The current song is $SONGTITLE"

What an excellent job.  I did not even begin on it and here is the solution already.  Only one problem as I see it.  When I ran the script I did not get the current song but the one before, so I changed the "head -n 1" to "tail -n 1" and the current song printed out.  For Piccolo: the "tail" command works just like the "head" command except it counts from the end of the file.  So "tail -n 1" tells it to print the last line.
Enjoy
--Ted

Piccolo:
Question for you:  Where do you get the files of song lyrics?
--Ted

WOW! I'm very excited. Thanks very much. You all have helped me, from Clacker who did translate that wrote Dare2Dreamer into Bash code to finally the small but opportune correction of Tedmoore99 whose words encourage me to follow improving.
However I must say that who have worked really hard am I. Because I thougth the script name: The script is called "clacker" in honour to "The GodBash". :D

Let know that due I live inside a community that not speaks english, to offer help on DSL forum is a remote method to learn english that I'm using. But I do not understand nothing from that I listen. Evidently to read, write, talk the mechanisms are different then to exercise my sense of hearing I need read the exact text from that I listen.
Heretofore I was doing this manually losing time and therefore reading the lyrics later, sometimes too late the song had begun.
This script is really very useful for my purpose!.

I below have posted the script that can be useful for others that want learn english like me.
The interested can contact me if need lyrics files or any other kind of help about the script.

The radio can be listened by download the play.pls file from:
www.live365.com/cgi-bin/play.pls?stationid=309492
Then play with command:
mplayer -playlist play.pls

I'm saving the lyrics files inside the same directory were is the script with exactly the "$ SONGTITLE" filename given by the script. Generally I found the lyrics in this website: http://www.lyricsplayground.com  

Code Sample
#!/bin/bash
# Real time lyrics viewer for Jones College Radio (http://wktz.jones.edu)

i=1
while [ i=1 ] # infinite loop

do
 SONGTITLE=`wget http://wktz.jones.edu/wktz/BSI_WKTZFM_Page.htm -O -  | \
 grep "[0-9][0-9]*:[0-9][0-9]*:[0-9][0-9]*"  | \
 sed 's/.*>\(.*\)<br><\/font>/\1/' | \
 tail -n 1 | \
 sed 's/.*, \(.*\)/\1/'`

 echo "Currently playing: $SONGTITLE"
 
 if [ "$SONGTITLE1" != "$SONGTITLE" ]   # Check if the playlist was updated
   then                              #+ prevent open one lyrics file more than one time.  
     echo "New song playing"
     if [ -e "$SONGTITLE" ]                                      # Check if lyrics file exists.
       then
         # Write here command to open the $SONGTITLE file using text editor.
        kate "$SONGTITLE"                         # Kate text editor in this example
       else
      # write here command if want to generate automatically empty lyrics files
        echo "$file Lyrics file does not exist yet."
     fi
 fi

SONGTITLE1="$SONGTITLE"   # Actual song title stored for next iteration
sleep 10                              # Delay 10 seg before retrieve playlist again.

done

#Thanks Dare2Dreamer, Clacker and Tedmoore99 users from DSL Forum


original here.