Our June 2021 cohorts are filling up quickly. Join our free webinar to Uplevel your career
About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Sed Command in Linux/Unix With Examples

The SED command in Linux stands for Stream EDitor and is helpful for a myriad of frequently needed operations in text files and streams. Sed helps in operations like selecting the text, substituting text, modifying an original file, adding lines to text, or deleting lines from the text.

If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation ebook to get interview-ready!

Having trained over 11,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!

At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.

Want to nail your next tech interview? Sign up for our FREE Webinar.

It’s quicker and easier to just learn about the sed command, but it takes a long time and practice to master its usage. This article focuses on the sed command as used in Linux/Unix and examples that demonstrate its usage.

We’ll cover:

  • What Is the Sed Command in Linux?
  • Sed Command Syntax in Linux
  • Awk and Sed Command In Linux
  • Sed Command in Linux With Examples
  • How to Use Variable in Sed Command in Linux
  • FAQs on Sed Command in Linux/Unix

What Is the Sed Command in Linux?

Linux sed command is a stream editor and acts as a text editor with no interactive interface. It works on piped input or text files based on the instructions we give it to follow as it goes through the text. We can manipulate text in streams and files using sed from the command line in Bash and other command-line shells. 

Sed Command Syntax in Linux

There are three parts of the sed command syntax.

  1. Options control the output of the Linux command
  2. Script contains a list of Linux commands to run
  3. File name (with extension) represents the file on which you’re using the sed command

We can run a sed command without any option. We can also run it without a filename, in which case, the script works on the std input data.

sed OPTIONS [SCRIPT] [INPUTFILENAME]

Awk and Sed Command In Linux

Awk and sed are both command-line utilities in Linux that work with text. However, there are some differences between the two:

SED

AWK

Works to parse and transform text in a compact and simple language.

Helps in text processing and writing potent programs in the form of statements.

Simple, limited, and less powerful than awk.

Complex, sophisticated, versatile, and more powerful than sed.

sed Command in Linux with Examples

Let us first create a file we’ll be working with. This file, ik.txt, forms the basis of all the examples. Note that sed doesn’t alter the original file, so all changes will show in the output, but the original file remains the same for each command we successively run.

Example 1

In this example, we replace the first instance of a pattern (abc) with another pattern (def) using s for substitution. Don’t forget to add / at the end to complete the script as shown below:

Notice that only the first instance of abc has been substituted with def.

Example 2

In this example, we replace the third instance of a pattern (abc) with another pattern (z) using s for substitution. You can replace the nth instance of a pattern similarly by replacing 3 with the number n in the example below:

Note that only the third instance of abc has been replaced by z.

Example 3

In this example, we do two types of global replacements. In a global replacement, we replace all the instances of a pattern (abc) with another pattern (XYZ) using s for substitution. 

We do a global replacement by adding /g for global. And to do a global replacement starting from the n-th instance of the pattern, you can use /ng. We can see both the examples below, where we have taken n to be 3 for the second example:

Notice that with /g, all instances of “abc” have been replaced by “XYZ,” while with /3g, all instances starting from the third instance have been replaced.

Example 4

In this example, we print the altered line twice using /p for printing the altered line:

Notice that only the line with abc that was replaced is printed twice, after which the rest of the following lines are printed.

Example 5

In this example, we only print the altered line using the -n option (for disabling automatic printing, so anything not asked to print is not printed) along with /p, which prints:

Notice that only the altered line is printed.

Example 6

In this example, we delete the first line, last line, a range of lines, and a line with a particular pattern. 

  • To delete the nth line, you can use ‘nd’ as seen in the first example below for deleting the first line where n equals one. 
  • To delete the last line, you use ‘$d’.
  • To delete a range of lines from line number p to line number q, you use ‘p,qd’, as shown in the third example below.
  • To delete the first line with a specific pattern, use ‘/pattern/d’ as in the last example where the pattern is abc

Note that in each of these cases, only the lines deleted are not printed. All other lines are printed.

Example 7

In this example, we substitute a pattern (a) with another pattern (z) only if present in a particular line number n by using ‘n s/oldpattern/newpattern/’. 

We end with an example where we replace the old pattern with the new pattern in a range of line numbers from line numbers p to q using ‘p,q s/oldpattern/newpattern/’ as shown below:

Note that in each of these cases, only the line numbers or ranges mentioned are changed. 

How to Use a Variable in the Sed Command in Linux

Here are some things to keep in mind when using variables in the sed command in Linux:

  • The shell expands variables, and if we use single quotes for strings, the shell treats its contents literally. Use double quotes to avoid this, so the shell expands the variables.
  • If the replacement contains /, then use a different separator, not / itself.
  • If you don’t want to expand it, escape the $ in the pattern.

FAQs on Sed Command in Linux/Unix

Q1. What is the sed command in Linux?

The sed command or the stream editor command in UNIX or Linux, is a stream editor that’s like a text editor with no interface. It works on piped input or text files based on the instructions we give and can manipulate text in streams and files.

Q2. What does sed do in Unix?

Stream EDitor or SED command, is a Unix utility to help us parse and transform text in a simple and compact manner.

Q3. What is the main difference between Awk and Sed Command In Linux?

Sed is a command utility in Unix that searches, filters, and processes text in a stream of characters. Awk has more sophisticated programming constructs like while, do/while, if/else, etc., as compared to sed. The key difference between the two is that awk is more powerful and robust than sed.

Q4. What is the difference between the grep and sed command in Linux?

Grep and sed are both line-based utilities in Linux. Grep is a search utility we mainly use to return lines from one or more files that match a specific search term. In contrast, we usually use sed for string replacement within lines of text.

Q5. Does sed change the original file?

No, sed by default does not alter the original file. It writes to stdout instead. 

Ready to Nail Your Next Coding Interview?

Whether you’re a coding engineer gunning for a software developer or software engineer role, a tech lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

Attend our Free Webinar on How to Nail Your Next Technical Interview

Recommended Posts

All Posts