Using the same database file datebook, record in the spaces provided the sed com
ID: 3852594 • Letter: U
Question
Using the same database file datebook, record in the spaces provided the sed commands required to complete each problem. Make sure you suppress default printing for Problems 1, 3, 5, & 6. Problem 10 has the student create a separate sed script file called sed.sh which is run from the command line using the following sed command:
$ sed -f sed.sh datebook
SED EXERCISE
1. Change the name Jon to Jonathan.
--------------------------------------------------------------------------------------------------------------------------------
2. Delete the first three lines.
------------------------------------------------------------------------------------------------------------------------------------
3. Print lines 5 through 10.
-----------------------------------------------------------------------------------------------------------------------------------
4. Delete lines containing Lane.
----------------------------------------------------------------------------------------------------------------------------------
5. Print all lines where the birthdays are in November or December (must use mm/dd/yy format).
-----------------------------------------------------------------------------------------------------------------------------------------
6. Append three asterisks to the end of lines starting with Fred.
-------------------------------------------------------------------------------------------------------------------------------------------
7. Replace the line containing Jose with JOSE HAS RETIRED.
----------------------------------------------------------------------------------------------------------------------------------------------
8. Change Popeye's birthday to 11/14/46. Assume you don't know Popeye's original birthday. Use a regular expression to search for the birthday (i.e., you must match a generic mm/dd/yy search pattern).
-----------------------------------------------------------------------------------------------------------------------------------------------------
9. Delete all blank lines (blank lines can be null lines, contain spaces, or tabs).
--------------------------------------------------------------------------------------------------------------------------------
10. Write a sed script (in a separate file called sed.sh) that will:
a. Insert above the first line the title PERSONNEL FILE.
b. Remove the salaries ending in 500.
c. Print the contents of the file with the last names
d. . Append at the end of the file THE END.
Explanation / Answer
NOTE: If you have any problems with any of the below commands please comment and i will revert back within 24 hrs.
Commands and the execution output:
Sample input text file i have created since i do not have datebook file provided as part of the assignment
Unix Terminal> cat datebook
Jon is a nice guy
Hello world is the first program people write
Project Gutenberg offers over 54,000 free eBooks
You will find the world's great literature here, especially older works for which copyright has expired
Historically, object caching in the web application keeps an in-memory hash map of table objects
Caching is another homegrown technology called Stout
UI not so good, the focus is on performance and scale.
Two subsystems: CMS and DCS (dynamic content system).
The CMS for editorial staff.
Highly optimized for querying.
Optimized UI for users that make changes to stories.
Full relational database model (SQL Server). Only a few hundred editors so it doesn’t need to scale horizontally.
The DCS uses SQL Server, but is denormalized and stored as a blob type.
Retrieval is fast.
Edited data goes on in the CMS.
When an article is published the content serialized and put into the DCS.
DCS is a cluster of 10 servers that can be horizontally scaled.
Unix Terminal>
1. Change the name Jon to Jonathan.
Ans)
Unix Terminal> sed -n 's/Jon/Jonathan/p' datebook
Jonathan is a nice guy
2. Delete the first three lines.
Ans)
Unix Terminal> sed '1,3d' datebook
You will find the world's great literature here, especially older works for which copyright has expired
Historically, object caching in the web application keeps an in-memory hash map of table objects
Caching is another homegrown technology called Stout
UI not so good, the focus is on performance and scale.
Two subsystems: CMS and DCS (dynamic content system).
The CMS for editorial staff.
Highly optimized for querying.
Optimized UI for users that make changes to stories.
Full relational database model (SQL Server). Only a few hundred editors so it doesn’t need to scale horizontally.
The DCS uses SQL Server, but is denormalized and stored as a blob type.
Retrieval is fast.
Edited data goes on in the CMS.
When an article is published the content serialized and put into the DCS.
DCS is a cluster of 10 servers that can be horizontally scaled.
3) Print lines 5 through 10.
Ans)
Unix Terminal> sed -n '5,10p' datebook
Historically, object caching in the web application keeps an in-memory hash map of table objects
Caching is another homegrown technology called Stout
UI not so good, the focus is on performance and scale.
Two subsystems: CMS and DCS (dynamic content system).
The CMS for editorial staff.
Highly optimized for querying.
4) Delete lines containing Lane.
Ans)
I dont have the datebook file with string Lane. So i have given the command directly.
Unix Terminal> sed '/Lane/d' datebook
5) Print all lines where the birthdays are in November or December (must use mm/dd/yy format).
Ans)
I have created a test file with name 'testfile' for showing how the command works
Unix Terminal> cat testfile
01/10/17
11/11/18
01/12/12
02/10/12
btw 12/11/13 hey
hello world 09/12/17 how are you
Unix Terminal> sed -n '/1[12]/[0-9][0-9]/[0-9][0-9]/p' testfile
11/11/18
btw 12/11/13 hey
Unix Terminal>
6) Append three asterisks to the end of lines starting with Fred.
Ans)
I dont have datefile that contains line starting with Fred. So i have given the command here.
Unix Terminal> sed '/^Fred/ s/(.*)/***/'
7) Replace the line containing Jose with JOSE HAS RETIRED.
Ans)
Unix Terminal> sed 's/Jose/JOSE HAS RETIRED/' datebook
8) Change Popeye's birthday to 11/14/46. Assume you don't know Popeye's original birthday. Use a regular expression to search for the birthday (i.e., you must match a generic mm/dd/yy search pattern).
Ans)
I have created a testfile 'testfile2' to show how the command works.
Unix Terminal> cat testfile2
krishna birthday is on 12/01/17
ravi birthday is on 01/11/17
Popeye birthday is on 11/11/10
Ravesh birthday is on 12/09/10
Unix Terminal> cat testfile2|sed '/Popeye birthday/ s/[0-9][0-9]/[0-9][0-9]/[0-9][0-9]/11/14/46/'
krishna birthday is on 12/01/17
ravi birthday is on 01/11/17
Popeye birthday is on 11/14/46
Ravesh birthday is on 12/09/10
Unix Terminal>
9. Delete all blank lines (blank lines can be null lines, contain spaces, or tabs).
Ans)
Unix Terminal> cat datebook
Jon is a nice guy
Hello world is the first program people write
Project Gutenberg offers over 54,000 free eBooks
You will find the world's great literature here, especially older works for which copyright has expired
Historically, object caching in the web application keeps an in-memory hash map of table objects
Caching is another homegrown technology called Stout
UI not so good, the focus is on performance and scale.
Two subsystems: CMS and DCS (dynamic content system).
The CMS for editorial staff.
Highly optimized for querying.
Optimized UI for users that make changes to stories.
Full relational database model (SQL Server). Only a few hundred editors so it doesn’t need to scale horizontally.
The DCS uses SQL Server, but is denormalized and stored as a blob type.
Retrieval is fast.
Edited data goes on in the CMS.
When an article is published the content serialized and put into the DCS.
DCS is a cluster of 10 servers that can be horizontally scaled.
Unix Terminal> sed '/^$/d' datebook
Jon is a nice guy
Hello world is the first program people write
Project Gutenberg offers over 54,000 free eBooks
You will find the world's great literature here, especially older works for which copyright has expired
Historically, object caching in the web application keeps an in-memory hash map of table objects
Caching is another homegrown technology called Stout
UI not so good, the focus is on performance and scale.
Two subsystems: CMS and DCS (dynamic content system).
The CMS for editorial staff.
Highly optimized for querying.
Optimized UI for users that make changes to stories.
Full relational database model (SQL Server). Only a few hundred editors so it doesn’t need to scale horizontally.
The DCS uses SQL Server, but is denormalized and stored as a blob type.
Retrieval is fast.
Edited data goes on in the CMS.
When an article is published the content serialized and put into the DCS.
DCS is a cluster of 10 servers that can be horizontally scaled.
Unix Terminal>
10) Write a sed script (in a separate file called sed.sh) that will:
a. Insert above the first line the title PERSONNEL FILE.
b. Remove the salaries ending in 500.
c. Print the contents of the file with the last names
d. Append at the end of the file THE END.
Ans)
Input file:-
Note that krishna, ravi, harish etc are the last names
Unix Terminal> cat testfile
Vishal krishna salary is 1000
Mathur ravi salary is 5500
Vignesh harish salary is 4500
Robert hermen salary is 500
John howard salary is 1233
Script output:-
Unix Terminal> sed -f sed.sh testfile
PERSONNEL FILE
krishna salary is 1000
howard salary is 1233
THE END
Unix Terminal>
Script:
Unix Terminal> cat sed.sh
/500$/d
s/([a-zA-Z]*) ([a-zA-Z]*)//
1s/^/PERSONNEL FILE /
$s/$/ THE END/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.