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
Related Q & A:
- How to read a PDF file with PHP?Best solution by Stack Overflow
- how can I write this shell script in python?Best solution by Stack Overflow
- Is there a limit on the size of a new file or a text file?Best solution by Stack Overflow
- I need a new screen name can you help me.Best solution by Yahoo! Answers
- Need a new msn name???? PLEASE?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.