问题
I want to delete text from multiple files. It could be from 10 to 1000 files, so deleting it by hand is too much work. I found a couple of SO questions, such as: Delete specific lines in multiple Text Files with freeware App? but none of them helped
To do find and replace, I could write a perl command, but it lacks options. For example, I want to delete the first four lines in all files. Does anyone know a solution? Or maybe an app? I would prefer something without VBScript.
回答1:
sed(1) or awk(1) would be great for this job. For example, to delete the first 4 lines of all of the .txt
files in the current directory:
sed -i~ 1,4d *.txt
This will delete the first 4 lines of all .txt
files anywhere in a subdirectory of the current directory:
find . -type f -iname \*.txt -exec sed -i~ 1,4d '{}' +
回答2:
BBEdit TextWrangler (free) has the capability to make changes in multiple files. The command is "Multi-File Search" under the "Search"menu.
回答3:
SED would probably work very well. Is free and can do a lot of what you want. There are windows, mac and linux ports.
SED Stream Editor
回答4:
If for example you are trying to remove comments from multiple files of code, and there is a pattern like all comments start with #, then open up a console, move into the directory where your files reside, and type the following commands:
foreach file (*dat)
sed '/^\#/d' $file > tt
mv tt $file
end
In other cases, you may have a text file for which a give number of lines on top are comments. Let's say that you want to get rid of the first 3 lines. Well, here is the trick:
sed '1,3d' myFile > tt
mv tt myFile
Here is what happens:
sed '/^\#/d' myFile
removes all lines starting with # from the file myFile and outputs the result in the console.> tt
redirects the output into a temporary file called ttmv tt myFile
moves the temporary file tt to myFile
来源:https://stackoverflow.com/questions/8139329/delete-text-from-multiple-files