Discussion:
[9fans] removing spaces from filenames
(too old to reply)
Peter A. Cejchan
2012-03-21 09:26:27 UTC
Permalink
# remove spaces from file names /// does not work
EXT=boo
rm run
for (old in `{9 ls *.$EXT}) {
new=`{echo $old | tr -d ' ' }
echo mv $old $new >> run
}

# this works, but is ugly:
# remove unwanted chars from file names
EXT=boo
9 ls *.$EXT > foo
9 cat foo | 9 tr -c [0-9][a-z][A-Z]'.''\x0a' '_' | 9 tr -s '_' '_' |
9 sed 's/_\././g' > foo2
paste foo foo2 > foo3
9 awk '{print "mv ", $0}' foo3 > run

what am I missing?
Thanks,
++pac
yy
2012-03-21 13:20:17 UTC
Permalink
Post by Peter A. Cejchan
for (old in `{9 ls *.$EXT}) {
Here you are iterating over the elements of `{9 ls *.$EXT}, sepparated
by $ifs. Therefore, spaces are breaking your lines into word fields.
This is not what you want.

Your second example directly process whole lines (uses sed), so it
works. I think you will get what you want if you replace this line
with:

ifs='
' for (old in `{9 ls *.$EXT}) {
--
- yiyus || JGL .
erik quanstrom
2012-03-21 16:36:08 UTC
Permalink
Post by yy
ifs='
' for (old in `{9 ls *.$EXT}) {
i think ls and the temporary file are getting in the
way. if you use globbing and execing directly from
the shell you can avoid quoting problems. for example,

for(old in *^' '^*.$EXT)
ifs=$nl mv $old `{echo $old | sed 's/ //g'}

and now for a digression ....

this looks like c programming to me. unfortunately
mv doesn't take pairs, so we can't get rid of the for
loop, but we can generate parallel lists.

i'm going to assume that ☺ isn't in any of your file names.
you could pick something else.

the trick here is to paste ☺ onto the end of every list
element. that way we know that '☺ ' are end-of-token
markers, and other spaces are just spaces. then we
can delete the spaces without ☺s, then delete the ☺s
and then turn the remaining spaces (they mark the
end of a token) into newlines.

i'm going to assume that $ext also contains the '.'.

old = *^' '^*$ext
ifs=$nl new = `{echo $old^☺ | sed 's/([^☺]) /\1/g
s/☺//g
s/ /\n/g' }

(the last two sed steps are combinable; s/☺ /\n/g.)

now that we have this, we can

while(! ~ $#new 0){
mv $old(1) $new(1)
old=$old(2-)
new=$new(2-)
}

this reminds me, the whole ifs thing is awkward because
it applies for the whole command which is almost never
what you want. i modified rc to allow one to specify a
backquote splitter. e.g.

new = `$nl {echo $old^☺ | sed 's/([^☺]) /\1/g
s/☺//g
s/ /\n/g' }

- erik
Peter A. Cejchan
2012-03-22 07:05:59 UTC
Permalink
thank you very much , Eric!
I don't use strange chars in my filenames, but others do, sigh,

++pac

Continue reading on narkive:
Loading...