Solved

how to remove speical characters from a file name

Posted on 2016-08-23
8
60 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Expert Comment

by:Hanno Schröder
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 Schröder
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Suggested Solutions

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

860 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