Sed (Stream Editor) Interview Questions & Answers

  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. Adobe Photoshop Interview Questions

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

    Answer :

    sed ‘s///,/’ < filename
    sed ‘s|/|,|’ < filename

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

    Answer :

    sed s/apple/(&)/ < filename

  6. Adobe Photoshop Tutorial

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

  8. Audio Editor Interview Questions

  9. 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

  10. 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

  11. Proofreader Interview Questions

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

    Answer :

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

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

    Answer :

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

  14. Communication Skills Interview Questions

  15. 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

  16. 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

  17. Video Editing Interview Questions

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

    Answer :

    sed ‘1,10 d’ < filename

  19. Adobe Photoshop Interview Questions

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

    Answer :

    sed ‘p’ < filename

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

    Answer :

    sed ‘/^$/ p’ < filename

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

    Answer :

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

  23. Medical Transcription Interview Questions

  24. 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

  25. 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.

  26. Vi Editor Interview Questions

  27. 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!

  28. Audio Editor Interview Questions

  29. 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 “

Comments are closed.