Script to search for a character in files in a Directory & remove it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to search for a character in files in a Directory & remove it
# 1  
Old 05-28-2012
Java Script to search for a character in files in a Directory & remove it

Hi All,

Am new to both Unix & this Forum - Need some help on a script that I am trying to write:

In a Directory i have few text files which might or might not contain some text that I am trying to find.
Once that text is found in any of the files, it needs to be removed from the file


Example ::
Lets say I am in Directory : "ritu/direct"
Inside "ritu/Direct" directory, I have "a.txt" & "b.txt"
The character that I am trying to find in these files is "#"

So, I need the script to be able to search all the files in the directory & look for a "#" & remove the "#" from the line in the file it was found in

Could anyone please help me on this

Thanks in Advance Smilie
# 2  
Old 05-28-2012
Hi

Run this command inside ritu/direct directory:

Code:
sed -i 's/^#//' *.txt

Guru.
# 3  
Old 05-28-2012
Thanks a lot guruprasadpr, for the swift reply.

But I forgot to mention 1 thing Smilie - Sorry for the inconvenience
I need the # to be removed from lines which matches another string.

Lets says:
a.txt contains :
ritu|abc
#unix|def
unix|ghi

& b.txt contains :
#ritu|123
unix|567

the script needs to take an input for the main string - which will be "ritu" or "unix" in this case

So if the user enters "unix", the script should find all lines that contain "unix" in both the files & remove the "#" from it

So a.txt should now be :
ritu|abc
unix|def
unix|ghi

& b.txt should now be :
#ritu|123
unix|567

Could you please help on this
# 4  
Old 05-28-2012
Hi


Code:
echo Enter the string:
read str
sed -i "s/^#$str/$str/" *.txt

This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 05-28-2012
while running this : sed -i "s/^#$str/$str/" *.txt
m getting this error : sed: illegal option -- i

& if i try : sed "s/^#$str/$str/" *.txt
i get : sed: command garbled: s/^#ritu/ritu

& if i try : sed "s/\#$collname/$collname" *.txt
i get : sed: command garbled: s/\#ritu/ritu

Smilie i tried the chekcing the man files for sed - it is not listing any option as "i" - it only has "-f", "-e" & "-d"
I am executing in the bash shell - Does that make any difference - Or is there anything else I can change?
# 6  
Old 05-28-2012
Hi
Since your sed does not support the -i option, we need to copy the sed output to a temporary file and then move the file contents to the original file:


Code:
echo Enter the string:
read str
TEMP=temp_`date '+%Y%m%d'`
for file in *.txt
do
    sed "s/^#$str/$str/" $file > $TEMP
    mv $TEMP $file
done

Guru.
This User Gave Thanks to guruprasadpr For This Post:
# 7  
Old 05-28-2012
Oops - sorry - i missed the last / there & so it was not working.
Now its working fine Smilie

But there is 1 more issue - only the output displayed on running the script have the required changes.
However, the changes are not reflecting in the original file - which is what I require.

Could someone please help on this - on how to make the original file change directly?

Please ignore this post - i guess it was being answerewd while I was posting it ---
Thanks Guru - let me try that out

---------- Post updated at 05:30 PM ---------- Previous update was at 05:11 PM ----------

Hi,

Code:
echo "enter the coll name"
read collname
TEMP=temp_`date '+%Y%m%d'`
for file in *.txt
do
        sed "s/^#$collname/$collname/" $file > $TEMP
        mv $TEMP $file
done

once i run this script, the whole input file goes(a.txt & b.txt) goes empty for some reason Smilie
Could you pls help on this

---------- Post updated at 06:40 PM ---------- Previous update was at 05:30 PM ----------

Its Working now !!! Smilie
the 1st time i run it - all the .txt files in my Directory went blank...
But since after that its been working perfectly Smilie

Thanks Guru Smilie

Last edited by Scrutinizer; 05-28-2012 at 09:30 AM.. Reason: changes table tags to code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Compressing & removing files in a directory & subdirectory

Hi, I want a simple line of code that will compress files within a directory specified (parameter) and its subdirectories and also i want to remove files which are exactly 365 days old from the sysdate after this compression. Please help. Thanks, JD (8 Replies)
Discussion started by: Jesshelle David
8 Replies

2. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

3. UNIX for Dummies Questions & Answers

How to search for a file having a particular character in a particular place in a directory.?

Hi Guys, I want to search for a specific file in a directory which have a "b" letter as the 3rd character in the name of the file. For Example : /abc/efg/ldbjfblkj.sh /abc/efg/erublkd.sh /abc/efg/eibueora.sh /abc/efg/kfvnmnb.sh Since we have 2 files with "b" as a 3rd character in... (5 Replies)
Discussion started by: Pramod_009
5 Replies

4. UNIX for Dummies Questions & Answers

Script to remove zip files from a directory

Hi Folks, There is a job which generates a .zip files every day at /usr/app/generated directory , now please advise for the script that will delete this zip files permanently.but while deleting it should make sure that it will not delete the last two days recently generated zip files and this... (1 Reply)
Discussion started by: punpun66
1 Replies

5. Programming

Script for creating a directory & move the .tif files in it.

Hi Team, I have thousands of TIF files which are converted from PDF. Below is a sample of it. LH9406_BLANCARAMOS_2012041812103210320001.tif LH9406_BLANCARAMOS_2012041812103210320002.tif LH9406_BLANCARAMOS_2012041812103210320003.tif LH9411_ANGENIAHUTCHINSON_2012041812102510250001.tif... (9 Replies)
Discussion started by: paragnehete
9 Replies

6. Shell Programming and Scripting

Need script to remove millions of tmp files in /html/cache/ directory

Hello, I just saw that on my vps (centOS) my oscommerce with a seo script has created millions of tmp files inside the /html/cache/ directory. I would need to remove all those files (millions), I tried via shell but the vps loads goes to very high and it hangs, is there some way to do a... (7 Replies)
Discussion started by: andymc1
7 Replies

7. Shell Programming and Scripting

Script That Can navigate to 3 differents directory & remove files under them

Hi I am Trying to Write a script that can goto 4 different directorys on the server & remove the Files older then 30 days ?? /logs logs1 logs2 logs3 Now I need to remove files under logs1 logs2 logs3 which are older then 30 days whose name stat 'sit' , 'mig','bld' . in... (3 Replies)
Discussion started by: Beginner123
3 Replies

8. Shell Programming and Scripting

How do I remove everything after a certain character in text files?

I have a text file with a few thousand lines in the format: abcdef*ghijk*lmno pqrs*tuv wx*y*z etc. What I want to do is, get rid of everything after the SECOND asterik (the *) in each line (including the asterik). So for the example above, it would look like this after the editing: ... (11 Replies)
Discussion started by: guitarscn
11 Replies

9. Shell Programming and Scripting

Script to remove all empty files within the directory structure?

Hi I need to write a shell script which basically searches for all the empty files within the directory structure, lists them before asking the user to confirm if they would like to delete them. If the user deletes the file then a notice would appear confirming the file is deleted. I've be... (5 Replies)
Discussion started by: cat123
5 Replies

10. Shell Programming and Scripting

Remove 5th character from Field1 & Print

Hi , I need to remove the 5th character of column1 and print the rest. Can anybody give some advice? Input: 0001c xx 0001r gg jj 0002y vv 0002p kk 0003q gg ll 0003v tt 0003t gg pp kk Output: 0001 xx 0001 gg jj (9 Replies)
Discussion started by: Raynon
9 Replies
Login or Register to Ask a Question