Solved

how to remove speical characters from a file name

Posted on 2016-08-23
8
63 Views
Last Modified: 2016-08-26
I have file names that may or may not have #, ", ' and spaces in the file name.  (55" test #1.txt).  Would like to remove the chara. and spaces, either with a dash or nothing.

Ran this manually an it worked ok.  mv 55\'\ testfile_newtest_5555.001.txt $(echo 55\'\ testfile_newtest_5555.001.txt | sed -e 's/[^A-Za-z0-9._-]/-/g').   This work ok.  55--testfile_newtest_5555.001.txt

However, in my unix script, I would need to loop through several files.  How should the $file be written?

FILELIST=`ls -1`
                  for file in $FILELIST
            do
                  mv "${file}" $(echo "${file}" | sed -e 's/[^A-Za-z0-9._-]/-/g')
    done


Thank you for help
bje
0
Comment
Question by:bje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 41767960
Hi,

I think we will not be able to manage this with the list of files being stored in a variable.
Trying to handle the mix of desired spaces (between file names) and the other ones (inside file names) will become a pain. For the same reason we cannot use "for".

So we should rather use "while read" with input from a pipe:

    ls -1 | while read file
            do
                  mv  "${file}" $(echo "${file}" | sed -e 's/[^A-Za-z0-9._-]/-/g') 2>/dev/null
            done

Please note that stderr of "mv" should be discarded to avoid tons of messages saying "X and X are the same" when sed doesn't find anything in the file name to be changed.
1
 
LVL 12

Expert Comment

by:tel2
ID: 41767970
Looks reasonable, woolmilkporc.

The only (minor) change I would make is to remove the '-1' switch from 'ls', because it's unnecessary.  As far as I know, it only makes a difference when outputting directly to screen, (or something like that).
For example:
$ ls -1 >filelist
$ wc filelist
 4  5 31 filelist
$ ls >filelist
$ wc filelist
 4  5 31 filelist

tel2
1
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41767977
Right you are, tel2, thanks for the hint. Copy-and-paste laziness!.
0
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 41768333
Just a little idea:

I'd use
for file in `ls` ; do
 mv  "${file}" `echo \"${file}\" | sed -e 's/[^A-Za-z0-9._-]/-/g' 2>/dev/null`
done

Open in new window

instead ;-)
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 41768415
@Hanno,

Did you notice the spaces in the file names?
Seems you didn't test your solution taking those spaces into account,
because it wouldn't have worked then.
See my first comment for kind of an explanation.

wmp
0
 
LVL 16

Expert Comment

by:Hanno P.S.
ID: 41768540
Hi wmp,
I did -- that's why I included the " -- but nevertheless, I cannot test drive at the moment as I don't have a Unix box at hand this very hour.
0
 
LVL 12

Expert Comment

by:tel2
ID: 41771051
Hi Hanno,

Including the " where you did will not help.
If, for example, you have a file called:
    my mp3
then $file will contain "my" in 1 iteration of the loop and "mp3" in the next iteration.
0
 

Author Closing Comment

by:bje
ID: 41771990
thank you this is working great.
0

Featured Post

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I hope you'll find this tutorial useful and interesting. So let's try to extend Tcl with a new package.  For anyone more deeply interested please check out the book "Practical Programming in Tcl and Tk". It's really one of the best written books abo…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question