如何在字符串中查找和删除多个子字符串

时间:2021-05-13 19:34:27

I have an input as follows

我有如下输入

Input File Name : a.txt

输入文件名:a.txt

-- Some Comment

- 一些评论

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

从tb.Test删除WHERE id ='abxd1';

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

从tb1.Test中删除WHERE id ='abxd2';

-- Some Comment

- 一些评论

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

从tb1.Table1删除WHERE id ='abxd3';

Expected output file : b.txt

预期的输出文件:b.txt

-- Some Comment

- 一些评论

delete from Test WHERE id = 'abxd1';

从Test WHERE id ='abxd1'中删除;

delete from Test WHERE id = 'abxd2';

从Test WHERE id ='abxd2'中删除;

-- Some Comment

- 一些评论

delete from Table1 WHERE id = 'abxd2';

从Table1删除WHERE id ='abxd2';

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

以下代码将仅替换值“tb。”。我试图将其作为通用脚本。

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

Thanks for you help

谢谢你的帮助

3 个解决方案

#1


1  

You could use sed:

你可以使用sed:

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

#2


0  

You can do it using sed:

你可以用sed做到:

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

HTH

HTH

#3


0  

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

假设您要从X.Y中删除X.(DOT和DOT之前的所有内容),它甚至更简单:

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

sed's / delete from。* \ ./从/'a.txt删除

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

并且您可以使用-i选项覆盖同一文件

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

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

HTH, Marcello

HTH,Marcello

#1


1  

You could use sed:

你可以使用sed:

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

#2


0  

You can do it using sed:

你可以用sed做到:

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

HTH

HTH

#3


0  

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

假设您要从X.Y中删除X.(DOT和DOT之前的所有内容),它甚至更简单:

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

sed's / delete from。* \ ./从/'a.txt删除

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

并且您可以使用-i选项覆盖同一文件

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

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

HTH, Marcello

HTH,Marcello