1

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)

7

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")

  • 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
0

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`
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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