Sed

sed (Stream Editor) is a powerful text-processing tool in Unix/Linux that allows you to perform various operations on text files or streams. The most common operation is the substitution (s) command, but sed offers many other commands and options. Below are explanations of some common usages:

1. Substitution (s) Command

The s command in sed is used for substitution, meaning it replaces a specified pattern with another.

Basic Syntax:

sed 's/pattern/replacement/'
  • Example:

    echo "hello world" | sed 's/world/universe/'

    Output:

    hello universe

Common Flags:

  • g (global): Replaces all occurrences of the pattern in a line, not just the first one.

    echo "hello world world" | sed 's/world/universe/g'

    Output:

    hello universe universe
  • n (specific occurrence): Replaces the nth occurrence of the pattern in a line.

    echo "hello world world world" | sed 's/world/universe/2'

    Output:

    hello world universe world
  • p (print): Prints the lines where substitutions were made.

    echo "hello world" | sed 's/world/universe/p'

    Output:

    hello universe
    hello universe
  • i (ignore case): Makes the search case-insensitive.

    echo "Hello World" | sed 's/world/universe/i'

    Output:

    Hello universe

2. Delete (d) Command

The d command deletes lines matching a specified pattern.

Example:

echo -e "line1\nline2\nline3" | sed '/line2/d'

Output:

line1
line3

3. Print (p) Command

The p command prints lines matching a pattern. Often used with the -n flag to suppress automatic printing of lines.

Example:

echo -e "line1\nline2\nline3" | sed -n '/line2/p'

Output:

line2

4. Insert (i) Command

The i command inserts text before a matching line.

Example:

echo -e "line1\nline2\nline3" | sed '/line2/i\newline'

Output:

line1
newline
line2
line3

5. Append (a) Command

The a command appends text after a matching line.

Example:

echo -e "line1\nline2\nline3" | sed '/line2/a\newline'

Output:

line1
line2
newline
line3

6. Replace Line (c) Command

The c command replaces the entire line matching a pattern.

Example:

echo -e "line1\nline2\nline3" | sed '/line2/c\replaced line'

Output:

line1
replaced line
line3

7. Transform (y) Command

The y command works like the tr command, translating one set of characters to another.

Example:

echo "hello" | sed 'y/abcde/ABCDE/'

Output:

hEllo

8. Multiple Commands (-e or ;)

You can chain multiple sed commands together using ; or the -e flag.

Example:

echo "hello world" | sed -e 's/hello/hi/' -e 's/world/universe/'

Output:

hi universe

9. Substitute with Shell Variables

You can use shell variables in sed by wrapping them in double quotes.

Example:

name="world"
echo "hello world" | sed "s/$name/universe/"

Output:

hello universe

10. Address Ranges

You can apply sed commands to specific lines or ranges of lines.

Example:

echo -e "line1\nline2\nline3" | sed '2s/line2/changed/'

Output:

line1
changed
line3

Summary

  • s: Substitute a pattern with a replacement.

  • g: Apply substitution globally across the line.

  • d: Delete matching lines.

  • p: Print matching lines.

  • i: Insert text before a line.

  • a: Append text after a line.

  • c: Change a matching line.

  • y: Translate characters.

These are just a few examples of what you can do with sed. It's a very flexible and powerful tool for text processing.

Last updated