Find and replace

4 snippets across 4 stacks - Bash & Linux, JavaScript, Regular Expressions, SQL

SHBash & Linux

Stream Editor for Find & Replace

SH · Text Processing
Syntax
sed [options] 's/pattern/replacement/flags' file
Example
sed 's/localhost/0.0.0.0/g' config.ini
sed -i.bak 's/v1\.2/v1.3/g' version.txt
sed -n '10,20p' access.log
sed '/^$/d' notes.txt

Note -i edits in place. -i.bak creates a backup before editing (strongly recommended). g flag replaces all occurrences on a line, not just the first. -n suppresses default output; use with p to print specific lines. /d deletes matched lines.

JSJavaScript

Replacing Text

JS · Strings
Syntax
str.replace(search, replacement)
str.replaceAll(search, replacement)
Example
const template = "Hello {name}, welcome to {place}!";
const filled = template
  .replace("{name}", "Ava")
  .replace("{place}", "Berlin");
console.log(filled);

const csv = "a,b,c,d";
console.log(csv.replaceAll(",", " | "));
Output
"Hello Ava, welcome to Berlin!"
"a | b | c | d"

Note replace() only swaps the first match unless you use a regex with the /g flag. replaceAll() replaces every occurrence.

RXRegular Expressions

JS: replace() / replaceAll()

RX · String Operations with Regex
Syntax
string.replace(/pattern/, replacement)
string.replaceAll(/pattern/g, replacement)
Example
// Backreference in replacement
'John Smith'.replace(/(\w+) (\w+)/, '$2, $1')
// Function replacement
'hello'.replace(/\w/g, c => c.toUpperCase())
Output
"Smith, John"
"HELLO"

Note In the replacement string, $1 $2 refer to captured groups, $& is the full match, $` is before-match, $' is after-match. Use a function for dynamic replacements. replaceAll() requires the g flag on regex arguments.

SQLSQL

REPLACE

SQL · String Functions
Syntax
REPLACE(string, search, replacement)
Example
SELECT
  REPLACE(phone, '-', '') AS digits_only,
  REPLACE(product_name, 'V1', 'V2') AS updated_name
FROM products;
Output
-- digits_only | updated_name
-- 2065551234  | Widget V2 Pro

Note REPLACE is case-sensitive in PostgreSQL and MySQL. It replaces all occurrences, not just the first one. For regex-based replacement in PostgreSQL, use REGEXP_REPLACE.

Frequently asked questions

How do you find and replace?
This task is covered in 4 stacks on this page: Bash & Linux, JavaScript, Regular Expressions, SQL. The "Stream Editor for Find & Replace" snippet in Bash & Linux uses `sed [options] 's/pattern/replacement/flags' file`.
Which command does the Bash & Linux example use?
The "Stream Editor for Find & Replace" snippet uses `sed [options] 's/pattern/replacement/flags' file`, from the Text Processing section of the Bash & Linux cheat sheet.
Which stacks cover "find and replace" on this page?
Bash & Linux, JavaScript, Regular Expressions, SQL. Together they hold 4 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Stream Editor for Find & Replace": -i edits in place. -i.bak creates a backup before editing (strongly recommended). g flag replaces all occurrences on a line, not just the first. -n suppresses default output; use with p to print specific lines. /d deletes matched lines.