Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command-line recursive code cleanup, '){' to ') {', using grep/sed

Tags:

regex

grep

sed

I'm trying to clean up my code and make it consistent, by globally adding a space between ) and {. For instance:

function (args){ -> function (args) {
while(1){ -> while(1) {
foreach($a in $b){ -> foreach($a in $b) {

I'm trying to, by command line, put something together that will go through folders and apply this rule to all files recursively, by searching for "){" and replacing with ") {", using some combination of sed and find/grep.

What's the right way to do this? I'm not sure what needs escaping due to either sed, find/grep, or the shell.

If it matters, the shell is Bash, and the code is php that occasionally generates javascript. I did a quick grep to see what might come up, and it looks fine to do this general search/replace, though if anyone sees potential trouble I'd love the pointer. :)

Thank you! :)

Edited to add: Solutions using common linux programs other than grep/sed/find definitely very welcome, that's just what I'm starting with. Thank you! :)

like image 940
heykatieben Avatar asked Dec 01 '25 04:12

heykatieben


1 Answers

You can try this sed:

sed -i.bak 's/){/) {/g' file

To make it recursive use find:

find -type f -exec sed -i.bak 's/){/) {/g' '{}' +
like image 131
anubhava Avatar answered Dec 02 '25 22:12

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!