Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Script for removing characters in files

Posted on 2008-11-12
9
Medium Priority
?
326 Views
Last Modified: 2013-12-27
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.
 
0
Comment
Question by:mohona
  • 4
  • 3
  • 2
9 Comments
 
LVL 20

Expert Comment

by:edster9999
ID: 22939998
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
 

Author Comment

by:mohona
ID: 22940446
but how do I mention the directory, or shall i run it from the directory where I need to search for files?

0
 
LVL 20

Expert Comment

by:edster9999
ID: 22940715
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 20

Expert Comment

by:edster9999
ID: 22940738
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
 

Author Comment

by:mohona
ID: 22941147
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
 
LVL 20

Accepted Solution

by:
edster9999 earned 2000 total points
ID: 22941907

#!/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
 

Author Closing Comment

by:mohona
ID: 31515942
Excellent! Thank you.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22943778
Huh?  This is the *exact* same question asked a few days ago (which was answered).  Is this a duplicate or an assignment question?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22943795
Whoops.  I didn't see the link to the related question and didn't read this one carefully enough.  Sorry.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Suggested Courses

577 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