You are not logged in.

#1 2008-03-11 20:39:11

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

How to truncate text in a shell script? [SOLVED]

Here is my shell script...

#!/bin/bash

case "$1" in

artist) dcop amarok player artist ;;
title)  dcop amarok player title ;;

esac

I want it to send the artist and title truncated if they are more than 20 characters and with a little '...' ellipsis at the end. Can anyone help me?

Last edited by tony5429 (2008-03-12 13:28:24)

Offline

#2 2008-03-11 20:48:47

wain
Member
From: France
Registered: 2005-05-01
Posts: 289
Website

Re: How to truncate text in a shell script? [SOLVED]

That should works :

#!/bin/bash

case "$1" in

artist) 
    artistname=`dcop amarok player artist`
    if [ ! -z "${artistname:20}" ]; then
        echo "${artistname::20}..."
    else
        echo $artistname
    fi
    ;;
title)  
    songtitle=`dcop amarok player title`
    if [ ! -z "${songtitle:20}" ]; then
        echo "${songtitle::20}..."
    else
        echo $songtitle
    fi
    ;;
esac

Last edited by wain (2008-03-11 20:55:43)

Offline

#3 2008-03-11 20:52:05

finferflu
Forum Fellow
From: Manchester, UK
Registered: 2007-06-21
Posts: 1,899
Website

Re: How to truncate text in a shell script? [SOLVED]

This might be a starting point, even though I don't know the answer: http://developer.apple.com/documentatio … 03521-SW16 (gosh what a long URL)

edit
wow, I was slow tongue

Last edited by finferflu (2008-03-11 20:52:38)


Have you Syued today?
Free music for free people! | Earthlings

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- A. de Saint-Exupery

Offline

#4 2008-03-12 13:28:11

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: How to truncate text in a shell script? [SOLVED]

Fantastic! Thanks, Wain! That is exactly what I needed smile

Offline

Board footer

Powered by FluxBB