How can I find and replace specific words in a text file using command line?
Kaz Wolfe Jon Doe Jon Doe 3, 5 5 gold badges 17 17 silver badges 12 12 bronze badges. May of your interest github. Akiva If you include regex special characters in your search sed will match them. Add a -r flag if you want to use extended REs instead. BrianZ As far as the file system is concerned the output of sed is a new file with the same name.
The OSX command sed -i '. MacOS users will have to add ''" after -i as a parameter for -i ed. You can directly say sed ' Indeed this can be reduced further: sed -i'. The file contents: roses are red , violets are blue This is an input. Perl Perl is another tool which is often used for text processing, but it's a general purpose language, and is used in networking, system administration, desktop apps, and many other places.
Python This language is very versatile and is also used in a wide variety of applications. For instance, here's a simple one:! Sergiy Kolodyazhnyy Sergiy Kolodyazhnyy Nice compilation! TapajitDey Yes, tr is another great tool, but note that it is for replacing sets of characters for example tr abc cde would translate a to c , b to d.
Steven Penny Steven Penny 1. Moses 3 3 bronze badges. Avinash Raj Avinash Raj I used this on MacOS terminal and it did nothing I'm watching file with inotifywait under sh env, and reporting data in CSV format because custom format is buggy.
I then figured there is no simple way to handle CSV document in shell scripts And I want it very light. So I started a quite simple script to parse and report CSV. I read CSV spec and noticed it is more elaborated than I expected and support multiline value wrapped in double quotes. I was relying on sed for tokenization but soon realized that even what sed call multilines is up to two lines. TextMate has full support for regular expressions and handles files and directories very well.
Another that I haven't tried but heard good things about is TextWrangler. Sign up to join this community.
The best answers are voted up and rise to the top. Home Questions Tags Users Unanswered.
Asked 9 years, 5 months ago. Active 2 years, 7 months ago. Viewed 19k times. I'm aware of grep and I don't need an editor, just a standalone tool. Komodo Edit activestate. Its a free editor for Mac, Windows and Linux. Josh Josh 5, 9 9 gold badges 40 40 silver badges 69 69 bronze badges.
Perl can do inline replacement: find.
If you want perl to make backup copies before replacing, run: find. How it goes around here apparently. Open in Script Editor. Listing and Listing show how to call the handlers in Listing and Listing to convert text to uppercase.
Listing and Listing show how to call the handlers in Listing and Listing to convert text to lowercase. The handler in Listing can be used to find and replace text in a string.
To use it, provide some source text, a string to find, and a replacement string. This handler replaces any found instances of the specified search string. Listing shows how to call the handler in Listing Unlike the previous AppleScript example, this function replaces only the first occurrence of the found text.
The replace method can be combined with a regular expression to replace every occurrence of the found text, as shown in Listing Listing and Listing show how to get a list of characters in a string. Listing and Listing show how to get the length of—the number of characters in—a string.