Sed Exercise (From “UNIX® Shells by Example Fourth EditionBy Ellie Quigley”)

To become familiar with sed (stream editor), I decided to write the answer of exercise from the book titled “UNIX® Shells by Example Fourth EditionBy Ellie Quigley”. Download here for downloading the questions and answers, or directly from your beloved shell wget http://bondhan.web.id/files/sed_exercise.tar.gz.

Suppose you have a file, containing these lines:

█▓▒░bondhan@optimus-prime█▓▒░ Tue Dec 21 16:58:22

/media/BONDHAN_4GB/workspace/sed_exercise/ cat text_file
Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300

Betty Boop:245-836-8357:635 Cutesy Lane, Hollywood, CA 91464:6/23/23:14500

Igor Chevsky:385-375-8395:3567 Populus Place, Caldwell, NJ 23875:6/18/68:23400

Norma Corder:397-857-2735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700

Jennifer Cowan:548-834-2348:583 Laurel Ave., Kingsville, TX 83745:10/1/35:58900

Jon DeLoach:408-253-3122:123 Park St., San Jose, CA 04086:7/25/53:85100

Karen Evich:284-758-2857:23 Edgecliff Place, Lincoln, NB 92743:7/25/53:85100

Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200

Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900

Fred Fardbarkle:674-843-1385:20 Parak Lane, Duluth, MN 23850:4/12/23:780900

Lori Gortz:327-832-5728:3465 Mirlo Street, Peabody, MA 34756:10/2/65:35200

Paco Gutierrez:835-365-1284:454 Easy Street, Decatur, IL 75732:2/28/53:123500

Ephram Hardy:293-259-5395:235 CarltonLane, Joliet, IL 73858:8/12/20:56700

James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000

Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500

Lesley Kirstin:408-456-1234:4 Harvard Square, Boston, MA 02133:4/22/62:52600

William Kopf:846-836-2837:6937 Ware Road, Milton, PA 93756:9/21/46:43500

Sir Lancelot:837-835-8257:474 Camelot Boulevard, Bath, WY 28356:5/13/69:24500

Jesse Neal:408-233-8971:45 Rose Terrace, San Francisco, CA 92303:2/3/36:25000

Zippy Pinhead:834-823-8319:2356 Bizarro Ave., Farmount, IL 84357:1/1/67:89500

Arthur Putie:923-835-8745:23 Wimp Lane, Kensington, DL 38758:8/31/69:126000

Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:3/19/35:22350

Jose Santiago:385-898-8357:38 Fife Way, Abilene, TX 39673:1/5/58:95600

Tommy Savage:408-724-0140:1222 Oxbow Court, Sunnyvale, CA 94087:5/19/66:34200

Yukio Takeshida:387-827-1095:13 Uno Lane, Ashville, NC 23556:7/1/29:57000

Vinh Tranh:438-910-7449:8235 Maple Street, Wilmington, VM 29085:9/23/63:68900

And with these questions:

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.

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

9. Delete all blank lines.

10. Write a sed script 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 and first names reversed.

d. Append at the end of the file THE END.

Here’s the answer:

#!/bin/bash

if [ "${1}" == "" ];
then
TEXT=”./text_file”
else
TEXT=”${1}”
fi

if [ ! -e ${TEXT} ];
then
echo “${TEXT} do not exist”
exit 1
fi

press_execute()
{
echo
read -n1 -p “Press any key to see the result ” key <$TTY
echo
}

read -p “Insert which questions and answer you would like to know [1-10] ” number <$TTY

case ${number} in
1)
echo “1. Change the name Jon to Jonathan.”
echo “answer:”
echo “sed ‘s/\/Jonathan/’ ${TEXT}”
press_execute
sed ‘s/Jon/Jonathan/’ ${TEXT}
;;
2)
echo “2. Delete the first three lines.”
echo “answer:”
echo “sed ’1,3d’ ${TEXT}”
press_execute
sed ’1,3d’ ${TEXT}
;;
3)
echo “3. Print lines 5 through 10.”
echo “answer:”
echo “sed -n ’5,10p’ ${TEXT}”
press_execute
sed -n ’5,10p’ ${TEXT}
;;
4)
echo “4. Delete lines containing Lane.”
echo “answer:”
echo “sed ‘/\1/d’ ${TEXT}”
press_execute
sed ‘/Lane/d’ ${TEXT}
;;
5)
echo “5. Print all lines where the birthdays are in November or December.”
echo “answer”
echo “sed -n ‘s#:1[12]/#&#p’ ${TEXT}”
press_execute
sed -n ‘s#:1[12]/#&#p’ ${TEXT}
;;
6)
echo “6. Append three asterisks to the end of lines starting with Fred.”
echo “answer:”
echo “sed ‘s/Fred/&***/g’ ${TEXT}”
press_execute
sed ‘s/Fred/&***/g’ ${TEXT}
;;
7)
echo “7. Replace the line containing Jose with JOSE HAS RETIRED.”
echo “answer:”
echo “sed ‘s/Jose/JOSE HAS RETIRED/g’ ${TEXT}”
press_execute
sed ‘s/Jose/JOSE HAS RETIRED/g’ ${TEXT}
;;
8)
echo “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 it.”
echo “answer:”
echo “sed -e ‘/[pP]opeye/!d’ -e ‘s/.\/..\/../4\/14\/46/’ ${TEXT}”
press_execute
sed -e ‘/[pP]opeye/!d’ -e ‘s/.\/..\/../4\/14\/46/’ ${TEXT}
;;
9)
echo “9. Delete all blank lines.”
echo “answer:”
echo “sed ‘/^$/d’ ${TEXT}”
press_execute
sed ‘/^$/d’ ${TEXT}
;;
10)
echo “10. Write a sed script that will”
echo ” a. Insert above the first line the title PERSONNEL FILE.”
echo ” b. Remove the salaries ending in 500.”
echo ” c. Print the contents of the file with the last names and first names reversed.”
echo ” d. Append at the end of the file THE END.”
echo “Answer: (written in a file named:answer10)”
echo
ANSWER10_FILE=”answer10″
rm -rf “${ANSWER10_FILE}”
if [ ! -e "${ANSWER10_FILE}" ];
then
touch ${ANSWER10_FILE}
echo “# Insert above the first line the title PERSONNEL FILE.” >> ${ANSWER10_FILE}
echo “1,0i\PERSONNEL FILE” >> ${ANSWER10_FILE}
echo “# Remove the salaries ending in 500.” >> ${ANSWER10_FILE}
echo “s/500$/xxx/g” >> ${ANSWER10_FILE}
echo “# Print the contents of the file with the last names and first names reversed.” >> ${ANSWER10_FILE}
echo \$a\\END >> ${ANSWER10_FILE}
echo “# Append at the end of the file THE END.” >> ${ANSWER10_FILE}
echo “# if only names reversed:” >> ${ANSWER10_FILE}
echo “#’sed -e ‘s/\([^:]*\).*/\1/’ -e ‘s/\([a-zA-Z]*\) \([a-zA-Z]*\)/\2 \1/’” >> ${ANSWER10_FILE}
echo “#else:” >> ${ANSWER10_FILE}
echo “s/\([a-zA-Z]*\) \([a-zA-Z]*\)/\2 \1/” >> ${ANSWER10_FILE}

fi
cat ${ANSWER10_FILE}
press_execute
sed -f ${ANSWER10_FILE} ${TEXT}
;;
*)
echo “Wrong number!”
exit 1
esac

Some execution example (number 5):

█▓▒░bondhan@optimus-prime█▓▒░ Tue Dec 21 17:08:14
/media/BONDHAN_4GB/workspace/sed_exercise/ ./q_answer.sh
Insert which questions and answer you would like to know [1-10] 5
5. Print all lines where the birthdays are in November or December.
answer
sed -n ‘s#:1[12]/#&#p’ ./text_file

Press any key to see the result

Steve Blenheim:238-923-7366:95 Latham Lane, Easton, PA 83755:11/12/56:20300
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
Karen Evich:284-758-2867:23 Edgecliff Place, Lincoln, NB 92743:11/3/35:58200
James Ikeda:834-938-8376:23445 Aster Ave., Allentown, NJ 83745:12/1/38:45000
Barbara Kertz:385-573-8326:832 Ponce Drive, Gary, IN 83756:12/1/46:268500

Another execution example (number  8):

█▓▒░bondhan@optimus-prime█▓▒░ Tue Dec 21 17:09:40
/media/BONDHAN_4GB/workspace/sed_exercise/ ./q_answer.sh
Insert which questions and answer you would like to know [1-10] 8
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 it.
answer:
sed -e ‘/[pP]opeye/!d’ -e ‘s/.\/..\/../4\/14\/46/’ ./text_file

Press any key to see the result

Popeye Sailor:156-454-3322:945 Bluto Street, Anywhere, USA 29358:4/14/46:22350

Good references for SED:

About Bondhan Novandy

Just simply ordinary person.
This entry was posted in Exercise, Linux, Programming, Sed and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>