Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux Sed Command: Edit File Text

The sed (stream editor) command is a powerful text processing tool used in Linux and Unix-like systems for filtering and transforming text. In this tutorial, we will cover the basics of using the sed command, including syntax, operations, and examples.

  • Basic syntax:

The basic syntax of the sed command is:

sed [options] 'command' input_file
  • options: Optional flags that modify the behavior of the sed command.
  • command: The operation or series of operations to be performed on the input text.
  • input_file: The file containing the text to be processed.
  • Common operations:

The sed command supports various operations, such as substitution, deletion, insertion, and more. Here are some common operations:

  • Substitution (s): Replaces occurrences of a pattern with a given string.

    sed 's/pattern/replacement/' input_file
    
  • Deletion (d): Removes lines that match a given pattern.

    sed '/pattern/d' input_file
    
  • Insertion (i): Inserts text before lines that match a given pattern.

    sed '/pattern/i\text_to_insert' input_file
    
  • Append (a): Appends text after lines that match a given pattern.

    sed '/pattern/a\text_to_append' input_file
    
  • Examples:
  • Replace the first occurrence of the word "apple" with "orange" in each line:

    sed 's/apple/orange/' input_file
    
  • Replace all occurrences of the word "apple" with "orange" in each line:

    sed 's/apple/orange/g' input_file
    
  • Replace all occurrences of the word "apple" with "orange" only on lines containing the word "fruit":

    sed '/fruit/s/apple/orange/g' input_file
    
  • Delete all lines containing the word "apple":

    sed '/apple/d' input_file
    
  • Insert the text "Fruits:" before lines containing the word "apple":

    sed '/apple/i\Fruits:' input_file
    
  • Append the text "(fruit)" after lines containing the word "apple":

    sed '/apple/a\(fruit)' input_file
    
  • Saving changes:

By default, the sed command prints the modified text to the standard output without altering the input file. To save the changes to the input file, use the -i option:

sed -i 's/apple/orange/' input_file

Alternatively, you can redirect the output to a new file:

sed 's/apple/orange/' input_file > output_file

In this tutorial, we've covered the basics of the sed command in Linux, including syntax, common operations, and examples. The sed command is a powerful tool for text processing and manipulation, allowing you to perform complex transformations with ease. For more information and advanced options, consult the man page by running man sed.

  1. How to use sed for text editing in Unix-like systems: sed (stream editor) is a powerful tool for text processing. Use it in the terminal to edit text streams.

    sed 's/pattern/replacement/' filename
    
  2. Basic text substitutions with sed in Linux: Perform basic text substitutions. Example:

    echo "Hello, world!" | sed 's/world/there/'
    
  3. Advanced pattern matching with sed: Use regular expressions for advanced pattern matching with sed. Example:

    echo "123 apples" | sed 's/[0-9]\+ //'
    
  4. In-place editing with sed on Linux: Edit files in-place with sed. Example:

    sed -i 's/old/new/' filename
    
  5. sed for search and replace in multiple files: Search and replace in multiple files. Example:

    sed -i 's/foo/bar/' file1.txt file2.txt
    
  6. Using sed to delete lines or patterns in a file: Delete lines containing a specific pattern. Example:

    sed '/pattern/d' filename
    
  7. sed scripting and automation in Linux: Create sed scripts for complex text transformations. Save a script in a file (e.g., myscript.sed) and run:

    sed -f myscript.sed input.txt
    
  8. Combining grep and sed for text manipulation in Linux: Combine grep and sed to filter and edit text. Example:

    grep 'error' logfile | sed 's/.*: //'