views:

43

answers:

1

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
+2  A: 

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
sheepsimulator
Thanks this looks very helpful, but how would I iterate through the different files in the 5 directories? They have different names but the same extension.
Run Loop
Are each of these 5 things directory _trees_ or just directories? If they are just directories, I'd simply change file_pattern above to fit those 5 directories, or write a shell script to run that pair 5 times, once for each directory.
sheepsimulator
If they are directory _trees_, you'll have to write a script that traverses the tree, finds all the directories containing the files, and then run the pair of sed commands for each leaf directory. Not sure how to do this off the top of my head. sed, at least on machine, has no option to replace files in a recursive search. If you need to write this, I'd look for documentation on the unix find command.
sheepsimulator
@Run Loop - Do also remember you can use wildcards to match multiple files with the same extension. *.<extension> is a valid file_pattern.
sheepsimulator
Did not know that, thank you!
Run Loop
@Run Loop - ummm... I feel kinda bad about how I wrote these comments. I must apologize - I've been a commandline guy since I was 12, and I use wildcards on a daily basis. Sometimes I need to remember that by and large people don't do that much anymore with these newfangled GUIs. :) Thanks for being kind.
sheepsimulator
@Run Loop - Oh... I almost forgot... the <pattern to delete at beginning/end> part can be really weird to come up with. I can help with that, too.
sheepsimulator
If you could help with the pattern bit I would be most grateful. Thank you so much for all your help. My command line knowledge is embarrasing!
Run Loop
@Run Loop - If you could add examples of the text that you need to delete at the beginning and end of these textfiles by editing your initial question, I can help you out.
sheepsimulator
Included the examples. Please note that there are newline characters at the end of each phrase so the above examples would appear on 4 lines in the file.
Run Loop
@Run Loop - Thanks!
sheepsimulator
Thank you very much. I tried it but it in Terminal but it says "Invalid Command Code A"
Run Loop
Hi, again!. It works now, I had to include -e after the -i, apparently it's necessary to do so in OSX. I just want to say thank you very very much for all your kind help and patience! I have learnt a lot.
Run Loop
@Run Loop - Good to hear! Glad to be of service.
sheepsimulator