You are not logged in.

#1 2009-06-05 13:22:59

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Help with a little script - tabular output in bash?

hi, i have a little script that writes my played songs to some txt file on my webserver...

#!/bin/bash
path="$HOME/Documents/_html/history"
delimiter="-"                           
polltime=1                              
previous=$RANDOM

while true
do
        OF=Playlist-$(date +%Y%m%d)     
        song=`mpc --format "%artist%            %title%                 (%album% (%date%))" |head -n 1`
        sleep $polltime
        if [ "$song" != "$previous" ]; then
                if [ -n "$song" ]; then
                        echo $(date +%H:%M:%S) $delimiter "$song" | sed -s '/repeat: off/d' >> $path/$OF
                        previous="$song"
                fi
        fi
done

This works, but the output has some flaws:

15:15:44 - Gumball          With a Little Rain         (Revolution on Ice (1994))
15:16:11 - Stiltskin          Rest In Peace         (The Mind's Eye (1994))
15:19:05 - Yok          Sound 3         (Liese Lotte Meyer Lebt (Back to the Rotz) (2006))
15:19:33 - Calexico          Close Behind         (Feast Of Wire (2003))
15:22:25 - Echobrain          Anjali         (Strange Enjoyment (2002))

Any idea how i can "tableize" this?


*edit* ok, the [Code] tab messes the ouput up even more... but you get the idea..

Last edited by Rasi (2009-06-05 13:25:46)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#2 2009-06-05 14:23:51

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Help with a little script - tabular output in bash?

Hmm this is interesting. I first thought a direct song=`printf FORMAT $(mpc artist album date)` wouldn't work, so you would need:

song=`echo $(eval $(mpc --format 'printf "%-20.20s %-20.20s %-20.20s" "%artist%" "%title%" "(%album% (%date%))"' | head -n 1))`

But this looks really bad, even \n in the printf format didn't work because of eval.

The reason song=`printf FORMAT $(mpc --format "artist" "title" "(album(date))")` won't work is the space delimiter and the ignoring of " inside the mpc format.
So it would read for example printf "%10s %10s %10s" \"水樹奈々\" \"Take a shot\" \"(ALIVE&KICKING ())\" as being 6 arguments. In this case you would have to use printf "%10s %10s %10s" "$(mpc ARTIST)" "$(mpc TITLE)" etc

But the solution is IFS, just set it to something unique:

IFS="@"
song=`printf "%-20.20s %-20.20s %-20.20s\n" $(mpc --format '%artist%@%title%@(%album% (%date%))' | head -n 1)`
echo "$song"

Last edited by Procyon (2009-06-05 14:25:25)

Offline

#3 2009-06-05 15:24:02

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: Help with a little script - tabular output in bash?

Hi!

What is that file for? If you want to look at it using a browser, I'd generate htm instead of text, this way it'd be easy
pseydohtm (me little rusty here wink)

echo "<table>" > bla 
[...]
while [...]
echo "<tr><td>%stuff</td><td>%morestuff</td>[...]</tr>" >> bla
[...]

or if you really want a text file, you can p.e....
1) use \t instead of spaces, that will already help a bit
2) do a "minimum column size" by using sed something like
sed 's/[^\t\n]1[:alnum:\\]1-5)\ *\t/\1 \t\t/g'
( which is also pseydo, because my regex is bad too. Maybe you get the idea:
"if the alphanumeric string that's between [beginning|newline|tab] and [tab|spaces+tab] is only 1-5 chars long, insert a second tab" )
edit: if you know how many spaces width a "full tab" is and want to make it exact, you can do so of course too by making the amount of tabs relational to the "shortness" of the string...
sed 's/[^\t\n]1[:alnum:\\]1-5)\ *\t/\1 \t\t/g'
sed 's/[^\t\n]1[:alnum:\\]6-10)\ *\t/\1 \t\t\t/g'
etc...

There might be better solutions though.

Last edited by whoops (2009-06-05 15:31:05)

Offline

#4 2009-06-05 16:24:18

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: Help with a little script - tabular output in bash?

Procyon, seems you've already solved it with IFS but i'd just like to note that awk would also work for this.  (can you tell awk's my new favorite toy?)

mpc --format '%artist%@%title%@(%album% (%date%))' | head -n 1 | awk -F '@' '{printf "%-20.20s %-20.20s %-20.20s\n", $1, $2, $3}'

Offline

#5 2009-06-05 21:58:56

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: Help with a little script - tabular output in bash?

whoops wrote:

Hi!

What is that file for? If you want to look at it using a browser, I'd generate htm instead of text, this way it'd be easy
pseydohtm (me little rusty here wink)

echo "<table>" > bla 
[...]
while [...]
echo "<tr><td>%stuff</td><td>%morestuff</td>[...]</tr>" >> bla
[...]

or if you really want a text file, you can p.e....
1) use \t instead of spaces, that will already help a bit
2) do a "minimum column size" by using sed something like
sed 's/[^\t\n]1[:alnum:\\]1-5)\ *\t/\1 \t\t/g'
( which is also pseydo, because my regex is bad too. Maybe you get the idea:
"if the alphanumeric string that's between [beginning|newline|tab] and [tab|spaces+tab] is only 1-5 chars long, insert a second tab" )
edit: if you know how many spaces width a "full tab" is and want to make it exact, you can do so of course too by making the amount of tabs relational to the "shortness" of the string...
sed 's/[^\t\n]1[:alnum:\\]1-5)\ *\t/\1 \t\t/g'
sed 's/[^\t\n]1[:alnum:\\]6-10)\ *\t/\1 \t\t\t/g'
etc...

There might be better solutions though.

Interesting... the html is a nice idea...

i have come this far:

#!/bin/bash
path="$HOME/Documents/_html/history"    #path to write out files
delimiter="-"                #used to seperate time and track info - other suggestions: ‣ ✠ ⠯ ♥
polltime=1                #how often to poll in seconds
previous=$RANDOM
OF=Playlist-$(date +%Y%m%d).html     #filename format, placed here so it changes when date changes

echo "<table>" > $path/$OF
while true
do
#    OF=Playlist-$(date +%Y%m%d)    #filename format, placed here so it changes when date changes
        song=`mpc --format "%artist% - %title%"|head -n 1`
    artist=`mpc --format "%artist%"|head -n 1`
        album=`mpc --format "%album%"|head -n 1`
        track=`mpc --format "%title%"|head -n 1`
        year=`mpc --format "%date%"|head -n 1`
    sleep $polltime
    if [ "$song" != "$previous" ]; then
        if [ -n "$song" ]; then
            echo "<tr><td>$(date +%H:%M:%S)</td><td>$artist</td><td>$track</td><td>$album ($year)</td></tr>" >> $path/$OF
            previous="$song"
        fi
    fi
done

Now i just need to find out how to actually draw table borders..


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#6 2009-06-05 22:04:32

gnud
Member
Registered: 2005-11-27
Posts: 182

Re: Help with a little script - tabular output in bash?

Style the table with a CSS file.

Offline

#7 2009-06-05 22:08:47

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: Help with a little script - tabular output in bash?

ok.. got it sorted: here is my final script:

update: now checks for existing file and resumes, if present

#!/bin/bash
path="$HOME/Documents/_html/history"    #path to write out files
delimiter="-"                #used to seperate time and track info - other suggestions: ‣ ✠ ⠯ ♥
polltime=1                #how often to poll in seconds
previous=$RANDOM
OF=Playlist-$(date +%Y%m%d).html     #filename format, placed here so it changes when date changes

if [ -e "$path/$OF" ];then
    echo "file exists... resuming..."
else
cat ~/bin/style > $path/$OF
echo "<table class="center">" >> $path/$OF
fi
while true
do
#    OF=Playlist-$(date +%Y%m%d)    #filename format, placed here so it changes when date changes
        song=`mpc --format "%artist% - %title%"|head -n 1`
    artist=`mpc --format "%artist%"|head -n 1`
        album=`mpc --format "%album%"|head -n 1`
        track=`mpc --format "%title%"|head -n 1`
        year=`mpc --format "%date%"|head -n 1`
    sleep $polltime
    if [ "$song" != "$previous" ]; then
        if [ -n "$song" ]; then
            echo "<tr><td>$(date +%H:%M:%S)</td><td>$artist</td><td>$track</td><td>$album ($year)</td></tr>" >> $path/$OF
            previous="$song"
        fi
    fi
done

Last edited by Rasi (2009-06-06 09:21:35)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

Board footer

Powered by FluxBB