Stripping whitespace in bash script

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Stripping whitespace in bash script

Dylan-13
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?

Thanks

Dylan
--
"The man who strikes first admits that his ideas have given out."
                                                (Chinese Proverb)

--
Check the headers for your unsubscription address
For additional commands send e-mail to [hidden email]
Also check the archives at http://lists.suse.com
Please read the FAQs: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace in bash script

stephan beal
On Saturday 12 August 2006 01:24, Dylan wrote:
> 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?

You can try sed or perl, for example:


stephan@owl:~/cvs/s11n.net/SpiderApe> echo "with trailing spaces   " |
perl -pe 's|\s*$||'
with trailing spacesstephan@owl:~/cvs/s11n.net/SpiderApe>  



--
----- [hidden email]   http://s11n.net
"...pleasure is a grace and is not obedient to the commands
of the will." -- Alan W. Watts

attachment0 (196 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace in bash script

Theo v. Werkhoven
In reply to this post by Dylan-13
Sat, 12 Aug 2006, by [hidden email]:

> 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?

No need for a script, just a simpel use of Bash built-in
capabilities.

$ file="test file 1"
$ echo ${file// /}
testfile1

Zie bash(1); Parameter Expansion

Theo
--
Theo v. Werkhoven    Registered Linux user# 99872 http://counter.li.org
ICBM 52 13 26N , 4 29 47E.     +      ICQ: 277217131
SUSE 9.2                       +   Jabber: [hidden email]
Kernel 2.6.8                   +   See headers for PGP/GPG info.
Claimer: any email I receive will become my property. Disclaimers do not apply.

--
Check the headers for your unsubscription address
For additional commands send e-mail to [hidden email]
Also check the archives at http://lists.suse.com
Please read the FAQs: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace in bash script

Theo v. Werkhoven
Sat, 12 Aug 2006, by [hidden email]:

> Sat, 12 Aug 2006, by [hidden email]:
>
> > 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?
>
> No need for a script, just a simpel use of Bash built-in
> capabilities.
>
> $ file="test file 1"
> $ echo ${file// /}
> testfile1
>
> Zie bash(1); Parameter Expansion

Forgot to mention: if you only want the trailing spaces removed, use
this form: ${file//% /}

Theo
--
Theo v. Werkhoven    Registered Linux user# 99872 http://counter.li.org
ICBM 52 13 26N , 4 29 47E.     +      ICQ: 277217131
SUSE 9.2                       +   Jabber: [hidden email]
Kernel 2.6.8                   +   See headers for PGP/GPG info.
Claimer: any email I receive will become my property. Disclaimers do not apply.

--
Check the headers for your unsubscription address
For additional commands send e-mail to [hidden email]
Also check the archives at http://lists.suse.com
Please read the FAQs: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace in bash script

Randall Schulz
Theo, Dylan,

On Friday 11 August 2006 17:34, Theo v. Werkhoven wrote:
> ...
>
> Forgot to mention: if you only want the trailing spaces removed, use
> this form: ${file//% /}

I think you mixed up the arbitrary substitution syntax with the trailing
stripping syntax. It should be:

        "${file%% }"

And for the leading ones:

        "${file## }"

The "EXPANSION" secion of the BASH manual page details these and other
similar expansion substitutions available in BASH.


> Theo


Randall Schulz

--
Check the headers for your unsubscription address
For additional commands send e-mail to [hidden email]
Also check the archives at http://lists.suse.com
Please read the FAQs: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace in bash script

Theo v. Werkhoven
Fri, 11 Aug 2006, by [hidden email]:

> Theo, Dylan,
>
> On Friday 11 August 2006 17:34, Theo v. Werkhoven wrote:
> > ...
> >
> > Forgot to mention: if you only want the trailing spaces removed, use
> > this form: ${file//% /}
>
> I think you mixed up the arbitrary substitution syntax with the trailing
> stripping syntax. It should be:
>
> "${file%% }"

You're right, but it wasn't immediately clear to me (late night,
yada yada yada) if Dylan wanted all spaces removed or only the
trailing ones, so I chose the arbitrary syntax.
Tnx.

Theo
--
Theo v. Werkhoven    Registered Linux user# 99872 http://counter.li.org
ICBM 52 13 26N , 4 29 47E.     +      ICQ: 277217131
SUSE 9.2                       +   Jabber: [hidden email]
Kernel 2.6.8                   +   See headers for PGP/GPG info.
Claimer: any email I receive will become my property. Disclaimers do not apply.

--
Check the headers for your unsubscription address
For additional commands send e-mail to [hidden email]
Also check the archives at http://lists.suse.com
Please read the FAQs: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace in bash script

Randall Schulz
Theo, Dylan,

On Saturday 12 August 2006 02:08, Theo v. Werkhoven wrote:

> Fri, 11 Aug 2006, by [hidden email]:
> > Theo, Dylan,
> >
> > On Friday 11 August 2006 17:34, Theo v. Werkhoven wrote:
> > > ...
> > >
> > > Forgot to mention: if you only want the trailing spaces removed,
> > > use this form: ${file//% /}
> >
> > I think you mixed up the arbitrary substitution syntax with the
> > trailing stripping syntax. It should be:
> >
> > "${file%% }"
>
> You're right, but it wasn't immediately clear to me (late night,
> yada yada yada) if Dylan wanted all spaces removed or only the
> trailing ones, so I chose the arbitrary syntax.
> Tnx.

Actually, I'm not right.

The problem is that the shortest (single # or %) vs. longest (double #
or %) are only distinguished when there's a * involved, and since these
are not real regular expressions, but rather shell glob patterns, it's
not possible to use any of the variable substitution expansions to
achieve this affect.

I believe it actually is necessary to use sed, perl or awk. Sed's my
tool of choice for these things, so here's how it would work:

% title="   One, two, buckle my shoe     "

% strippedTitle="$(echo -n "$title" |sed -r -e 's/^ +//' -e 's/ +$//')"
% echo "[[$strippedTitle]]"
[[One, two, buckle my shoe]]


> Theo


Sorry for the erroneous information.


Randall Schulz

--
Check the headers for your unsubscription address
For additional commands send e-mail to [hidden email]
Also check the archives at http://lists.suse.com
Please read the FAQs: [hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: Stripping whitespace in bash script

Hylton Conacher (ZR1HPC)
In reply to this post by Dylan-13
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?
I the scripts or work arounds don't do what you need I'd suggest
visiting Unix Guru Universe (http://www.ugu.com). Browse the site as I
receive a daily tip from them and you might be able to source one that
will help.

HiH
--
========================================================================
Currently using SuSE 9.2 Professional with KDE and Mozilla 1.7.2
Linux user # 229959 at http://counter.li.org
========================================================================


--
Check the headers for your unsubscription address
For additional commands send e-mail to [hidden email]
Also check the archives at http://lists.suse.com
Please read the FAQs: [hidden email]