0

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

3 Answers 3

1

You could use sed:

sed 's/delete from \(tb[0-9]\?\).\([[:alnum:]]\+\)/delete from \2/g' input.file
Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean with sub string?
what I meant to say is the some string that need to be removed, basically anything after "from" till "." In the above example "tb.", "tb1.". sorry for not asking the question completely.
I don't get, I could just guess. You need to explain more.
0

You can do it using sed:

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

HTH

Comments

0

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

2 Comments

what if I want to remove anything after "delete from" to .(DOT including tb. tb1. or abc.), so the output will be delete from test ..
in that case: sed 's/delete from .*\./delete from /' a.txt (I've updated the answer above accordingly)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.