Script for removing characters in files

Dear Experts,

1.I need a script which will search a directory for files which contains " inside files (not in filenames) and remove this character(")from that file.

2. IT will also log everything to a log file with execution time and date.

Please help me.

Thanks,
Mohona.
 
mohonaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

edster9999Commented:
The command is :

for i in `grep -l "\"" *`; do echo "updated - $i"; mv $i $i.bak ; cat $i.bak | tr -d "\"" > $i ; done

This will look for double quote in all files. (Change to *.txt if you want to narrow it down)

All files it finds are then moved to filename.bak and then stripped of the double quote and saved with the original name.

or as a script file :


#!/bin/bash
 
logfile="/var/logs/strip.log"
 
echo "Strip executed `date`" >> $logfile
 
for i in `grep -l "\"" *`; do 
    echo "updated - $i" >> $logfile
    mv $i $i.bak  
    cat $i.bak | tr -d "\"" > $i 
done

Open in new window

0
mohonaAuthor Commented:
but how do I mention the directory, or shall i run it from the directory where I need to search for files?

0
edster9999Commented:
This would work on the current directory you were in so you could put it somewhere on the path like
/usr/bin/
and then as you move round you could run it.

Or if you want to specify it you could do something like :


#!/bin/bash
 
logfile="/var/logs/strip.log"
 
echo "Strip executed `date`" >> $logfile
 
if [ $# -ne 1 ]; then
        echo "Please put the directory name after the command"
        echo "Stopped due to wrong parameters" >> $logfile
else
    for i in `grep -l "\"" $1/*`; do 
        echo "updated - $i" >> $logfile
        mv $i $i.bak  
        cat $i.bak | tr -d "\"" > $i 
    done
fi

Open in new window

0
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.

edster9999Commented:
and you could add in this line
    echo "on this directory - $1" >> $logfile
after the else before the for line
so it logs the directory name too.

Now this doesn't check the syntax of the directory line you give it so if you put something silly on the line you will prob get nothing back
0
mohonaAuthor Commented:
Thank you, almost completed and woking very nice.
One last thing is that, I don't want to pass the directory name as an argument. Please make it permanent like example /data/abc/xyz .  
0
edster9999Commented:

#!/bin/bash
 
# Script to strip files of double quotes in a directory
#
 
logfile="/var/logs/strip.log"
 
targetdir="/data/abc/xyz"
 
echo "Strip executed `date` on directory $targetdir" >> $logfile
 
for i in `grep -l "\"" $targetdir/*`; do 
    echo "updated - $i" >> $logfile
    mv $i $i.bak  
    cat $i.bak | tr -d "\"" > $i 
done

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mohonaAuthor Commented:
Excellent! Thank you.
0
TintinCommented:
Huh?  This is the *exact* same question asked a few days ago (which was answered).  Is this a duplicate or an assignment question?
0
TintinCommented:
Whoops.  I didn't see the link to the related question and didn't read this one carefully enough.  Sorry.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.