How to bulk edit TXT files?

Greetings,
How do I batch replace a long string of text with another one?

It’s at the very beginning of the text file.

I need to replace this text with…nothing…I simply have to delete it from these hundreds of files.

Well…I also have some text to remove from the very bottom of the file… :wink:

The text I want to remove is the exact same on all the files: they are HTML webpages converted to TXT, so I need to cut all the text that is not the body, such as menu and other.
Alas I can’t reach the original HTML files.

Any (possibly no code or low code) workaround?

Thank you!

Sed and awk would be able to do this, but I rarely use them, so I’d have to Google exactly how to use it again!

Sed appears to be relatively easy - Delete Lines With Sed [11 Examples] has some examples. Whilst it’s a terminal solution, it may help?

The likes of Visual Studio and other text editors allow you to open multiple files and once and delete lines, but not sure that’s helpful for a large number of files

1 Like

I cannot think of a “no code” solution. Probably relatively simple to do with Python but one must know the exact patterns you are searching for and what the replacement text is. And of course have to know some Python. Also, most any coding language could do this.

Perhaps find a programmer to do this for you?

BBEdit?


JJW

7 Likes

Yep; BBEdit search and replace; no coding needed

2 Likes

The :bufdo command in Vim:

As others have suggested, I would recommend BBEdit. It is an amazing text tool for countless reasons. Its Search and Replace functionality is peerless.

It is also capable of dealing with an entire folder of text files in one swoop. So it could complete your task in a couple of seconds.

No programming. The above site discusses how to set this up. It is a two or three step process.

1 Like

This can also be done in vscode in case you’re already a user.

1 Like

As others have suggested use sed or awk in a Terminal session. I have to change text like that when dealing with electoral data from my local council. Although I found the GNU version sed better than the default BSD version that comes pre-installed with macOS. (GNU sed has a few more options available when processing repetative patterns.)

Once upon a time I’d have used sed for this, but these days I’d just use python, mostly because the string functions are easier to remember (for me) than sed.

This is the kind of problem that Unix geeks (counting myself as one) dream of when we go on about how command line interfaces are superior to GUIs for “real work” :laughing:

3 Likes