user1854236
user1854236

Reputation: 456

sed removing spaces before and after something

I'm wondering how to make mass rename (using rename or sed/awk), for files like this:

Name 1 - Name 2 - Name 3.doc
Name 1- Name 2 - Name 3.doc
Name 1 -Name 2 - Name 3.doc

the problem is that i want to have all files in the same schema, for example

Name 1-Name 2-Name 3.doc

without spaces at all. I'm reading rename documentation but can't find way to do this.

Upvotes: 1

Views: 100

Answers (2)

jaypal singh
jaypal singh

Reputation: 77155

How about:

for file in *.doc; do 
   mv "$file" "$(sed 's# *- *#-#g' <<< "$file")"
done

Upvotes: 2

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185570

Try doing this :

rename 's/\s*-\s*/-/g' *.doc

You need the Perl's rename, see this post

Upvotes: 2

Related Questions