Take the 2-minute tour ×
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.

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

-----------------------------------------------------------------
A TOTAL OF 3008 ERROR(S) AND 380 WARNING(S) WERE FOUND IN 46 FILE(S)
-----------------------------------------------------------------

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
add comment

2 Answers 2

up vote 6 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
2  
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
1  
@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
add comment

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
1  
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
add comment

Your Answer

 
discard

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.