The Linux Sed command is a powerful text processing tool that allows system administrators to manipulate and transform text files. This cheatsheet provides a comprehensive guide to the Sed command, including its various options, flags, and examples.
The Sed command has several options that can be used to perform different operations. These include:
q: Exit without processing any more commands or input.d: Delete the pattern space immediately after the current cycle.p: Print out the pattern space (to the standard output).a: Append text after a line.i: Insert text before a line.c: Replace the contents of the file with the contents of the pattern space.w: Write the pattern space to the specified file name.D: Delete the first line that matches the pattern.
The Sed command also has several options that can be used to modify its behavior. These include:
-n: Suppress automatic printing of pattern space.-1: Add the contents of script-file to the commands to be executed.e: Execute multiple Sed commands.s: Use extended regular expressions (supports '+', '?', and '*').
The Sed command has several flags that can be used to modify its behavior. These include:
-i: Edit files in place (make backup if SUFFIX supplied).-l: Suppress automatic printing of pattern space.--help: Display a help message and exit.
Here are some basic examples of Sed commands:
# Delete the second input line
sed '2d' file.txt
# Print only the second input line
sed '2p' file.txt
# Replace all occurrences of a string with another string
sed 's/old_string/new_string/g' file.txtThe Sed command can be used to replace text in a file. Here are some examples:
# Replace all occurrences of a string with another string
sed 's/old_string/new_string/g' file.txt
# Replace the first occurrence of a string with another string
sed 's/old_string/new_string/' file.txtThe Sed command can be used to append or prepend text to a file. Here are some examples:
# Append text at the end of each line
sed 's/$/ appended_text/' file.txt
# Prepend text before each line
sed 's/^/prepended_text/' file.txtThe Sed command can be used to delete lines from a file. Here are some examples:
# Delete every 4th line starting with line 6
sed '6~4d' file.txt
# Delete all empty lines
sed '/^$/d' file.txt- The Sed command is a powerful text processing tool that can be used to manipulate and transform text files.
- The
-iflag should be used with caution, as it edits files in place and can potentially cause data loss. - The
--helpflag should be used to display a help message and exit if you are unsure of how to use the Sed command. - It's always a good idea to test your Sed commands on a sample file before running them on a production file.
- Sysxplore: A website that provides tutorials, guides, and resources for system administrators.
- Linux Sed Command: The official documentation for the Linux Sed command.
- Original Tweet: https://twitter.com/i/web/status/1881322615945777273
- Date: 2025-02-20 17:16:42
Description: The infographic, titled "Linux SED Command Cheatsheet," provides a comprehensive overview of the Sed command's functionality. The title is displayed at the top in white text against a black background, accompanied by the website URL "sysxplore.com" and its logo on either side.
Sed Commands
- This section lists various options available for the Sed command, including:
q: Exit without processing any more commands or input.d: Delete the pattern space immediately after the current cycle.p: Print out the pattern space (to the standard output).a: Append text after a line.i: Insert text before a line.c: Replace the contents of the file with the contents of the pattern space.w: Write the pattern space to the specified file name.D: Delete the first line that matches the pattern.
Command Options
- This section explains the different command options available for Sed, including:
-n: Suppress automatic printing of pattern space.-1: Add the contents of script-file to the commands to be executed.e: Execute multiple Sed commands.s: Use extended regular expressions (supports '+', '?', and '*').
Command Flags
- This section lists various flags that can be used with Sed, including:
-i: Edit files in place (make backup if SUFFIX supplied).-l: Suppress automatic printing of pattern space.--help: Display a help message and exit.
Basic Examples
- This section provides examples of basic Sed commands, including:
- Deleting the second input line.
- Printing only the second input line.
- Replacing all occurrences of a string with another string.
Replacing Text
- This section explains how to replace text using Sed, including:
- Replacing all occurrences of a string with another string.
- Replacing the first occurrence of a string with another string.
Appending and Prepending Text
- This section describes how to append or prepend text to a file using Sed, including:
- Appending text at the end of each line.
- Preferring text before each line.
Deleting Lines
- This section explains how to delete lines from a file using Sed, including:
- Deleting every 4th line starting with line 6.
- Deleting all empty lines.
Overall, this infographic provides a comprehensive guide to the Sed command and its various options, flags, and examples. It is an essential resource for anyone looking to learn more about this powerful text processing tool.
Last updated: 2025-02-20 17:16:42