RunLoop
RunLoop

Reputation: 20376

Editing large number of text files

I have about 350 text files which comprise the entire contents of 5 folders on my HD. For each of these files, I would like to delete a specified number of characters at the start and end of each file. I have no knowledge of AppleScript but suspect it may be suitable for what I want to achieve. Any help on automating this would be greatly appreciated as manually editing these files is a daunting task. Thank you very much.

The following text needs to be removed from the start of each file:

STARTTYPE:RGIN
MODEXP:NO

The following needs to be removed from the end of each file:

REFACTORSCALE:2.0
ENDTYPE:FACTORED

Upvotes: 1

Views: 158

Answers (1)

J. Polfer
J. Polfer

Reputation: 12481

Does OS X have the unix command sed? It was designed to solve precisely this type of problem.

You would say (assuming your text to remove is the same in every file):

sed -i 's/STARTTYPE:RGIN\nMODEXP:NO\n//' file_pattern
sed -i 's/REFACTORSCALE:2.0\nENDTYPE:FACTORED\n//' file_pattern

For example, lets say a bunch of .txt files need to have these edits done in these 5 directories under your home directory. You could do:

sed -i 's/STARTTYPE:RGIN\nMODEXP:NO\n//' /home/Run_Loop/directory_1/*.txt
sed -i 's/REFACTORSCALE:2.0\nENDTYPE:FACTORED\n//' /home/Run_Loop/directory_1/*.txt
sed -i 's/STARTTYPE:RGIN\nMODEXP:NO\n//' /home/Run_Loop/directory_2/*.txt
sed -i 's/REFACTORSCALE:2.0\nENDTYPE:FACTORED\n//' /home/Run_Loop/directory_2/*.txt
sed -i 's/STARTTYPE:RGIN\nMODEXP:NO\n//' /home/Run_Loop/directory_3/*.txt
sed -i 's/REFACTORSCALE:2.0\nENDTYPE:FACTORED\n//' /home/Run_Loop/directory_3/*.txt
sed -i 's/STARTTYPE:RGIN\nMODEXP:NO\n//' /home/Run_Loop/directory_4/*.txt
sed -i 's/REFACTORSCALE:2.0\nENDTYPE:FACTORED\n//' /home/Run_Loop/directory_4/*.txt
sed -i 's/STARTTYPE:RGIN\nMODEXP:NO\n//' /home/Run_Loop/directory_5/*.txt
sed -i 's/REFACTORSCALE:2.0\nENDTYPE:FACTORED\n//' /home/Run_Loop/directory_5/*.txt

Upvotes: 2

Related Questions