Site TIPS PAGE🔎 SEARCH  Ξ INDEX  MAIN MENU  UP ONE LEVEL
 OJB's Web Site. Version 2.4. Mac Tip.Location: tip207 tips mac owen2 
Tip

Discuss   Up to OJB's Mac Tips List

Unix Search and Replace

To Search and Replace from the Command Line

Use "sed", the stream editor. You can use | (pipe) to send standard streams (STDIN and STDOUT specifically) through sed and alter them on the fly. To edit files directly, use the -i (in place) parameter.

sed -i "" -e 's/SEARCH/REPLACE/g' FILE

Where:
SEARCH is what to search for.
REPLACE is what to replace it with.
FILE is the path to the file to be processed.
s indicates the search terms.
g sets search to global (all instances on each line, not just the first).
-i specifies search the file "in place", otherwise uses standard input and output.
-e precedes each search specification; multiple can be used (see below).

Note 1: "" indicates don't add anything to the filename of the output file (just replace the existing file). This seems to be different in different shells, so YMMV.

Note 2: Any character (except "\" and "\n") can be used as the separator ("/" in the example above). Alternatively, use the escape character ("\") to escape "/" in the search or replace strings.

Note 3: the search string can be a regular expression.

Multiple Search and Replace

Use multiple search and replace string, each preceded by "-e":
sed -i "" -e 's/SEARCH1/REPLACE1/g' -e 's/SEARCH2/REPLACE2/g' FILE

Case Insensitive Search

Use the "I" option at the end of the strings:
sed -i "" -e 's/SEARCH/REPLACE/gI' FILE





I usually write a blog post about once a week. The latest post can be viewed here: Think for Yourself: In the end, everything is just an opinion. Be skeptical, and think for yourself! (posted 2026-04-25 at 21:12:02). Or go to the Home Page of My Blog to choose a post to view.

I do podcasts too! You can listen to my latest podcast, here: OJB's Podcast 2026-04-14 How Far is that Star?: How would we really know how big the universe is? Or subscribe to my podcast RSS feed, on my RSS Feeds page.

If you're not sure what to view from the thousands of pages on my site, here are some suggestions: My Latest Airshow Report (photos and movies from Warbirds Over Wanaka, 2026), My Favourite Wines and Beers (tasting notes for some of my favourites), An Interesting Astronomical Observation (learn a bit about astronomy from these observing notes), See Some Photos (A short rail journey through the Taieri Gorge), Read Some Mac Tips (Learn about how to use your Mac, and fix some problems).


Latest Site News and Notifications (Desktop): You are currently viewing OJB's web site, version 2.4 which has some major changes, and possibly errors! Please report any problems to ojb@mac.com.

 Site ©2026 by OJBRSS FeedMicrosoft Free ZoneMade & Served on Mac 
Site Features: Blog RSS Feeds Podcasts Feedback Log07 Jun 2024. Hits: 1,001,194,348
Description: Mac TipLogged: nothingLoad Timer: 365ms