Sunday, January 12, 2025
HomeComputer ScienceSed Command in Linux/Unix with examples

Sed Command in Linux/Unix with examples

The sed command in Linux/Unix stands for stream editor. It is used for text manipulation and performs basic text transformations on an input stream (a file or input from a pipeline). You can use sed for tasks like search and replace, text substitution, deleting lines, and more.

Basic Syntax of sed:

bash
sed [options] 'command' input_file
  • command: The operation to be performed on the text (such as search and replace, deletion, etc.).
  • input_file: The file to operate on (if omitted, sed reads from standard input).
  • [options]: Various options like -i (for in-place editing), -n (to suppress automatic output), etc.

Commonly Used sed Commands and Examples:

1. Substitute/Replace Text (s)

The most common use of sed is to substitute or replace text.

  • Syntax: s/pattern/replacement/flags

Example:

bash
# Replace the first occurrence of "apple" with "orange"
sed 's/apple/orange/' input.txt

In this case, sed replaces the first occurrence of “apple” on each line with “orange.”

Flags for Substitution:

  • g: Replace all occurrences in the line (not just the first one).
  • i: Perform a case-insensitive search.
  • n: Show only the lines where a substitution was made.

Example with g flag (global replacement):

bash
# Replace all occurrences of "apple" with "orange"
sed 's/apple/orange/g' input.txt

2. In-Place Editing (-i option)

You can edit a file directly (without printing the result to the screen) using the -i option.

See also  Types of Registers in Computer Organization

Example:

bash
# Replace all occurrences of "apple" with "orange" in input.txt
sed -i 's/apple/orange/g' input.txt

This will directly modify the input.txt file.

3. Delete Lines (d)

The d command in sed deletes lines that match a pattern or meet certain conditions.

  • Syntax: sed '/pattern/d'

Example: Delete all lines containing the word “apple”:

bash
sed '/apple/d' input.txt

This will remove all lines that contain “apple” from the file.

4. Print Specific Lines (p and -n option)

By default, sed prints all lines of the file. If you want to print only specific lines, you can use the -n option and the p command.

Example: Print lines 2 to 4:

bash
sed -n '2,4p' input.txt

This will print only lines 2, 3, and 4 from the file.

5. Insert Text Before/After a Line (i, a)

You can insert text before or after a specific line using the i (insert) and a (append) commands.

  • Syntax:
    • i: Insert text before a line.
    • a: Append text after a line.

Example: Insert “This is a new line.” before line 3:

bash
sed '3i\This is a new line.' input.txt

Example: Append “End of file.” after line 5:

bash
sed '5a\End of file.' input.txt

6. Change a Line (c)

The c command is used to change an entire line.

  • Syntax: sed 'line_number c\replacement_text'

Example: Change line 3 to “This is the changed line”:

bash
sed '3c\This is the changed line' input.txt

7. Search and Replace with a Regular Expression

You can use regular expressions (regex) in sed to match patterns more flexibly.

See also  What Is Computer Science Degree?

Example: Replace all words that start with “a” with “fruit”:

bash
sed 's/\ba\w*/fruit/g' input.txt
  • \b represents a word boundary.
  • \w* matches any word starting with the letter “a”.

8. Multiple Commands

You can use multiple sed commands on the same line by separating them with a semicolon or using -e option for each command.

Example: Delete lines containing “apple” and replace “banana” with “fruit”:

bash
sed -e '/apple/d' -e 's/banana/fruit/g' input.txt

9. Print the Line Number Along with the Line

You can print the line numbers of matching lines using =.

Example: Print the line number and content of lines containing “apple”:

bash
sed -n '/apple/=' input.txt

10. Using sed with Pipes

You can use sed in combination with other commands by piping input to it.

See also  How to Rename Directory in Linux System

Example: Using echo and piping it to sed:

bash
echo "apple orange banana" | sed 's/apple/fruit/'

This will output: fruit orange banana

Summary of Key sed Commands:

Command Description
s/pattern/replacement/ Substitutes text (search and replace)
-i Edits file in-place (modifies the file directly)
/pattern/d Deletes lines matching the pattern
-n Suppresses automatic printing, used with p for specific printing
i Inserts text before a line
a Appends text after a line
c Changes an entire line
= Prints the line number along with the line
-e Allows multiple commands in a single sed invocation

Conclusion:

The sed command is a powerful tool for text manipulation in Linux/Unix systems. It’s commonly used for tasks like searching and replacing text, deleting lines, and modifying files. With its support for regular expressions, sed becomes even more versatile for complex text processing tasks.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x