Solved

how to remove speical characters from a file name

Posted on 2016-08-23
8
53 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 11

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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 11

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 to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now