英文:
Multiple line search and replace bash sed
问题
我有一个名为tst.txt的文件,其中包含文本:
exec(compile(open(bla bla bla'))
DATE = "05JUN20"
Only capture the below line
params = {'System_Date': getparamDate(DATE), 'Param': '2'}
This Line should be available
''End
'Short description:
'bla bla bli, bla bla bla
First line with }
Normal Line without bracket
Another bracket}&
Then normal text
blabla
blublu
tatati
我想捕获并修改文件中的以下行:
params = {'System_Date': getparamDate(DATE), 'Param': '2'}
我找到了一个sed命令,可以替换模式之间的内容。/c
sed -i '/params = {/,/}/c params = MyNewParam' tst.txt
在我的文件中,我打算只替换以下内容:
params = {
和
}
但不幸的是,我的正则表达式会替换到
First line with }
exec(compile(open(bla bla bla'))
DATE = "05JUN20"
Only capture the below line
params = MyNewParam
Normal Line without bracket
Another bracket}&
Then normal text
blabla
blublu
tatati
I5TaxActivationDate
这不是我期望的结果。
我发现使用
,/}/q
可以在第一次出现时停止,但我找不到正确的语法。
有人能帮忙吗?
提前感谢。
英文:
I have a file called tst.txt that contains text:
exec(compile(open(bla bla bla'))
DATE = "05JUN20"
Only capture the below line
params = {'System_Date': getparamDate(DATE), 'Param': '2'}
This Line should be available
''End
'Short description:
'bla bla bli, bla bla bla
First line with }
Normal Line without bracket
Another bracket}&
Then normal text
blabla
blublu
tatati
I want to capture and change only the line:
params = {'System_Date': getparamDate(DATE), 'Param': '2'}
in my file.
I've fond a sed command that allow to substitute what is between patterns. /c
sed -i '/params = {/,/}/c params = MyNewParam' tst.txt
where I intend to substitute only what is between:
params = {
and
} from the same line.
unfortunately, my regex will replace until First line with }
exec(compile(open(bla bla bla'))
DATE = "05JUN20"
Only capture the below line
params = MyNewParam
Normal Line without bracket
Another bracket}&
Then normal text
blabla
blublu
tatati
I5TaxActivationDate
which is not what I expect.
I've seen that with
,/}/q
it's possible to stop at the first occurrence, I cannot find the right syntax.
Can anyone help please?
Thanks in advance
答案1
得分: 1
使用GNU sed
在括号内,捕获并返回带有回溯引用\1
的params
,直到包括{'
。
排除从}
之前的所有内容,并替换为MyNewParam
。
$ sed -Ez "s/(params[^{]*{')[^}]*/MyNewParam'/ input_file
exec(compile(open(bla bla bla'))
DATE = "05JUN20"
只捕获以下行
params = {'MyNewParam'}
这行应该可用
''End
'简要说明:
'bla bla bli, bla bla bla
第一行带}
正常行没有括号
另一个括号}&
然后正常文本
blabla
blublu
tatati
英文:
Using GNU sed
Capture and return with back-reference \1
params
up to and including {'
within the parenthesis.
Exclude everything up to }
and replace with MyNewParam
$ sed -Ez "s/(params[^{]*\{')[^}]*/MyNewParam'/" input_file
exec(compile(open(bla bla bla'))
DATE = "05JUN20"
Only capture the below line
params = {'MyNewParam'}
This Line should be available
''End
'Short description:
'bla bla bli, bla bla bla
First line with }
Normal Line without bracket
Another bracket}&
Then normal text
blabla
blublu
tatati
答案2
得分: 1
这可能适用于您(GNU sed):
sed -e '/params = {.*}/c CHANGE ONE' -e '/params = {/,/}/c CHANGE MULTIPLE' 文件名
如果一行包含整个模式,则只更改该行,否则尝试将模式适应到多行。
<details>
<summary>英文:</summary>
This might work for you (GNU sed):
sed -e '/params = {.*}/c CHANGE ONE' -e '/params = {/,/}/c CHANGE MULTIPLE' file
If a line contains the whole pattern then only change that line, otherwise try to fit the pattern over multiple lines.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论