0

I need to replace a bunch of

 &p_Data->scratchBuffer0_U32[ 0 ]

with

 scratchBuffer0_U32

in a tons of c files in a directory and its sub-directories.

Also, some developers wrote the [ 0 ] as [0] and [ 0]

electro
  • 911
  • 4
  • 12
  • 28

3 Answers3

1

This would be work:

find your_directory -name "*.c" | xargs sed -i 's/&\(p_Data->scratchBuffer0_U32\)[^]]*]/\1/g'
Paul
  • 1,630
  • 1
  • 16
  • 23
  • 1
    where does the output go in this cmd? Using the `-i` arg (assuming it is available), is the likely answer, but also risky until the O.P. tests for unintended consequences ;-) . Good luck to all. – shellter Sep 15 '17 at 01:57
  • @shellter Thanks, I forget to add the `-i` arg. I have updated my answer. :) – Paul Sep 15 '17 at 02:00
  • @electro I have updated me answer to match your need. Please let me know if it still goes wrong:) – Paul Sep 16 '17 at 02:42
0

If you are using *nix and you are trying to replace a string in multiple files, check out this answer here which shows you how to do a recursive replacement with find and perl.

As per the difference between the brackets, you'll have to run it for all 4 cases -- [0], [ 0], [0 ], and [ 0 ].

Saustin
  • 1,117
  • 6
  • 15
  • 27
0

With GNU sed for -i for pseudo-inplace editing:

find . -type f -exec sed -i 's/&p_Data->scratchBuffer0_U32\[[[:space:]]*0[[:space:]]*\]/scratchBuffer0_U32/g' {} +
Ed Morton
  • 188,023
  • 17
  • 78
  • 185