How to use find, -exec, grep, and sed with regular expression to replace part of a line of text?
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:
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.
Admin
Updated on July 09, 2022Comments
-
Admin almost 2 years
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. Stackoverflow 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.
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"
find . -exec grep -Hn '^.*Copyright (c) 2008 - 2009' {} \;