How to delete file that has invalid name?

How do you write a shell script to read a text file delete the special symbols in it and save the file with a new file name?

  • Answer:

    You have mentioned "Shell Script", I assume you are working on Linux/Unix/Mac OS X environment. I would suggest to use small utility which by default should be included in almost all above mentioned OS families. It is called sed (stream editor). Here is how to use the utility: sed -e 's/#//g' source_file.txt > destination_file.txt What it does it changes all '#' to '' (removes it). 's///g': s - substitute - standard regular expression - symbol that will be as replacement. g - global Here is a Bash Script: #!bin/bash # Checking parameters number, we need two if [ $# -ne 2 ] then echo "Wrong number of parameters, please try again." exit 1 fi # Checking to see if file actuall exists and it is readbale if [ -e $1 -a -s $1 ] then # Calling sed program to find all '#' character and replace them with '' (empty), # redirecting stdout (standard output stream) to file (second argument) sed -e 's/#//g' $1 > $2 echo "Job done, please check " $2 " file." else echo "Missing file, please try again." exit 2 fi exit 0 First parameter of script should be input file and second one - output file. Source File: Word # Word Word # # Word Destination File: Word Word Word Word To change more than one character you could use this regular expression: s/[Word]/A/g Same source file and destination would look like: AAAA # AAAA AAAA # # AAAA [All possible chars]; [Word] = { 'W' OR 'o' OR 'r' OR 'd' } There are utilities like tr, that could be used for the same purpose.

wiki.answers.com Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.