What are important Sed Interview Questions ?

Sed Interview Questions :

In my previous article i have explained about Sed command with real examples.  This article gives you brief idea about Sed Interview Questions .Sed stands for Stream Editor and I have already explained about it in my previous article. There are lot of Interviews where interviewer will ask questions on Sed command. This article gives you idea about most important Sed Interview Questions with its answers.

Question 1 : What Do you mean by Sed ?

Answer :

1.”Sed” stands for Stream editor.

2.Sed is a non-interactive editor, written by the late Lee E. McMahon in 1973 or 1974.

3.Instead of altering a file interactively by moving the cursor on the screen (as with a word processor), the user sends a script of editing instructions to sed, plus the name of the file to edit (or the text to be edited may come as output from a pipe). In this sense, sed works like a filter — deleting, inserting and changing characters, words, and lines of text. Its range of activity goes from small, simple changes to very complex ones.

3. Sed reads its input from stdin (Unix shorthand for “standard input,” i.e., the console) or from files (or both), and sends the results to stdout (“standard output,” normally the console or screen). Most people use sed first for its substitution features.

4.Sed is often used as a find-and-replace tool.

sed ‘s/Amit/Pradnya/A’ oldfile >newfile

will replace every occurrence of “Amit” with the word “Pradnya”, wherever it occurs in the file. The “find” portion is a regular expression (“RE”), which can be a simple word or may contain special characters to allow greater flexibility.

Question 2 : Explain about different versions of Sed command? (100 % asked Sed Interview Questions )

Answer :

Note: “Free” does not mean “public domain” nor does it necessarily mean you will never be charged for it. All versions of sed in this section except the CP/M versions are based on the GNU general public license and are “free software” by that standard. This means you can get the source code and develop it further.

At the URLs listed in this category, sed binaries or source code can be downloaded and used without fees or license payments.

Question 3 : Explain the Syntax of Sed Command. (100 % asked Sed Interview Questions )

Answer :

Sed Command in unix which will help to modify the files in unix.Sed command in unix is basically used to replace and find the text but  sed command also used to do many things apart from replacing the text.It is used to search and replace a string and it is multi-purpose filter string.


Sed “s/old string/new string/g”;

s:  means substitution

g:  means every line all occurrences

without g: means every line 1st occurrences

Question 4 : Can you create a file using SED command? If Yes How ?

Answer :

No .We cant Create a file using Sed Command.

Question 5 :  Consider the following File.


a b c complexsql d e complexsql f complexsql

complexsql a b complexsql

complexsql abc complexsql











How to replace word ‘complexsql’ with word ‘Amit’.(100 % asked Sed Interview Questions )

Answer :

Command 1 :

sed “s/complexsql/Amit/g” Sed_File;

The above command replaces the word complexsql to Amit.Here s stands for substitution and g stands for every line.

Command 2 :

 sed “s/complexsql/Amit/” Sed_File;

The above command directly substitudes the word complexsql to Amit.

Command 3 :

 sed “s/complexsql/Amit/gi” Sed_File;

The above command adds the new line before word complexsql and replaces word complexsql to Amit.

These are above 3 ways to replace the word named ‘complexsql’ with ‘Amit’.

Question 6 : What is the command to replace the word tech with amit by checking case sensitivity?

Answer :

Command 1 :

 sed “s/^tech$/Amit/g” Sed_File ;

It replaces exactly “tech” with Amit by checking uppercase and lowercase letters.

Command 2 :

sed “s/\<tech\>/Amit/g” Sed_File ;

It replaces exactly “tech” with Amit.This is another syntax for replacing the words by checking case sensitivity.

Question 7 : Write a command to delete first line and last line from Sed_file.txt.(100 % asked Sed Interview Questions )

Answer :

sed command to delete first line

$ sed ‘1d’ Sed_File.txt

sed command to delete last line

$ sed ‘$d’ Sed_File.txt

Question 8 : How Do I Insert A Newline Into The Rhs Of A Substitution?

Answer :

Several versions of sed permit ‘n’ to be typed directly into the RHS, which is then converted to a newline on output: ssed, gsed302a+, gsed103 (with the -x switch), sed15+, sedmod, and UnixDOS sed. The easiest solution is to use one of these versions.

For other versions of sed, try one of the following:

(a) If typing the sed script from a Bourne shell, use one backslash “” if the script uses ‘single quotes’ or two backslashes “” if the script requires “double quotes”. In the example below, note that the leading ‘>’ on the 2nd line is generated by the shell to prompt the user for more input. The user types in slash, single-quote, and then ENTER to terminate the command:

[sh-prompt]$ echo twolines | sed ‘s/two/& new
two new

(b) Use a script file with one backslash ” in the script, immediately followed by a newline. This will embed a newline into the “replace” portion. Example:

sed -f newline.sed files

# newline.sed
s/twolines/two new

Some versions of sed may not need the trailing backslash. If so, remove it.

(c) Insert an unused character and pipe the output through tr:

echo twolines | sed ‘s/two/& new=/’ | tr “=” “n”   # produces
two new

(d) Use the “G” command:

G appends a newline, plus the contents of the hold space to the end of the pattern space. If the hold space is empty, a newline is appended anyway. The newline is stored in the pattern space as “n” where it can be addressed by grouping “(…)” and moved in the RHS. Thus, to change the “twolines” example used earlier, the following script will work:

sed ‘/twolines/{G;s/(two)(lines)(n)/132/;}’

(e) Inserting full lines, not breaking lines up:

If one is not changing lines but only inserting complete lines before or after a pattern, the procedure is much easier. Use the “i” (insert) or “a” (append) command, making the alterations by an external script. To insert “This line is new” BEFORE each line matching a regex:

/RE/i This line is new   # HHsed, sedmod, gsed 3.02a
/RE/{x;s/$/This line is new/;G;}   # other seds

The two examples above are intended as “one-line” commands entered from the console. If using a sed script, “i” immediately followed by a literal newline will work on all versions of sed. Furthermore, the command “s/$/This line is new/” will only work if the hold space is already empty (which it is by default).

To append “This line is new” AFTER each line matching a regex:

/RE/a This line is new   # HHsed, sedmod, gsed 3.02a
/RE/{G;s/$/This line is new/;}  # other seds

To append 2 blank lines after each line matching a regex:

/RE/{G;G;}      # assumes the hold space is empty

To replace each line matching a regex with 5 blank lines:

/RE/{s/.*//;G;G;G;G;}# assumes the hold space is empty

(f) Use the “y///” command if possible:

On some Unix versions of sed (not GNU sed!), though the s/// command won’t accept ‘n’ in the RHS, the y/// command does. If your Unix sed supports it, a newline after “aaa” can be inserted this way (which is not portable to GNU sed or other seds):

s/aaa/&~/; y/~/n/; #assuming no other ‘~’ is on the line!

Question 9 : How to remove blank lines in specific file by using Sed Command?

Answer :

One of the common usage of SED command is to delete empty lines or remove blank lines from files, without opening them. Unix sysadmin often use SED command in Linux to clean up files by removing empty lines. Following example of UNIX SED command will show you how to remove blank lines using SED in unix:

Command :

sed ‘/^$/d’ Sed_File.txt;

here text between /–/ is a regular expression for matching matching empty lines i.e. “^$”. Since “^” denote start of line and“$” denote end of line, “^$” means empty lines.

Question 10 :  How to print only blank line using Sed Command?

Answer :

Following command is used to check and print blank line using Sed Command :

sed -n ‘/^$/p’ Sed_file.txt;

Question 11 : How to replace empty lines with ***Blank lines*** keyword ?

Answer :

Sed “s/^$/***Blank Lines***/g” Sed_File;

It replaces all Empty Lines with “***Blank Lines***”

Question 12 : Write a command to delete word complexsql from the file.

Answer :

sed “s/\<complexsql\>//gi” Sed_File ;

It deletes “complexsql” word from the file

Sed Interview Questions

Question 13 : How to print first and last line of the file using Sed Command?

Answer :

To Print First and Last Line using Sed Command following command is used

Command :

sed  -n ‘1p’ Test_file.txt

Question 14 : Write a command to display 3rd and 5th line from specific file.

Answer :

Command :

sed -n “3,5p” Sed_File;

It Prints 3rd to 5th lines

Question 15 :How Do I Convert Files With Toggle Characters, Like +this+, To Look Like [i]this[/i]?

Answer :

Input files, especially message-oriented text files, often contain toggle characters for emphasis, like ~this~, this, or =this=. Sed can make the same input pattern produce alternating output each time it is encountered. Typical needs might be to generate HMTL codes or print codes for boldface, italic, or underscore. This script accomodates multiple occurrences of the toggle pattern on the same line, as well as cases where the pattern starts on one line and finishes several lines later, even at the end of the file:

# sed script to convert +this+ to [i]this[/i]
/+/{ x;  # If “+” is found, switch hold and pattern space
/^ON/{ # If “ON” is in the (former) hold space, then ..
s///;     # .. delete it
x;      # .. switch hold space and pattern space back
s|+|[/i]|; # .. turn the next “+” into “[/i]”
ba;       # .. jump back to label :a and start over
s/^/ON/;  # Else, “ON” was not in the hold space; create it
x;            # Switch hold space and pattern space
s|+|[i]|;      # Turn the first “+” into “[i]”
ba;         # Branch to label :a to find another pattern
#—end of script—

This script uses the hold space to create a “flag” to indicate whether the toggle is ON or not. We have added remarks to illustrate the script logic, but in most versions of sed remarks are not permitted after ‘b’ranch commands or labels.

If you are sure that the +toggle+ characters never cross line boundaries (i.e., never begin on one line and end on another), this script can be reduced to one line:


If your toggle pattern contains regex metacharacters (such as ‘*’ or perhaps ‘+’ or ‘?’), remember to quote them with backslashes.

Question 16: Write a command to remove the first 10 lines from a file?

Answer :

Following command is used to remove first 10 lines from the file :

sed ‘1,10 d’ < filename

Question 17 : Write a command to duplicate each line in a file?

Answer :

Following command is used to duplicate each line in a file :

sed ‘p’ < filename

Question 18 : Write a command to duplicate empty lines in a file?


Following command is used to duplicate empty lines in a file :
sed ‘/^$/ p’ < filename

Question 19 : Write a sed command to print the lines that do not contain the word “complex”?

Answer :

Following command is used to print line which does not have word named “complex”.

sed -n ‘/complex/!p’ < filename

Question 20 : How to delete 3rd and 5th line from the file without using unix editor.


Following Sed command is used to delete the 3rd and 5th line from the file.

$ sed ‘3,5d’ Sed_File ;

It Deletes 3rd to 5th Lines from a file

These are above some most important Sed Interview Questions. Hope you like this article on Sed Interview Questions. If you like this article on Sed Interview Questions dont forget to comment it in to comment section.