编辑大量文本文件

发布于 2024-09-11 00:15:58 字数 314 浏览 7 评论 0原文

我有大约 350 个文本文件,其中包含硬盘上 5 个文件夹的全部内容。对于每个文件,我想删除每个文件开头和结尾处指定数量的字符。我对 AppleScript 不了解,但怀疑它可能适合我想要实现的目标。任何有关自动化此操作的帮助将不胜感激,因为手动编辑这些文件是一项艰巨的任务。非常感谢。

需要从每个文件的开头删除以下文本:

STARTTYPE:RGIN
MODEXP:NO

需要从每个文件的末尾删除以下文本:

REFACTORSCALE:2.0
ENDTYPE:FACTORED

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2024-09-18 00:15:58

OS X 是否有 unix 命令 sed ?它正是为了解决此类问题而设计的。

您可能会说(假设每个文件中要删除的文本都相同):

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

例如,假设一堆 .txt 文件需要在您的主目录下的这 5 个目录中完成这些编辑。你可以这样做:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文