Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use PHP CodeSniffer, and here's the result:


There a a lot of small changes on one line (for example: change @author Olivier Pons to @author Olivier Pons <olivier.pons@gmail.com>) that I'd like to do via a shell script, or even better, via a command line, and (if possible) using regular expression.

Do you have any idea/suggestion/working solution for this problem?

Thank you!

(Note: I've already read this but it didn't help me)

share|improve this question
up vote 7 down vote accepted

I'm not sure what didn't help you in the link you provided, but sed seems like the right tool for this. It supports regular expression replacements, and if you pass it -i it will change files in-place:

$ sed -i 's/@author Olivier Pons/& <olivier.pons@gmail.com>/g' /path/to/your/file /path/to/another/file ...

(The & in the replacement is a shorthand that means "include the entire match")

share|improve this answer
Wrapping in find /where/your/files/live -type f -name '*.php' -exec sed -i s/see/above {} \; should deal with the whole lot of them, I suppose – sr_ Apr 16 '12 at 14:34
Is there any way to replace using a regular expression (no matter the number of spaces between author and "olivier pons"), and replace only the first match found? – Olivier Pons Apr 16 '12 at 14:58
@OlivierPons It's somewhat more complicated, since sed is intended to operate per-line, not per file, but this should work: sed '0,/@author \+Olivier Pons/ s/@author \+Olivier Pons/& <olivier.pons@gmail.com>/' – Michael Mrozek Apr 16 '12 at 15:07

I think you could use sed.

For example.

sed -i "s/@author Olivier Pons/@author Olivier Pons <olivier.pons@gmail.com>/g" `grep "@author Olivier Pons" -rl YOUR_FILE_DIR`
share|improve this answer
Please don't (ab)use command substitution like that. It's fragile and inefficient and find(1) already exists for this purpose. – jw013 Apr 16 '12 at 20:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.