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 <[email protected]>) 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)

2 Answers 2

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/& <[email protected]>/g' /path/to/your/file /path/to/another/file ...

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

3
  • 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_
    Commented Apr 16, 2012 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? Commented Apr 16, 2012 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/& <[email protected]>/' Commented Apr 16, 2012 at 15:07
0

I think you could use sed.

For example.

sed -i "s/@author Olivier Pons/@author Olivier Pons <[email protected]>/g" `grep "@author Olivier Pons" -rl YOUR_FILE_DIR`
1
  • 1
    Please don't (ab)use command substitution like that. It's fragile and inefficient and find(1) already exists for this purpose.
    – jw013
    Commented Apr 16, 2012 at 20:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .