I have a bunch of files like this:
pic100.png
pig102.png
box103a.png
superb103b.png
px103c.png
rotor110 - new.png
ready1323 (yellow car).png
motorhome1036x red circle.png
...
so, you can notice that files may have 3 parts:
- a prefix that can be any string
- a number that may contain a suffix like "a", "b", etc.
- an optional ending, that is always a string and starts with a space, like " (yellow car)", " red circle", etc.
What I need is this: I want in one operation to:
- change the prefix to the one I want
- keep the number and the suffix (a, b, c...) if there's one
- get rid of the ending
using the first example, I may want to transform that in
object100.png
object102.png
object103a.png
object103b.png
object103c.png
object110.png
object1323.png
object1036x.png
how do I do that? As you see the only thing I am keeping is the number and the suffix "a, b, c" when there is one...
To make it simple, the command must operate in all files in a given directory.
Thanks in advance.
new="$( echo "$file" | sed 's/[^0-9]*\([^ ]*\)[^.]*\(\..*\)*/object\1\2/' )"
.... The basic issue is that$var
can easily cause problems with embedded whitespace. Using double-quotes solves this problem:echo "$var"
orvar="a b"
orvar="$(cat file)"
– Peter.O Apr 07 '12 at 05:14