简体   繁体   English

Bash就地替换文本

[英]Bash in-place replacement of text

I have some insert statements which I want to transform for another environment by replacing values in place. 我有一些插入语句,我想通过替换适当的值来转换为另一个环境。 My sql insert statement looks like below:- 我的sql insert语句如下所示: -

INSERT INTO TABLE (col1, col2, col3, col4, col5) VALUES ('1', '/var/tmp', 'xyz, mno, pqr', '123', '<dummy value>');

I want to convert it such that there are no spaces left between column and values between () which would look like this:- 我想转换它,以便列之间没有空格和()之间的值,这看起来像这样: -

INSERT INTO TABLE (col1,col2,col3,col4,col5) VALUES ('1','/var/tmp','xyz,mno,pqr','123','<dummy value>');

What I have tried so far is something like this:- 到目前为止我尝试的是这样的: -

echo "INSERT INTO TABLE (col1, col2, col3, col4, col5) VALUES ('1', '/var/tmp', 'xyz, mno, pqr', '123', '<dummy value>')" | awk -F'[()]' '{printf "%s(%s)%s(%s)", $1, $2, $3, gensub(".*\(","",gensub(" ", "", $4))}'

And the resulting output is as below:- 结果输出如下: -

INSERT INTO TABLE (col1, col2, col3, col4, col5) VALUES ('1', '/var/tmp', 'xyz, mno, pqr', '123', '<dummy value>'))

I am stuck here. 我被困在这里。 Any cues would be helpful. 任何提示都会有所帮助。

I think you can have much simpler solution using sed : 我认为使用sed可以有更简单的解决方案:

sed -E 's/, /,/g'

If you're not sure about the comma's position, you can use a regex (see @anubhava comment ): 如果您不确定逗号的位置,可以使用正则表达式(请参阅@anubhava注释 ):

sed -E 's/ *, */,/g'

Example: 例:

$ echo "(1, 2, 3) and (4 ,5 ,6)" | sed -E 's/ *, */,/g'
(1,2,3) and (4,5,6)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM