adspace


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.

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Hello all, This is my assignment on shell scripting, can anyone help me regarding this ? Create a shell script which connects to the database In second shell script include the first script for the DB connection Create a table (PRADEEP_DATA) with 2 columns (name, value) In Third shell script include the first script for the DB connection And insert/delete the values from the Table, by accepting input from the user This functionality should be a menu driven Program: 1) Insert to the database a. Name b. value 2)Delete from the database a.Name b.value Exception handling needs to be taken care.

2325