oreilly.comSafari Books Online.Conferences.


Unix Power Tools

Change Many Files by Editing Just One

by Jerry Peek

The diff command can make an editing script that you give to the ex or ed editors or the patch program. They'll apply your same edits to other copies of the same file. This is handy if you have a lot of copies of a big file, spread around a network or on a lot of disks, and you want to make the same small change to all the files. Instead of sending new copies of the whole file, just have diff make a script -- and use that little script to update all the big files.

Here's a demo. I'm going to modify a program called pqs.c. Then I'll use diff and ed to apply the same changes to a copy of the file named remote-pqs.c (which might be at a remote computer):

1% cp pqs.c remote-pqs.c
2% cp pqs.c

3% vi
4% diff pqs.c
<       fprintf(stderr,
<           "%s: quitting: not able to %s your .pq_profile file.\n",
>       fprintf(stderr, "%s: quitting: can't %s your .pq_profile file.\n",
>               "WARNING:",
5% diff -e pqs.c > edscr
6% cat edscr
        fprintf(stderr, "%s: quitting: can't %s your .pq_profile file.\n",
7% echo w >> edscr
8% ed remote-pqs.c < edscr
9% diff remote-pqs.c

At prompt 1%, I make the simulated "remote" copy of the pqs.c file. At prompt 2%, I make another copy of it; at prompt 3%, I edit the copy. Prompt 4% has a diff that shows the changes I made. Then, at prompt 5%, I run diff - e; I save the result in edscr, which I show at prompt 6.

Prompt 7% is important because diff - e doesn't add a w command to the script file. That tells ed to write its changes to the file. I use echo w to add the command.

In prompt 8%, I give ed the name of the "remote" file to edit as a command-line argument and give it the script file on its standard input. At prompt 9%, I do a diff that shows the changes have been made and the two versions are the same.

Back More Unix Power Tools


Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!

Linux Resources
  • Linux Online
  • The Linux FAQ
  • Linux Kernel Archives
  • Kernel Traffic

  • Sponsored by: