2

I'm trying to automatically replace some Copyright string in files. The string is is following format

"Copyright (C) 2004-2008 by"

but years can differ. I try to find this lines in all files and replace the last year with current.

grep -r ' * Copyright (C) [0-9]\{4\}-[0-9]\{4\} by.' *

Now how can I replace the last group found with 2013? (Want to use from pipe)

2 Answers 2

10

grep doesn't do replacements. You can try sed, e.g.:

sed 's/Copyright (C) \([0-9]\{4\}\)-[0-9]\{4\} by/Copyright (C) \1-2013 by/'

or as Kent notes:

sed 's/\(Copyright (C) [0-9]\{4\}\)-[0-9]\{4\} by/\1-2013 by/'

or ssed:

ssed -R 's/(?<=Copyright \(C\) )([0-9]{4})-[0-9]{4}(?= by)/\1-2013/'
Sign up to request clarification or add additional context in comments.

3 Comments

+1, it is job for sed. \1 could start from Copyright.., to save some typing. also from the question, I feel OP needs -i. anyway nice answer!
@Kent Good point, thank you. As for -i flag: I'd think that too, but the OP notes: Want to use from pipe. Anyway, -i is now mentioned.
indeed, the -i for saving files :) Thanks a lot both, this is exactlly what I was looking for.
4

This is what I came up with to answer your question:

sed -i 's/Copyright (C) \([0-9]\{4\}\)-[0-9]\{4\} by/Copyright (C) \1-2013 by/' `find -type f`

I'm using the sed query proposed by @Lev, but acting on the files directly. I included the -i to save the changes to the file. I also included a "find" to the end of the command line, so that sed would look for files recursively.

Please note the different types of quotes used. The first two are simple quotes, but the last two are back quotes, used to make the shell run the "find" command and use its results as parameter to "sed".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.