Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting one pattern from a file

Status
Not open for further replies.

rawatds

Programmer
Jun 3, 2004
30
SG
Hi all
i want a unix script which will accept a string from user and then find that pattern in all files present in that
path and if pattern finds then remove that pattern the file.

e.g Let us suppose the lines in the files are
i am going to US
i am going to UK
UK is beautiful

so if my pattern is UK
so the new file should be :
i am going to US
i am going to UK
is beautiful

Thanx in advance
rawat
 
man find
man sed

Why is 'i am going to UK' unchanged ?

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi
Can I use following code :
grep $string filename
and then can use sed 's/$string//g' . Please advice

rawat
 
Hi all
i wrote this script :

echo " enter the string to delete "
read pattern
for i in `ls`
do
grep $pattern $i
if [ $? -eq 0 ]
then
sed 's/$pattern//g' $i > $i.1
fi

But it didnot work
till sed the loop is going but i donot know if we can
pass $pattern in sed .
any help ???

regards
rawat
 
you may try this:
echo " enter the string to delete "
read pattern
for i in `ls`
do
grep -q "$pattern" $i && sed "s/$pattern//g" $i > $i.1
done

In your shell man page take a look at the quoting mechanism.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top