简体   繁体   English

Bash:Sed遇到的问题没有替换字符串,也没有清空整个文件

[英]Bash: Troubles with Sed not replacing string, or emptying entire file

I have a file with an SQL query in it. 我有一个带有SQL查询的文件。 I'm trying to alter the value one of the WHERE clauses in the query using the result of a previous query. 我正在尝试使用上一个查询的结果来更改查询中WHERE子句之一的值。

So basically, I run my first query and store the result in a variable: "LAST". 因此,基本上,我运行我的第一个查询,并将结果存储在变量“ LAST”中。

Then after that, in my bash file, I have this line: 然后,在我的bash文件中,我有这行:

sed -i 's/^AND \`id\` \> ([0-9]+)$/AND `id` > $LAST/g' /query_resources/query.sql

As you can see, I'm attempting to use regex to find the line in my query file that resembles this... 如您所见,我正在尝试使用正则表达式在我的查询文件中找到类似于此行...

AND `id` > 12345

...And replace the string of numbers with whatever I have stored in my variable. ...然后用我存储在变量中的任何数字替换数字字符串。

The problems I'm running into is that either that line is not being changed at all, or I have even had the contents of the file - that I'm doing the search and replace on - completely emptied out. 我遇到的问题是该行根本没有被更改,或者我什至有文件的内容(我正在进行搜索和替换)完全清空了。

您需要更改为双引号,才能将$LAST转换为其实际值,并且还需要转义+和所有`符号:

sed -i "s/^AND \`id\` > [0-9]\+/AND \`id\` > $LAST/g" /query_resources/query.sql

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

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