[Gllug] Stripping whitespace in bash script

Alex Sayle alexs at alphacomplex.info
Fri Aug 11 23:41:54 UTC 2006


On 12 Aug 2006, at 00:23BDT, Dylan wrote:

> Hi All,
>
> I'm working on a script which extracts the tags from ogg and mp3  
> files and
> distributes the files in a directory structure according to the tag  
> contents.
>
> I've found that some files have tags with excess whitespace at the  
> end. What
> methods are there for removing this whitespace in a bash script?

sed is your friend.

quck example, the following prints a the string word with a bunch of
white spaces on the end and then uses sed to put brackets around the
whole string.

$echo "word   " | sed -e 's/\(.*\)/[\1]/'
[word   ]

main example, which uses sed to remove all (zero or more) spaces
at the end of the line and the uses the example above to put
brackets around the string.

$echo "word   " | sed -e 's/ *$//g' | sed -e 's/\(.*\)/[\1]/'
[word]

details in man pages sed(1), and there's always funny looking awk(1)
and perl ( perldoc -f chomp ) that do the job just fine.

"the nice thing about standards is there are so many to choose from"

HTH

--alex

> Thanks
>
> Dylan
> -- 
> "The man who strikes first admits that his ideas have given out."
>                                                 (Chinese Proverb)
> -- 
> Gllug mailing list  -  Gllug at gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug
>

--
[----------------------------------------------------------------------]
  Alex Sayle                                     alexs at alphacomplex.info


-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list