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 :slight_smile:

Hi

Run this command inside ritu/direct directory:

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

Guru.

Thanks a lot guruprasadpr, for the swift reply.

But I forgot to mention 1 thing :frowning: - 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

Hi

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

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

:frowning: 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?

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:

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.

1 Like

Oops - sorry - i missed the last / there & so it was not working.
Now its working fine :slight_smile:

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,

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 :frowning:
Could you pls help on this

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

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

Thanks Guru :slight_smile: