Lesson 3
Basic sed Syntax and the petchip Dataset
Objective: Form a correct sed command and inspect its output.
sed 'instruction' input-fileUse straight ASCII quotation marks. Single quotes keep shell expansion from changing characters such as $, backslashes, or spaces inside the script. The shell removes those quotes before passing the script to sed. A file operand supplies input; without one, sed reads standard input.
Create the practice dataset
Copy the following text into a new file named petchip in your practice directory. Each physical line is one record; do not insert line breaks where a narrow display wraps the text.
Sam (Big Boy)
Sam (Big Boy):cat:DSH:5/20/1990:800:Susan:Barr:47823
Maisey:cat:DSH:9/24/1994:700:Antonio:Romero:55902
Licorice:dog:Lab Mix:7/5/1998:1200:Jeanne:Barr:34648
Mitsy:dog:Lab Mix:9/3/1990:500:Lena:Hatcher:37218
Milo:cat:DSH:7/7/1996:300:Kathy:Nagle:52867
Otis:cat:DSH:7/7/1996:300:John:Smith:52868
Farley:ferret::12/4/1999:100:Tom:Kensington:46978
Lucy:cat:DLH:4/23/1999:5000:Frank:Schwab:23970
Sheba:dog:Sheltie:7/30/1993:1200:Dianne:Sturwold:48124Each record contains eight colon-separated fields: pet name, species, breed, birth date, a legacy numeric sample field, owner given name, owner family name, and a five-digit sample identifier. The original lesson’s claim of seven fields was incorrect. The numeric field is left uninterpreted because its unit is not established by the supplied source. Farley’s empty breed field is represented by two adjacent colons.
Make a precise substitution
sed 's/:cat:/:rabbit:/' petchip
The colons constrain the example to the species token in this dataset. The first record becomes:
Sam (Big Boy):rabbit:DSH:5/20/1990:800:Susan:Barr:47823A bare pattern cat would also match those letters inside longer text. Sed is not a field parser, so this delimiter technique assumes the simple data format shown. For general field validation, use an appropriate parser or awk.
Keep the source safe
sed 's/:cat:/:rabbit:/' petchip > petchip.preview
cat petchip.previewChoose an unused output filename: > overwrites an existing output file. Review the preview and keep the original as the baseline for the remaining lessons. If an application requires replacing a real file, make a backup and account for ownership, permissions, links, and concurrent writers; a simple rename is not a complete production editing procedure.