Solved

how to remove speical characters from a file name

Posted on 2016-08-23
8
59 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
3 Use Cases for Connected Systems

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

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

832 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