1
grep -Hrl DataLogger /testFolder/ | xargs sed /,$/{N -e"s|DataLogger::copyStringLength(\(dl.[a-zA-Z0-9]\+\)..\(.\+\)(.c_str()\)\+\(,\s*.*;)|IDataLogger::copyString(\2, \1);|g"}

I'm using the above script to change a method call from DataLogger::copyStringLength(dest, source, length) to IDataLogger::copyString(source, dest).

The script above overall seems to do as I want it to, and is replacing things regardless of newlines in the method call. But as it is now, I am only printing to the console, rather than in-place in the files. I've tried placing the -i option in several places in the sed call, and it always gives back an error.

Any feedback would be appreciated.

For those curious as to why and where the line breaks appear, there are 3 different formats for this method (depending on the length of variable names)

copyStringLength(varA, varB, varC);

copyStringLength(varA, varB,
                 varC);

copyStringLength(varA,
                 varB,
                 varC);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Are you doing this on OS X? – Barmar Mar 03 '17 at 02:54
  • If you are, see http://stackoverflow.com/questions/4247068/sed-command-with-i-option-failing-on-mac-but-works-on-linux/4247319#4247319 and http://stackoverflow.com/questions/7573368/in-place-edits-with-sed-on-os-x/7573438#7573438 – Barmar Mar 03 '17 at 02:57
  • We're running on RHEL6. Though looking at the first question, if there's a way to suppress the backup files, that would be great. Not necessary, but saves me running several find/deletes. – Catherine Lyons Mar 03 '17 at 02:59
  • 2
    What is the error? – l'L'l Mar 03 '17 at 03:00
  • Just leave out the backup suffix after `-i` and it doesn't create backup files. – Barmar Mar 03 '17 at 03:04
  • 1
    Post the command that's getting the error. – Barmar Mar 03 '17 at 03:04
  • I believe you are experiencing a quoting problem. Your sed command should take the form sed -e '...' -e '...' i .e. surround each entire expression with single quotes (If I were you I would not use double quotes unless you wish to interpolate a shell variable). – potong Mar 03 '17 at 08:02
  • To help people answer your question, you'll need to be more specific about the error. Please [edit] your post to incorporate the exact errors you get from your [mcve] (preferably using copy+paste to avoid transcription errors). – Toby Speight Mar 03 '17 at 13:39

1 Answers1

0

I don't see how your sed command can work the way it is in your question (maybe it's a mistake in the copy from the original script).

Anyway, from your code I can get two assumptions:

  • Parameters cannot containng , (strings, called functions, etc)
  • The function call is divided in max 3 lines.

That said, the command line would be:

grep -rl DataLogger /testFolder/ | xargs sed -i \
    -e '/,\s*$/N;/,\s*$/N' \
    -e 's/\(DataLogger::copyString\)Length(\s*\([^,]\+\)\s*,\s*\([^,]\+\)\s*,.*);/I\1(\3, \2);/g' 

Explanation

  • In grep -H and -l are equivalent (printing the filename) with the -r (recursive) option.
  • The -i edit the file in place (without backup)
  • The first -e joins the first two lines that end with , with the next one (ignoring trailing spaces)
  • The second -e makes the switch of the first two parameters excluding the third parameter and any spaces between them.

It will work assuming that there's no spaces in the filenames, or else sedwill fail.

Wilfredo Pomier
  • 1,091
  • 9
  • 12