Interview Kickstart has enabled over 3500 engineers to uplevel.
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.
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.
There are three parts of the sed command syntax.
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 are both command-line utilities in Linux that work with text. However, there are some differences between the two:
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.
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.
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.
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.
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.
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.
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.
In this example, we delete the first line, last line, a range of lines, and a line with a particular pattern.
Note that in each of these cases, only the lines deleted are not printed. All other lines are printed.
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.
Here are some things to keep in mind when using variables in the sed command in Linux:
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.
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!
Attend our webinar on
"How to nail your next tech interview" and learn