A bug in some code I wrote resulted in some files being formatted incorrectly. If it were only one file, I could write
cat filename | sed 's/bad/good/g' >filename2
mv filename2 filename
and fix it. I have several hundred files, however, and I would like to make the same transformation on all of them.
Is there some way to apply the same transformation to a large number of files with sed, keeping the filenames the same?
Is sed even the right tool for this job? If not, what should I use?
Use the
-i(in-place) option on modern versions of sed.or, with find
If you’re concerned about something going awry, you can supply a suffix as an argument to
-i, and a backup copy of each file will be saved with that suffix before the transformation is applied:If your version of sed doesn’t support the
-ioption, you can always use perl, which is where sed got the idea (only fair, since much of perl’s command-line syntax comes from sed):