• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

sed Command Examples in Linux

by admin

The sed or stream editor command is a program that you can use to modify text files according to various parameters. The sed command can also be used for global search and replace actions. Some of the common command options and their uses are given in the following table.

Option Description
-d Delete the lines that match a specific pattern or line number.
-n,p Print only the lines that contain the pattern.
s Substitute the first occurrence of the string in the file.
S,g Globally substitute the original string with the replacement string for each occurrence in the file.

Syntax

The general syntax of the sed command is:

# sed {'option/address/ action'} {file names}

Addresses tell sed to act only on certain lines or to act only on text that matches a given regular expression pattern. They are optional. Addresses are followed by the action to be performed when a match is found. The last argument is the name of the input file. The option, address, and action parameters are typically enclosed within single quotation marks.

sed Command Examples

1. Replace the first occurrence of a regular expression in each line of a file, and print the result:

# sed 's/regular_expression/replace/' filename

2. Replace all occurrences of an extended regular expression in a file, and print the result:

# sed -r 's/regular_expression/replace/g' filename

3. Replace all occurrences of a string in a file, overwriting the file (i.e. in-place):

# sed -i 's/find/replace/g' filename

4. Replace only on lines matching the line pattern:

# sed '/line_pattern/s/find/replace/' filename

5. Delete lines matching the line pattern:

# sed '/line_pattern/d' filename

6. Print the first 11 lines of a file:

# sed 11q filename

7. Apply multiple find-replace expressions to a file:

# sed -e 's/find/replace/' -e 's/find/replace/' filename

8. Replace separator `/` by any other character not used in the find or replace patterns, e.g. `#`:

# sed 's#find#replace#' filename

Filed Under: Linux

Some more articles you might also be interested in …

  1. nohup Command Options in Linux
  2. How to Disable IPv6 on Ubuntu 22.04 LTS Jammy Jellyfish
  3. How to debug systemd boot process in CentOS/RHEL 7 and 8
  4. After Reboot RAID1 Disk Gets Removed/Out of Sync (CentOS/RHEL)
  5. kjv Command Examples in Linux
  6. Ubuntu Linux – OpenShot Video Editor
  7. btrbk: command not found
  8. CentOS / RHEL 7 : How to boot into Rescue Mode or Emergency Mode
  9. CentOS / RHEL 6,7 : How to recover an interrupted yum package installation
  10. Unable to ssh to server after integration into Active Directory (AD) Domain [CentOS/RHEL 7]

You May Also Like

Primary Sidebar

Recent Posts

  • nixos-rebuild Command Examples in Linux
  • nixos-option: Command Examples in Linux
  • nixos-container : Command Examples in Linux
  • nitrogen Command Examples in Linux

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright