sed

sed

The sed command, short for "stream editor," is a powerful utility in Unix and Linux used for text manipulation. It allows for searching, finding, and replacing text, as well as other text processing operations.

Basic Usage

The basic syntax for the sed command is:

sed [options] 'command' [file]
  • options: Command-line options to control sed behavior.

  • command: The sed command to execute.

  • file: The file(s) to process. If no file is specified, sed reads from standard input.

Examples

Replacing Text

To replace text in a file, you can use the s (substitute) command:

sed 's/old-text/new-text/' file.txt

This command replaces the first occurrence of old-text with new-text in each line of file.txt.

To replace all occurrences of old-text with new-text in each line, add the g (global) flag:

sed 's/old-text/new-text/g' file.txt

Editing a File In-Place

To edit a file in place, use the -i option:

sed -i 's/old-text/new-text/g' file.txt

This command replaces all occurrences of old-text with new-text in file.txt and saves the changes to the same file.

Using Regular Expressions

You can use regular expressions for more complex text manipulations. For example, to remove digits from a file:

sed 's/[0-9]//g' file.txt

This command removes all digits from file.txt.

Deleting Lines

To delete lines that match a pattern, use the d command:

sed '/pattern/d' file.txt

This command deletes all lines containing pattern in file.txt.

Printing Specific Lines

To print specific lines of a file, use the -n option combined with the p (print) command:

sed -n '5p' file.txt

This command prints the fifth line of file.txt.

To print a range of lines:

sed -n '5,10p' file.txt

This command prints lines 5 to 10 of file.txt.

Advanced Usage

Applying Multiple Commands

To apply multiple sed commands, use the -e option or separate commands with a semicolon:

sed -e 's/old-text/new-text/g' -e 's/another-old-text/another-new-text/g' file.txt

Or:

sed 's/old-text/new-text/g; s/another-old-text/another-new-text/g' file.txt

Using Files for Commands

To use a file containing sed commands, use the -f option:

sed -f commands.sed file.txt

The commands.sed file might contain:

s/old-text/new-text/g
s/another-old-text/another-new-text/g

Inserting and Appending Lines

To insert text before a line, use the i command:

sed '/pattern/i\new line of text' file.txt

To append text after a line, use the a command:

sed '/pattern/a\new line of text' file.txt

Changing Lines

To replace a line that matches a pattern with new text, use the c command:

sed '/pattern/c\new line of text' file.txt

Practical Use Cases

Batch File Renaming

To rename files by replacing text in their names:

for file in *.txt; do
  new_file=$(echo "$file" | sed 's/old-text/new-text/')
  mv "$file" "$new_file"
done

Configuration File Editing

To change settings in configuration files:

sed -i 's/setting1=value1/setting1=value2/' config.cfg

Log File Processing

To extract and process log entries:

sed -n '/ERROR/p' logfile.log > error.log

Summary

The sed command is a versatile tool for text manipulation in Unix and Linux, capable of performing a wide range of operations from simple text substitutions to complex data transformations. Understanding and utilizing its powerful features can greatly enhance your ability to process and manipulate text files efficiently.

help

Last updated