Sign in to follow this  
kRogue

text replace question for Unix masters

Recommended Posts

kRogue    100
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

Share this post


Link to post
Share on other sites
kRogue    100
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 :)

Share this post


Link to post
Share on other sites
ToohrVyk    1595
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

Share this post


Link to post
Share on other sites
Sign in to follow this