on “sed” command
EmpData(Sample Database)

1122|j.b. saxena |g.m. |account |12/12/52|6000
2233|n.k. gupta |d.g.m |sales |31/12/40|9000
4545|anil agarwal |director |account |06/07/47|7500
5656|lalit choudhury |executive|marketing|07/09/50|5000
1265|chanchal singhvi|g.m. |admin |12/09/63|6000
0110|shyam saksena |chairman |marketing|12/12/43|8000
5566|jai sharma |director |account |23/12/89|7000
7733|jayant |d.g.m |sales |29/02/70|6000


1. From the above database substitute the delimiter of first
3 lines with “ : “
2. From the above database substitute the delimiter with “ : ”
3. Insert the string “ XYZ Employees” in the first line.
4. Store the lines pertaining to the directors, d.g.m and
g.m into three separate files.
5. Using address store first 4 lines into a file Empupdate.
6. Find the pattern “account” in the database and replaces
that with “accounts”.
7. Select those lines which do not have a pattern “g.m”.
8. Insert a blank line after every line in the database.



on “sed” command EmpData(Sample Database) 1122|j.b. saxena |g.m. |account |12/12/52|6000 22..

Answer / Prem Sagar

To perform these tasks using the sed command, you can chain several commands together as follows:n
1. Replace the first three lines' delimiter with " by executing `sed -i '3,5s/: /:/g' EmpData`. This command selects lines 3 to 5 and replaces all instances of : with : using the sed replace command (-s) in-place (-i).n
2. Replace the delimiter with " by executing `sed -i 's/://g' EmpData`.n
3. Insert "XYZ Employees" at the beginning of the file using `sed -i '1i "XYZ Employees"' EmpData`. The i option inserts a new line before the specified one (line 1).n
4. To store lines pertaining to directors, d.g.m, and g.m into separate files, you can use `sed -n '/director/,/g.m/p' EmpData > director.txt` for directors, `sed -n '/d.g.m/,/g.m/p' EmpData > dgm.txt`, and `sed -n '/g.m/,/g.m/p' EmpData > gm.txt`. These commands select the lines containing 'director' or 'd.g.m' or 'g.m' and print them (p) using sed.n
5. To store the first 4 lines into a file called Empupdate, you can use `sed -n '1,4p' EmpData > Empupdate`. This command selects and prints lines 1 to 4.n
6. Replace "account" with "accounts" using `sed -i 's/account/accounts/g' EmpData`.n
7. Select those lines which do not have a pattern "g.m" using `grep -v 'g.m' EmpData`. The grep command prints only lines that do not match the specified pattern (-v).n
8. Insert a blank line after every line in the database using `sed '/./{n$a n}' EmpData > newEmpData`. This command selects every line (/) with a dot (.) and inserts a newline ($) after each line (a) to create a blank line before printing (>). The resulting file will be called newEmpData.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Shell Script Interview Questions

Is powershell a bash?

1 Answers  


What language is shell scripting?

1 Answers  


What is path in shell script?

1 Answers  


What is meant by $1 in shell script?

1 Answers  


What is sed in shell script?

1 Answers  


Hi All, Is it possible to create one file name only space or space in file name in UNIX and we can able to run that on Unix?

2 Answers   Cap Gemini,


What does it mean to debug a script?

1 Answers  


c program which behaves like a shell(command interpreter). it has its own prompt say "NewShell$".any normal shell command is executed from your shell by starting a child process to execute a system program corrosponding to the command

1 Answers  


What is k shell?

1 Answers  


What is a shell environment?

1 Answers  


What is the difference between a 'thread' and a 'process'?

3 Answers  


How to take input values from the user?

4 Answers  


Categories