Save time and destroy the world with bash
Using a bash for loop in the shell is an extremely powerfull way to apply global changes to code, and has proved essential for me when upgrading Symfony2 PRs :) But I always forget the syntax, so I am sharing the wealth.
Search and replace in matching files
for i in `find -name "*ImporterExtension.php"`; do cat $i | sed -e 's/function extend/function extendSiteImport/g' > $i.new; mv $i.new $i; done;
This will find all files names matching "ImporterExtension", pipe the contents to sed which pipes the output to a file called [original filename].new, which is then moved to the original filename, overwriting it. This is necessary I think as if you redirect the output to the same file you are cat'ing it will be blank and your code will disapear ...
"git mv" (rename) all files matching a pattern
for i in `find -type f name "twig.html"`; do git mv $i `echo $i | sed 's/twig.html/html.twig/g'; done;
Upper case matches and underscore space between 2 words
e.g. <foobar label="Do Something" help="Help me"/> to <foorbar label="DO_SOMETHING" help="Help me"/>
for i in `find ./ -name '*.xml' | egrep "site_config|navigation|global" | grep -v skeleton`; do cat $i | sed -e 's/label="\([^\"]*\)"/label="\U\1"/g' | sed -e 's/label="\([A-Z]*\)[[:blank:]]\([A-Z]*\)[[:blank:]]\([A-Z]*\)"/label="\1_\2_\3"/g' > $i.new; mv $i.new $i; done;
I have to run this at least 2 times to match 2 words and then 3 words, am sure there is a better way..
Comments
Post new comment
Tags
- DropBox
- XMPP
- android
- apache
- archos
- audacious
- awesome
- bash
- bootstrap
- bristol
- diagramming
- doctrine
- doctrine2
- git
- gloucester
- graphs
- gt540
- jack
- javascript
- manchester
- mapdroyd
- markdown
- mongodb
- paris
- php
- profiling
- projectm
- running
- scripting
- sed
- software design
- ssh
- symfony
- symfony2
- thonon-les-bains
- trainer
- travel
- twig
- ubnutu
- vim
- weymouth
- workflow
- xdebug
- xml
- ylly
- yprox
10 Latest Items
-
08
Maytrainer [Velo] paris - compiègne 153.00km / 05:48:32 / 00:02:16mpkm Fois.
-
06
Maytrainer [Velo] Vincennes Hippodrome 1hr 34.34km / 01:00:00 / 00:01:44mpkm Solo effort. Did interfals (sprinting from zebra crossing to hairpin turn).
-
05
Maytrainer [Run] Diderot > Pnt. Alx III > Rue de Charonne 13.96km / 01:08:40 / 00:04:55mpkm Good to run in the rain. Lots of traffic.
-
04
Maytrainer [Velo] Vincennes Hippodrome 1hr 32.20km / 01:00:00 / 00:01:51mpkm Rode apace a peloton, but tried not to get in the draft. Sprinted for a few minutes on every lap.
-
03
Maytrainer [Run] Dumas > P. Auguste > Belleville > Prc. de Villette > Pt. de Lilas > Pt. Vincennces 14.48km / 01:07:00 / 00:04:37mpkm Kept up a reasonably good pace. No problems from calf muscle as was the case yesterday.
-
02
Maytrainer [Run] Dumas > Diderot > Trocadero > Basitlle 14.48km / 01:10:00 / 00:04:49mpkm Experienced pain in the back of my calf and stopped a few times towards the end.
-
01
Maytrainer [Velo] Dumas > Rambouillet 123.31km / 05:06:00 / 00:02:28mpkm Paris Rambouillet. Sunny day. Ate a bakery pizza, tarte au pomme and drank a cola then lay down on a bench in the châte...
-
29
Aprtrainer [Run] 14 - 15 miles 23.34km / 01:50:00 / 00:04:42mpkm Run down the Rue Charonne, down Boulevard Henri IV, round le Ile St-Louis, along the Seine, crossing the Pont d'lén...
-
28
Aprtrainer [Velo] Vincennes Hippodrome 1hr 34.12km / 01:00:00 / 00:01:45mpkm Actually 45 minutes. Rain stopped play. Also boredom. "Sprinted" up the second half of the upside at each circ...
-
27
Aprtrainer [Run] 54 minutes 11.50km / 00:54:00 / 00:04:41mpkm Ran down and around.
