Reputation: 175
I want to replace anything that looks like
if (Thing *thing1 = Stuff) {
with
Thing *thing1 = Stuff;
if (thing1) {
Essentially take declarations out of if statements.
I have all the parts and I see that they're correct, I'm just having trouble with the replacement
grep ' if (.* = .*) {' $file | while read -r line ; do
inside="$( echo "$line" | cut -d "(" -f2 | cut -d ")" -f1 );"
object="$( echo $( echo "$inside" | cut -d "*" -f2 | cut -d "=" -f1 ) | xargs )"
newIF=" if ($object) {"
replacement="$inside\n$newIF"
line_regexp="$(echo "$line" | sed -e 's/[]\/$*.^|[]/\\&/g')"
replacement_regexp="$(echo "$replacement" | sed -e 's/[\/&]/\\&/g')"
sed -i.bak "s/$line_regexp/$replacement_regexp/g" $file
done
Edit thanks to: https://superuser.com/questions/422459/substitution-in-text-file-without-regular-expressions
Now I just have to figure out how to turn "\n" into an actual newline
Upvotes: 0
Views: 36
Reputation: 15461
This sed should work for you:
$ sed -n 's/if (\([^ ]* \*\([^ ]*\) = [^)]*\)) {/\1;\nif (\2) {/p' <<< "if (Thing *thing1 = Stuff) {"
Thing *thing1 = Stuff;
if (thing1) {
Upvotes: 1