如何用正则表达式使用find、-exec、grep和sed替换部分文本?

时间:2022-04-25 16:49:33

Well, I really wanted to create a filter to use in DeltaWalker to ignore files with only a particular line difference, so I could identify files that had other source code changes. * regex guru's gave me several good expressions that yield good find -exec grep results in OS X Terminal, but have no effect on DeltaWalker results. So, I have another idea.

我真的想要创建一个过滤器来使用DeltaWalker来忽略只有一个特定行差异的文件,这样我就可以识别有其他源代码更改的文件。* regex guru's给出了几个很好的表达式,它们可以在OS X终端中产生良好的find -exec grep结果,但对DeltaWalker结果没有影响。我有另一个想法。

I recall a utility called sed, which I have no experience... but, I believe it could be called with find's -exec switch. So, I've got the following and wonder if anyone knows how to mix-in sed to replace the matching line of text: "Copyright (c) 2008 - 2009" with "Copyright (c) 2008 - 2010"

我记得一个叫做sed的实用程序,我没有经验……但是,我相信可以用find的-exec开关调用它。因此,我想知道是否有人知道如何混合使用sed来替换匹配的文本行:“Copyright (c) 2008 - 2009”和“Copyright (c) 2008 - 2010”

find . -exec grep -Hn '^.*Copyright (c) 2008 - 2009' {} \;

1 个解决方案

#1


13  

You could simply do this

你可以这么做

find . -exec sed -r -e 's/(^.*)Copyright \(c\) 2008 - 2009/\1Copyright (c) 2008 - 2010/g' {} \;

I'm presuming here that you don't want to lose whatever it is that's on the line before Copyright and is matched by .*

我在这里假设你不想失去任何在版权之前的线上并且被匹配的东西。*

But, depending on your input, it might be as simple as

但是,根据你的输入,它可以简单到

find . -exec sed -e 's/2009/2010/g' {} \;

Note that in both cases I am not modifying the files, just showing what the results of modifying the contents would look like. I imagine that in your real scenario you'd want to write the changes back to disk. For this you need the -i switch to sed, thus:

注意,在这两种情况下,我都没有修改文件,只是显示了修改内容的结果。我假设在真实场景中,您希望将更改写回磁盘。为此你需要-我切换到sed,因此:

find . -exec sed -r -i'' -e 's/(^.*)Copyright \(c\) 2008 - 2009/\1Copyright (c) 2008 - 2010/g' {} \;

It's worth noting that here I am using GNU sed syntax. I don't have an OS X box handy to test with but BSD sed generally uses -E instead of -r to turn on extended regex.

值得注意的是,我在这里使用GNU sed语法。我没有现成的OS X测试框,但是BSD sed通常使用-E而不是-r来打开扩展的regex。

#1


13  

You could simply do this

你可以这么做

find . -exec sed -r -e 's/(^.*)Copyright \(c\) 2008 - 2009/\1Copyright (c) 2008 - 2010/g' {} \;

I'm presuming here that you don't want to lose whatever it is that's on the line before Copyright and is matched by .*

我在这里假设你不想失去任何在版权之前的线上并且被匹配的东西。*

But, depending on your input, it might be as simple as

但是,根据你的输入,它可以简单到

find . -exec sed -e 's/2009/2010/g' {} \;

Note that in both cases I am not modifying the files, just showing what the results of modifying the contents would look like. I imagine that in your real scenario you'd want to write the changes back to disk. For this you need the -i switch to sed, thus:

注意,在这两种情况下,我都没有修改文件,只是显示了修改内容的结果。我假设在真实场景中,您希望将更改写回磁盘。为此你需要-我切换到sed,因此:

find . -exec sed -r -i'' -e 's/(^.*)Copyright \(c\) 2008 - 2009/\1Copyright (c) 2008 - 2010/g' {} \;

It's worth noting that here I am using GNU sed syntax. I don't have an OS X box handy to test with but BSD sed generally uses -E instead of -r to turn on extended regex.

值得注意的是,我在这里使用GNU sed语法。我没有现成的OS X测试框,但是BSD sed通常使用-E而不是-r来打开扩展的regex。