Skip to main content

Command Palette

Search for a command to run...

sed(Text Editor) command

Published
4 min readView as Markdown
sed(Text Editor) command
X

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.

"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."

sed

The sed command, short for "stream editor," is a powerful text processing tool commonly used in Unix-like operating systems. It operates on text input, line by line, and allows for various types of editing operations such as substitution, deletion, insertion, and more.

The basic syntax of the sed command is as follows:

sed [options] 'command' [input-file]

Here's a breakdown of the components:

  • options: Optional flags that modify the behavior of sed, such as -e for specifying the sed script directly on the command line.

  • 'command': The editing command or set of commands enclosed in single quotes.

  • input-file: Optional. Specifies the file to be processed by sed. If not provided, sed reads from standard input.

The editing commands within the single quotes are specified in the form address + action, where:

  • address: Specifies the lines to which the following action should be applied. It can be a line number, a regular expression, or a range of lines.

  • action: Specifies the operation to be performed on the selected lines, such as substitution (s/.../.../), deletion (d), printing (p), and more.

For example, the sed command with the substitution action (s) is commonly used for search and replace operations. Here's an example that replaces all occurrences of "apple" with "orange" in a file named fruits.txt:

sed 's/apple/orange/g' fruits.txt

This is just a brief introduction to the sed command. It has many advanced features, options, and editing capabilities. To learn more about its usage and available commands, you can refer to the sed manual (man sed).

Examples on how to use sed

  1. Substitute text in a file:

     sed 's/pattern/replacement/' file.txt
    

    This command replaces the first occurrence of "pattern" with "replacement" in the file "file.txt". To replace all occurrences, add the g (global) flag at the end: sed 's/pattern/replacement/g' file.txt.

  2. Substitute text and save changes in-place:

     sed -i 's/pattern/replacement/' file.txt
    

    This command replaces the first occurrence of "pattern" with "replacement" in the file "file.txt" and saves the changes directly to the file.

    The -i option in the sed command stands for "in-place editing." When used with sed, it allows you to modify a file directly, saving the changes to the original file instead of just displaying the modified output on the terminal.

  3. Delete lines from a file matching a pattern:

     sed '/pattern/d' file.txt
    

    This command deletes all lines in "file.txt" that contain the specified "pattern".

  4. Print specific lines from a file:

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

    This command prints lines 5 to 10 from "file.txt". The -n option suppresses automatic printing, and p at the end instructs sed to print the specified lines.
    "Automatic printing" refers to the default behavior of sed to print each line after applying the specified commands, whereas the -n option allows you to suppress this behavior and selectively print specific lines as desired.

  5. Append text after a specific line:

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

    This command finds the first occurrence of "pattern" in "file.txt" and appends "New line of text" immediately after that line.
    The backslash (\) is used to escape the newline character (\n) and indicate that the text "New line of text" is part of the same line as the a command.
    Without the backslash, the newline character would be interpreted as the end of the a command and a syntax error would occur. By escaping the newline character with a backslash, sed treats it as part of the text to be appended rather than as a command delimiter.

  6. Replace text only on specific lines:

     sed '10s/pattern/replacement/' file.txt
    

    This command replaces the first occurrence of "pattern" with "replacement" only on line 10 in "file.txt". To replace on multiple lines, use a range like 5,10.

  7. Delete lines from a file based on line numbers:

     sed '3d;7,10d' file.txt
    

    This command deletes line 3 and lines 7 to 10 from "file.txt". Multiple expressions can be separated by a semicolon.

  8. Insert text before a specific line:

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

    This command finds the first occurrence of "pattern" in "file.txt" and inserts "New line of text" immediately before that line.

  9. Transform text using regular expressions:

     sed 's/\bword\b/replacement/g' file.txt
    

    This command replaces all occurrences of "word" as a whole word with "replacement" in "file.txt". The \b denotes word boundaries.
    \b: This is a regular expression construct representing a word boundary. It matches the position between a word character (such as letters or digits) and a non-word character (such as whitespace or punctuation marks). It ensures that the pattern "word" is matched only as a whole word, not as part of a longer word.

    word: This is the pattern to be matched. It represents the word you want to find and replace.

    \b: This is another word boundary, indicating the end of the pattern.

  10. Execute multiple editing commands:

    sed -e 's/pattern1/replacement1/' -e 's/pattern2/replacement2/' file.txt
    

    This command applies multiple editing commands sequentially. In this example, it replaces "pattern1" with "replacement1" and then replaces "pattern2" with "replacement2" in "file.txt".

More from this blog

PERSONAL BLOG

110 posts

Use the search button to search for a specific topic or keyword *all posts are updated on the go*