Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

README.md

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 options, flags, and examples of basic usage.

Technical Content

Sed Commands

The Sed command has several options that can be used to perform various actions on text files. These options 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.
Command Options

The Sed command also has several options that can be used to modify its behavior. These options 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 '*').
Command Flags

The Sed command has several flags that can be used to modify its behavior. These flags 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.
Basic Examples

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.txt
Replacing Text

The 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.txt
Appending and Prepending Text

The 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/$/append_text/' file.txt
  • Prepend text before each line: sed 's/^/prepend_text/' file.txt
Deleting Lines

The 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

Key Takeaways and Best Practices

  • Always use the -n option to suppress automatic printing of pattern space when using Sed commands.
  • Use the -i flag to edit files in place and make backups if necessary.
  • Use extended regular expressions (supports '+', '?', and '*') with the s command option.
  • Test Sed commands on a sample file before running them on a production file.

References

Source

Media

Media 1

media_0 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-25 22:31:38