Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

how to remove speical characters from a file name

Posted on 2016-08-23
8
Medium Priority
?
66 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 2000 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
Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

 
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
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 …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

604 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