Linux Blog

Bulk Editing Text Files

Filed under: Shell Script Sundays — 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.

Man Pages for commands in this post »



Comment by tyboon

May 24, 2009 @ 5:04 pm

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

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.

Comment by nabu

January 27, 2014 @ 7:30 pm

Now, I have similar situation here, but with addition:
there is an “X” value in a series of config files:
“x: integer value”

I intent to change this value in all documents to any value subtracted from the existent value (or whichever math operation is put here)

So if, for instance, there is:
file1 x: 500, file2 x: 700, file3 x: 250…
I would like to change them to a value minus 200, so the new values should be:
file1 x: 300, file2 x: 500, file3 x: 50

I know it is possible, but it exceeds my knowledge.

Any idea?


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>