Ullan
Ullan

Reputation: 1341

How to find and remove multiple sub strings in a string

I have an input as follows

Input File Name : a.txt

-- Some Comment

delete from tb.Test WHERE id = 'abxd1';

delete from tb1.Test WHERE id = 'abxd2';

-- Some Comment

delete from tb1.Table1 WHERE id = 'abxd3';

Expected output file : b.txt

-- Some Comment

delete from Test WHERE id = 'abxd1';

delete from Test WHERE id = 'abxd2';

-- Some Comment

delete from Table1 WHERE id = 'abxd2';

The following code will just replace the value "tb.". I am trying to make this as a generic script.

while read line
do
   str=$line
   echo "${str/tb./}" >>b.txt
done <$1

Thanks for you help

Upvotes: 0

Views: 187

Answers (3)

marcello
marcello

Reputation: 146

Assuming that you want to remove X. in delete from X.Y (everything before the DOT and the DOT), it is even simpler:

sed 's/delete from .*\./delete from /' a.txt

and you can use the -i option to overwrite the same file

sed -i 's/delete from .*\./delete from /' a.txt

HTH, Marcello

Upvotes: 0

Harsh Gupta
Harsh Gupta

Reputation: 4538

You can do it using sed:

echo $str | sed -r 's/tb[a-z0-9_]*.//g'

HTH

Upvotes: 0

hek2mgl
hek2mgl

Reputation: 157947

You could use sed:

sed 's/delete from \(tb[0-9]\?\).\([[:alnum:]]\+\)/delete from \2/g' input.file

Upvotes: 1

Related Questions