Linux sed FAQ: How can I use the sed command to modify many files at one time?
Here's a Linux/Unix sed command file that I just used to modify a bunch of files:
s/^M//g 1,2d $d
I put those three lines in a text file, and named the file sed.cmds.
After that, I call that sed script from a simple shell script. Here are the contents of the shell script:
#!/bin/sh
# modify every *.txt file in the current directory
for i in `ls *txt`
do
sed -f sed.cmds < $i > ${i}.new
done
What I'm doing in this shell script is:
"txt".".new" is appended to the end of the original filename."-f sed.cmds" argument to the sed interpreter.Nothing too exciting here, but it is incredibly productive, and I don't want to forget how I did this.
Also note that you can rename your ".new" files back to ".txt" files with a mv command, but I'll leave that as an exercise for the reader, especially because that command will overwrite your original .txt files, and I don't want anyone blaming me for that. :)
Post new comment