Sed (Stream Editor) Interview Questions & Answers

Sed (Stream Editor) Interview Questions

Finding another job can be so cumbersome that it can turn into a job itself. Prepare well for the job interviews to get your dream job. Here's our recommendation on the important things to need to prepare for the job interview to achieve your career goals in an easy way. Sed (Stream Editor) is a unix utility, which parses and transforms the text. It is still being used in the operating systems. The script to run can be given in a command line or through a file. It reads the text line by line. Certifications exams  are available to increase competency. Follow our Wisdomjobs page for Sed (Stream Editor) job interview questions and answers page to get through your job interview successfully in first attempt.

Sed (Stream Editor) Interview Questions And Answers

Sed (Stream Editor) Interview Questions
    1. Question 1. Write A Command To Replace The Word "bad" With "good" In File?

      Answer :

      sed s/bad/good/ < filename

    2. Question 2. Write A Command To Replace The Word "bad" With "good" Globally In A File?

      Answer :

      sed s/bad/good/g < filename

    3. Question 3. Write A Command To Replace The Character '/' With ',' In A File?

      Answer :

      sed 's///,/' < filename
      sed 's|/|,|' < filename

    4. Question 4. Write A Command To Replace The Word "apple" With "(apple)" In A File?

      Answer :

      sed s/apple/(&)/ < filename

    5. Question 5. Write A Command To Switch The Two Consecutive Words "apple" And "mango" In A File?

      Answer :

      sed 's/(apple) (mango)/2 1/' < filename

    6. Question 6. Write A Command To Replace The Second Occurrence Of The Word "bat" With "ball" In A File?

      Answer :

      sed 's/bat/ball/2' < filename

    7. Question 7. Write A Command To Remove All The Occurrences Of The Word "jhon" Except The First One In A Line With In The Entire File?

      Answer :

      sed 's/jhon//2g' < filename

    8. Question 8. Write A Command To Remove The First Number On Line 5 In File?

      Answer :

      sed '5 s/[0-9][0-9]*//' < filename

    9. Question 9. Write A Command To Remove The First Number On All Lines That Start With "@"?

      Answer :

      sed ',^@, s/[0-9][0-9]*//' < filename

    10. Question 10. Write A Command To Replace The Word "gum" With "drum" In The First 100 Lines Of A File?

      Answer :

      sed '1,00 s/gum/drum/' < filename

    11. Question 11. Write A Command To Replace The Word "lite" With "light" From 100th Line To Last Line In A File?

      Answer :

      sed '100,$ s/lite/light/' < filename

    12. Question 12. Write A Command To Remove The First 10 Lines From A File?

      Answer :

      sed '1,10 d' < filename

    13. Question 13. Write A Command To Duplicate Each Line In A File?

      Answer :

      sed 'p' < filename

    14. Question 14. Write A Command To Duplicate Empty Lines In A File?

      Answer :

      sed '/^$/ p' < filename

    15. Question 15. Write A Sed Command To Print The Lines That Do Not Contain The Word "run"?

      Answer :

      sed -n '/run/!p' < filename 

    16. Question 16. What Is Sed?

      Answer :

      "sed" stands for Stream EDitor. Sed is a non-interactive editor, written by the late Lee E. McMahon in 1973 or 1974. 

      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.

      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. Sed is often used as a find-and-replace tool.

           sed 's/Glenn/Harold/g' oldfile >newfile

      will replace every occurrence of "Glenn" with the word "Harold", 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 (for example, to prevent "Glenn" from also matching "Glennon").

      My very first use of sed was to add 8 spaces to the left side of a file, so when I printed it, the printing wouldn't begin at the absolute left edge of a piece of paper.

           sed 's/^/        /' myfile >newfile   # my first sed script
           sed 's/^/        /' myfile | lp       # my next sed script

      Then I learned that sed could display only one paragraph of a file, beginning at the phrase "and where it came" and ending at the phrase "for all people". My script looked like this:

           sed -n '/and where it came/,/for all people/p' myfile

      Sed's normal behavior is to print (i.e., display or show on screen) the entire file, including the parts that haven't been altered, unless you use the -n switch. The "-n" stands for "no output". This switch is almost always used in conjunction with a 'p' command somewhere, which says to print only the sections of the file that have been specified. The -n switch with the 'p' command allow for parts of a file to be printed (i.e., sent to the console).

      Next, I found that sed could show me only (say) lines 12-18 of a file and not show me the rest. This was very handy when I needed to review only part of a long file and I didn't want to alter it.

           # the 'p' stands for print

           sed -n 12,18p myfile

      Likewise, sed could show me everything else BUT those particular lines, without physically changing the file on the disk:

           # the 'd' stands for delete
           sed 12,18d myfile

      Sed could also double-space my single-spaced file when it came time to print it:

           sed G myfile >newfile

      If you have many editing commands (for deleting, adding, substituting, etc.) which might take up several lines, those commands can be put into a separate file and all of the commands in the file applied to file being edited:

           #  'script.sed' is the file of commands
           # 'myfile' is the file being changed
           sed -f script.sed myfile  # 'script.sed' is the file of commands

    17. Question 17. What Versions Of Sed Are There, And Where Can I Get Them?

      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.

    18. Question 18. 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!

    19. Question 19. How Do I Represent Control-codes Or Nonprintable Characters?

      Answer :

      Several versions of sed support the notation xHH, where "HH" are two hex digits, 00-FF: ssed, GNU sed v3.02.80 and above, GNU sed v1.03, sed16 and sed15 (HHsed). Try to use one of those versions.

      Sed is not intended to process binary or object code, and files which contain nulls (0x00) will usually generate errors in most versions of sed. The latest versions of GNU sed and ssed are an exception; they permit nulls in the input files and also in regexes.

      On Unix platforms, the 'echo' command may allow insertion of octal or hex values, e.g., `echo "nnn"` or `echo -n "nnn"`. The echo command may also support syntax like '\b' or '\t' for backspace or tab characters. Check the man pages to see what syntax your version of echo supports. Some versions support the following:

           # replace 0x1A (32 octal) with ASCII letters
           sed 's/'`echo "32"`'/Ctrl-Z/g'

           # note the 3 backslashes in the command below
           sed "s/.`echo \b`//g"

    20. Question 20. 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.

    21. Question 21. How Do I Perform A Case-insensitive Search?

      Answer :

      Several versions of sed support case-insensitive matching: ssed and GNU sed v3.02+ (with I flag after s/// or /regex/); sedmod with the -i switch; and sed16 (which supports both types of switches).

      With other versions of sed, case-insensitive searching is awkward, so people may use awk or perl instead, since these programs have options for case-insensitive searches. In gawk/mawk, use "BEGIN {IGNORECASE=1}" and in perl, "/regex/i". For other seds, here are three solutions:

      Solution 1: convert everything to upper case and search normally

           # sed script, solution 1
           h;          # copy the original line to the hold space
                       # convert the pattern space to solid caps
                       # now we can search for the word "CARLOS"
           /CARLOS/ {
                # add or insert lines. Note: "s/.../.../" will not work
                # here because we are searching a modified pattern
                # space and are not printing the pattern space.
           x;          # get back the original pattern space
                       # the original pattern space will be printed
           #---end of sed script---

      Solution 2: search for both cases

      Often, proper names will either start with all lower-case ("unix"), with an initial capital letter ("Unix") or occur in solid caps ("UNIX"). There may be no need to search for every possibility.

           /UNIX/b match
           /[Uu]nix/b match
      Solution 3: search for all possible cases
           # If you must, search for any possible combination
           /[Ca][Aa][Rr][Ll][Oo][Ss]/ { ... }

      Bear in mind that as the pattern length increases, this solution becomes an order of magnitude slower than the one of Solution 1, at least with some implementations of sed.

    22. Question 22. How Do I Match Only The First Occurrence Of A Pattern?

      Answer :

      (1) The general solution is to use GNU sed or ssed, with one of these range expressions. The first script ("print only the first match") works with any version of sed:

        sed -n '/RE/{p;q;}' file       # print only the first match
        sed '0,/RE/{//d;}' file     # delete only the first match
        sed '0,/RE/s//to_that/' file  # change only the first match

      (2) If you cannot use GNU sed and if you know the pattern will not occur on the first line, this will work:

        sed '1,/RE/{//d;}' file     # delete only the first match
        sed '1,/RE/s//to_that/' file  # change only the first match

      (3) If you cannot use GNU sed and the pattern might occur on the first line, use one of the following commands (credit for short GNU script goes to Donald Bruce Stewart):

      sed '/RE/{x;/Y/!{s/^/Y/;h;d;};x;}' file # delete (one way)
      sed -e '/RE/{d;:a' -e '$!N;$ba' -e '}' file  # delete (another way)
      sed '/RE/{d;:a;N;$ba;}' file # same script, GNU sed
      sed -e '/RE/{s//to_that/;:a' -e '$!N;$!ba' -e '}' file # change

      Still another solution, using a flag in the hold space. This is portable to all seds and works if the pattern is on the first line:

      # sed script to change "foo" to "bar" only on the first occurrence

           #---end of script---

    23. Question 23. How Do I Handle Fixed-length, Columnar Data?

      Answer :

      Sed handles fixed-length fields via (grouping) and backreferences (1, 2, 3 ...). If we have 3 fields of 10, 25, and 9 characters per field, our sed script might look like so:

       s/^(.{10})(.{25})(.{9})/321/;  # Change the fields
              ^^^^^^^^^^^~~~~~~~~~~~==========           #   from 1,2,3 to 3,2,1
               field #1   field #2   field #3

      This is a bit hard to read. By using GNU sed or ssed with the -r switch active, it can look like this:

       s/^(.{10})(.{25})(.{9})/321/;   # Using the -r switch

      To delete a field in sed, use grouping and omit the backreference from the field to be deleted. If the data is long or difficult to work with, use ssed with the -R switch and the /x flag after an s/// command, to insert comments and remarks about the fields.

      For records with many fields, use GNU awk with the FIELDWIDTHS variable set in the top of the script. For example:

      awk 'BEGIN{FIELDWIDTHS = "10 25 9"}; {print $3 $2 $1}' file

      This is much easier to read than a similar sed script, especially if there are more than 5 or 6 fields to manipulate.

    24. Question 24. How Do I Change Only One Section Of A File?

      Answer :

      You can match a range of lines by line number, by regexes (say, all lines between the words "from" and "until"), or by a combination of the two. For multiple substitutions on the same range, put the command(s) between braces {...}.

      For example:

           # replace only between lines 1 and 20
           1,20 s/Johnson/White/g

           # replace everywhere EXCEPT between lines 1 and 20
           1,20 !s/Johnson/White/g

           # replace only between words "from" and "until". Note the
           # use of as word boundary markers in GNU sed.
           /from/,/until/ { s//magenta/g; s//cyan/g; }

           # replace only from the words "ENDNOTES:" to the end of file
           /ENDNOTES:/,$ { s/Schaff/Herzog/g; s/Kraft/Ebbing/g; }

    25. Question 25. How Do I Delete Or Change A Block Of Text If The Block Contains A Certain Regular Expression?

      Answer :

      The following deletes the block between 'start' and 'end' inclusively, if and only if the block contains the string 'regex'. Written by Russell Davies, with additional comments:

           # sed script to delete a block if /regex/ matches inside it
           /start/,/end/ {    # For each line between these block markers..
              /end/!{         #   If we are not at the /end/ marker
                 $!{          #     nor the last line of the file,
                    N;        #     add the Next line to the pattern space
                 }            #   and branch (loop back) to the :t label.
              }               # This line matches the /end/ marker.
              /regex/d;       # If /regex/ matches, delete the block.
           }                  # Otherwise, the block will be printed.
           #---end of script---

      Note: When the script above reaches /regex/, the entire multi-line block is in the pattern space. To replace items inside the block, use "s///". To change the entire block, use the 'c' (change) command:

           1: This will replace the entire block
           2: with these two lines of text.

    26. Question 26. How Do I Locate A Paragraph Of Text If The Paragraph Contains A Certain Regular Expression?

      Answer :

      Assume that paragraphs are separated by blank lines. For regexes that are single terms, use one of the following scripts:

           sed -e '/./{H;$!d;}' -e 'x;/regex/!d'      # most seds
           sed '/./{H;$!d;};x;/regex/!d'              # GNU sed

      To print paragraphs only if they contain 3 specific regular expressions (RE1, RE2, and RE3), in any order in the paragraph:

           sed -e '/./{H;$!d;}' -e 'x;/RE1/!d;/RE2/!d;/RE3/!d'

      With this solution and the preceding one, if the paragraphs are excessively long (more than 4k in length), you may overflow sed's internal buffers.

    27. Question 27. How Do I Match A Block Of Specific Consecutive Lines?

      Answer :

      There are three ways to approach this problem:

      1. Try to use a "/range/, /expression/"
      2. Try to use a "/multi-linenexpression/"
      3. Try to use a block of "literal strings"

      We describe each approach in the following sections.

    28. Question 28. How Do I Address All The Lines Between Re1 And Re2, Excluding The Lines Themselves?

      Answer :

      Normally, to address the lines between two regular expressions, RE1 and RE2, one would do this: '/RE1/,/RE2/{commands;}'. Excluding those lines takes an extra step. To put 2 arrows before each line between RE1 and RE2, except for those lines:

           sed '1,/RE1/!{ /RE2/,/RE1/!s/^/>>/; }' input.fil

      The preceding script, though short, may be difficult to follow. It also requires that /RE1/ cannot occur on the first line of the input file. The following script, though it's not a one-liner, is easier to read and it permits /RE1/ to appear on the first line:

           # sed script to replace all lines between /RE1/ and /RE2/,
           # without matching /RE1/ or /RE2/
           #---end of script---

    29. Question 29. How Do I Join Two Lines If Line #1 Ends In A [certain String]?

      Answer :

      sed -e :a -e '/\$/N; s/\n//; ta' file # all seds
      sed ':a; /\$/N; s/\n//; ta' file  # GNU sed, ssed, HHsed

      Note that this replaces the backslash-newline with nothing. You may want to replace the backslash-newline with a single space instead.

    30. Question 30. How Do I Join Two Lines If Line #2 Begins In A [certain String]?

      Answer :

      Suppose a line begins with a particular string. How do you bring that line up to follow the previous line In this example, we want to match the string "<<=" at the beginning of one line, bring that line up to the end of the line before it, and replace the string with a single space:

      sed -e :a -e '$!N;s/n<<=/ /;ta' -e 'P;D' file  # all seds
      sed ':a; $!N;s/n<<=/ /;ta;P;D' file  # GNU, ssed, sed15+

    31. Question 31. How Do I Change All Paragraphs To Long Lines?

      Answer :

      A frequent request is how to convert DOS-style textfiles, in which each line ends with "paragraph marker", to Microsoft-style textfiles, in which the "paragraph" marker only appears at the end of real paragraphs. Sometimes this question is framed as, "How do I remove the hard returns at the end of each line in a paragraph?"

      The problem occurs because newer word processors don't work the same way older text editors did. Older text editors used a newline (CR/LF in DOS; LF alone in Unix) to end each line on screen or on disk, and used two newlines to separate paragraphs. Certain word processors wanted to make paragraph reformatting and reflowing work easily, so they use one newline to end a paragraph and never allow newlines within a paragraph. This means that textfiles created with standard editors (Emacs, vi, Vedit, Boxer, etc.) appear to have "hard returns" at inappropriate places. The following sed script finds blocks of consecutive nonblank lines (i.e., paragraphs of text), and converts each block into one long line with one "hard return" at the end.

           # sed script to change all paragraphs to long lines
           /./{H; $!d;}  # Put each paragraph into hold space
           x;   # Swap hold space and pattern space
       s/^(n)(..*)$/21/; # Move leading n to end of PatSpace
       s/n(.)/ 1/g;     # Replace all other n with 1 space
      # Uncomment the following line to remove excess blank lines:
           # /./!d;
           #---end of sed script---

      If the input files have formatting or indentation that conveys special meaning (like program source code), this script will remove it. But if the text still needs to be extended, try 'par' (paragraph reformatter) or the 'fmt' utility with the -t or -c switches and the width option (-w) set to a number like 9999.

    32. Question 32. How Do I Export Or Pass Variables Back Into The Environment?

      Answer :

      Suppose that line #1, word #2 of the file 'terminals' contains a value to be put in your TERM environment variable. Sed cannot export variables directly to the shell, but it can pass strings to shell commands. To set a variable in the Bourne

      shell:TERM=`sed 's/^[^ ][^ ]* ([^ ][^ ]*).*/1/;q' terminals`;
      export TERM

      If the second word were "Wyse50", this would send the shell command "TERM=Wyse50".

    33. Question 33. How Do I Replace "/some/unix/path" In A Substitution?

      Answer :

      Technically, the normal meaning of the slash can be disabled by prefixing it with a backslash. Thus,

           sed 's//some/UNIX/path//a/new/path/g' files

      But this is hard to read and write. There is a better solution. The s/// substitution command allows '/' to be replaced by any other character (including spaces or alphanumerics). Thus,

           sed 's|/some/UNIX/path|/a/new/path|g' files

      and if you are using variable names in a Unix shell script,

           sed "s|$OLDPATH|$NEWPATH|g" oldfile >newfile

    34. Question 34. Why Don't My Variables Like $var Get Expanded In My Sed Script?

      Answer :

      Because your sed script uses 'single quotes' instead of "double quotes." Unix shells never expand $variables in single quotes.

    35. Question 35. Where Are The Man Pages For Gnu Sed?

      Answer :

      Prior to GNU sed v3.02, there weren't any. Until recently, man pages distributed with gsed were borrowed from old sources or from other compilations. None of them were "official." GNU sed v3.02 had the first real set of official man pages, and the documentation has greatly improved with GNU sed version 4.0, which now includes both man pages and textinfo pages.

    36. Question 36. How Do I Tell What Version Of Sed I Am Using?

      Answer :

      Try entering "sed" all by itself on the command line, followed by no arguments or parameters. Also, try "sed --version". In a pinch, you can also try this:

             strings sed | grep -i ver

      Your version of 'strings' must be a version of the Unix utility of this name. It should not be the DOS utility STRINGS.COM by Douglas Boling.

    37. Question 37. Does Sed Issue An Exit Code?

      Answer :

      Most versions of sed do not, but check the documentation that came with whichever version you are using. GNU sed issues an exit code of 0 if the program terminated normally, 1 if there were errors in the script, and 2 if there were errors during script execution.

Popular Interview Questions

All Interview Questions

All Practice Tests

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

Sed (Stream Editor) Tutorial