Bash Sed

The sed command edits text as it flows through a pipe or file. The name stands for stream editor. Sed replaces words, deletes lines, and inserts text without opening an editor window. It works like the "Find and Replace" tool, controlled by a single command.

How Sed Works

  +-----------+     +----------------------+     +------------+
  |  Input    | --> |  sed reads one line  | --> |  Edited    |
  |  text     |     |  applies the rule    |     |  output    |
  |           |     |  repeats for others  |     |            |
  +-----------+     +----------------------+     +------------+
                   (the original file stays unchanged)

Sample File

Create notes.txt with these lines:

apple is red
banana is yellow
apple pie is sweet
grape is purple

Replace Text

The letter s stands for substitute. The syntax is s/old/new/.

sed 's/apple/mango/' notes.txt

Output:

mango is red
banana is yellow
mango pie is sweet
grape is purple

The original file stays the same. Sed prints the edited version on the screen.

Replace Every Match on a Line

Sed changes only the first match per line by default. The g flag (global) changes all of them.

echo "one one one" | sed 's/one/two/'
echo "one one one" | sed 's/one/two/g'

Output:

two one one
two two two

Ignore Case

sed 's/APPLE/mango/I' notes.txt

The I flag matches uppercase and lowercase letters alike.

Save Changes to the File

The -i option edits the file directly:

sed -i 's/apple/mango/g' notes.txt

Create a backup copy first by adding a suffix after the option:

sed -i.bak 's/apple/mango/g' notes.txt

macOS needs a slightly different form: sed -i '' 's/apple/mango/g' notes.txt.

Delete Lines

The letter d deletes lines.

sed '2d' notes.txt           # delete line 2
sed '2,3d' notes.txt         # delete lines 2 to 3
sed '/banana/d' notes.txt    # delete lines that contain banana
sed '$d' notes.txt           # delete the last line
sed '/^$/d' notes.txt        # delete empty lines

Print Selected Lines

The -n option hides the automatic output. The letter p prints chosen lines.

sed -n '2p' notes.txt        # print line 2
sed -n '1,2p' notes.txt      # print lines 1 and 2
sed -n '/grape/p' notes.txt  # print lines with grape

Insert and Append Lines

sed '1i Fruit List' notes.txt     # insert before line 1
sed '$a End of list' notes.txt    # append after the last line

Use a Different Separator

Folder paths contain slashes, which clash with the sed syntax. Pick another symbol such as | or #:

echo "/home/asha/docs" | sed 's|/home/asha|/home/ravi|'

Output:

/home/ravi/docs

Capture and Reuse Parts

Parentheses group text, and \1 brings the group back. The -E option removes the need for backslashes before parentheses.

echo "Asha Sharma" | sed -E 's/(\w+) (\w+)/\2, \1/'

Output:

Sharma, Asha

Run Several Edits at Once

sed -e 's/apple/mango/' -e 's/grape/lemon/' notes.txt

Practical Example: Clean a Settings File

Remove comment lines and blank lines from a configuration file:

sed -e '/^#/d' -e '/^$/d' settings.conf

Change a setting value in place:

sed -i 's/^port=.*/port=8080/' settings.conf

The pattern .* matches everything that follows port=, so any old value gets replaced.

Key Takeaways

  • sed 's/old/new/' replaces text, and the g flag replaces all matches.
  • -i saves edits to the file, and -i.bak keeps a backup.
  • d deletes lines and p with -n prints chosen lines.
  • Line numbers and patterns both select where a rule applies.
  • Choose a separator other than / when working with paths.