Author Archives: rotiyan

sed a powerful command line editor.

Recently I had to work with a lot of xml files in connection to my work. Now there are few 10’s of them less 100 arranged in a several directories. The original xml files that I had was passed on to me by a colleague who obviously had a different directory structure and she developed it accordingly for her directory layout. Also there is a special xml tag which lets the file identify itself in terms of its absolute path. Something like <path file=”/l1calo/segments/dqm/<thisfile>.xml” /> Now I need to repace the root path “/l1calo” with “myhome/l1calo”. Only then could I load these xml files to the application. Otherwise it wouldn’t work. So How do I do it. I could open each file in gvim and have done a pattern substitution. But this is extremely inefficient, given the fact that I have to go through 10 directories which on an average contains 5 or 6 xml files.

This problem can be easily solved using sed. A command line text editor. I just had to loop through all the directories and execute sed to replace the specific pattern with my choice. Here is command .  cd to the root directory of your xml’s and then do

for dir in `ls -d */`; do (cd $dir; sed -i 's/\/l1calo\/segments/\/home/\/myhome\/l1calo\/segments/g' * ); done;

Thats it.

Leave a comment

Filed under Unix