Stripping whitespace in bash script
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)
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> -- ----- stephan@s11n.net http://s11n.net "...pleasure is a grace and is not obedient to the commands of the will." -- Alan W. Watts
Sat, 12 Aug 2006, by dylan@dylan.me.uk:
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: muadib@jabber.xs4all.nl Kernel 2.6.8 + See headers for PGP/GPG info. Claimer: any email I receive will become my property. Disclaimers do not apply.
Sat, 12 Aug 2006, by twe-suse.e@ferrets4me.xs4all.nl:
Sat, 12 Aug 2006, by dylan@dylan.me.uk:
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: muadib@jabber.xs4all.nl Kernel 2.6.8 + See headers for PGP/GPG info. Claimer: any email I receive will become my property. Disclaimers do not apply.
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
Fri, 11 Aug 2006, by rschulz@sonic.net:
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: muadib@jabber.xs4all.nl Kernel 2.6.8 + See headers for PGP/GPG info. Claimer: any email I receive will become my property. Disclaimers do not apply.
Theo, Dylan, On Saturday 12 August 2006 02:08, Theo v. Werkhoven wrote:
Fri, 11 Aug 2006, by rschulz@sonic.net:
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
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 ========================================================================
participants (5)
-
Dylan
-
Hylton Conacher(ZR1HPC)
-
Randall R Schulz
-
stephan beal
-
Theo v. Werkhoven