简体   繁体   English

正则表达式替换(sed或perl)变量捕获

[英]Regex replace (sed or perl) with variable capture

I need to do a regex capture/repalce on a file with content like the following. 我需要对包含以下内容的文件执行正则表达式捕获/重新生成。 While I'm okay at matching things, I'm having trouble doing a capture replace. 虽然我可以匹配东西,但我在捕捉替换方面遇到了麻烦。

I'd like to do the following: 我想做以下事情:

  1. Capture which will be different in every instance. 捕获在每个实例中都会有所不同。
  2. Add a new line before the copy line that references the table name. 在引用表名称的复制行之前添加一个新行。

So, for example, copy mytable( would become hello mytable\\ncopy mytable( . 所以,例如, copy mytable(将成为hello mytable\\ncopy mytable(


Sample Input 样本输入

copy tablename(
        preferredid= 'c0»',
        qid= 'c0»' with null(''),
        qpi= 'c0»',
        ptid= 'c0»' with null(''))...

into '/idata2/backup/core/eq.ingres'

尝试这个 :

sed 's/^copy \([^(]*\)($/hello \1\ncopy \1(/' 

使用perl单线程

perl -i -pe 's/^(?=copy (\w+)\()/hello $1\n/' file

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

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