Linux Blog

Bulk Editing Text Files

Filed under: Shell Script Sundays — TheLinuxBlog.com at 1:00 am on Sunday, May 10, 2009

A Co-Worker wanted to edit a number of files in a directory that contained a lot of files. Each file that needed to be edited contained a function that needed to be replaced. Since it was production data we did not want to do a backup and run a sed find and replace for all files and risk screwing something up we decided to use vi to edit a list of files. Here is what I came up with to do that:

vi `grep function\_name * -n |cut -d : -f 1 | uniq`

If it were me, I would not have wanted to type sed find and replaces and would have done something like this because I’m lazy and I like to live on the edge:

 grep function\_name * -n | cut -d : -f 1 | uniq | while read i; do cp $i $i-bak; sed 's/function_name/new_function_name/g' $i-bak > $i; done;

Rather than editing them with vi it makes a -bak file, and uses sed to replace function_name with new_function_name. It does this from the bak file into the original. Some may think it’s kind of scary not making a backup, but I figure the -bak file should be enough depending on the operation. Make a backup if you value your data though.

Related Posts

Man Pages for commands in this post »

uniq
grep
sed

3 Comments »

Comment by tyboon

May 24, 2009 @ 5:04 pm

Hello,
Another solution to compute the substitution with sed is to use the -i option (in place) :

grep function\_name * -n | cut -d : -f 1 | uniq | while read i; sed -i.bak ’s/function_name/new_function_name/g’ $i; done;

Moreover, it automically backups your original files with a .bak extension

Thanks a lot for your blog BTW

Comment by xxx

December 8, 2009 @ 6:47 am

You don’t need to pass “-n” to grep (since you’re getting rid of anything but the file names). And if you pass “-l” (again, to grep) you avoid invoking the cut and the uniq…

Comment by TheLinuxBlog.com

December 8, 2009 @ 12:32 pm

@XXX fair enough, next time I need to do this, I’ll have to try and work your suggestions in.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>