🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

text replace question for Unix masters

Started by
2 comments, last by ToohrVyk 16 years, 8 months ago
Barring opening up an editor and doing search replace, I was hoping that a nix master could tell me how to do the following on set of files: I would like to replace: "Delete ptr;" with "Delete(ptr)" and "Delete []ptr;" with "DeleteArray(ptr)" where ptr can be any C++-identifier... I was thinking of somehow using sed, but then I was not too sure how to go about it... [lex/flex are also an option, but they seem like overkill for such a simple operation] Best Regards
Close this Gamedev account, I have outgrown Gamedev.
Advertisement
Take a look at the 'sed' command, or 'perl -e'. A quick google search shows many possibilities.

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

I did look at sed, but as I am quite unfamilary with it, I was not too sure how to manage, I though: replace "Delete []ptr;" with "Delete_array(ptr)", so the string to replace would be: "Delete []"*";", and maybe the correct sed command would then be, I thought:

sed 's/Delete \[\]*;/Delete_array(&);/' < InFile.cpp > OutFile.cpp

but that did not do anything... but alas I realized that I should be using .* and not *, at the end of the day 3 sed commands piped together did the full job of replacing "Delete ptr;" with "Delete(ptr)" and "Delete []ptr;" with "Delete_array(ptr);"

sed 's/\(Delete \[\]\)\(.*\);/delete_array(\2);/g' $1 | sed 's/\(Delete\)\(.*\);/Delete(\2);/g' | sed 's/delete_array/Delete_array/g'

but atleast I learned sed better :)

Close this Gamedev account, I have outgrown Gamedev.
Also, you could have called sed only once by writing:

sed 's/\(Delete \[\]\)\(.*\);/delete_array(\2);/g; s/\(Delete\)\(.*\);/Delete(\2);/g;s/delete_array/Delete_array/g;' $1

This topic is closed to new replies.

Advertisement