Fundamentals 10 min read

Mastering sed: Stream Editing Basics, Commands, and Real‑World Examples

This article introduces the Linux stream editor sed, explains its working principle, basic syntax, address ranges, and editing commands, and provides practical exercises demonstrating common text manipulation tasks such as printing even lines, commenting specific patterns, case conversion, and file modifications.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering sed: Stream Editing Basics, Commands, and Real‑World Examples
1. Introduction to sed sed stands for Stream Editor, a powerful tool for processing text line by line in a stream. 2. How sed works When sed processes a file, it reads each line into the pattern space, applies editing commands, outputs the result, and then discards the pattern space before moving to the next line. 3. Basic syntax sed OPTIONS… [SCRIPT] [INPUTFILE…] Common options: -n, --quiet : suppress automatic printing of the pattern space. -i : edit files in place. -e : allow multiple commands. -f /path/from/sed_script : read commands from a script file. -r : use extended regular expressions. 4. Addressing in the pattern space Addresses specify which lines a command applies to: # : a specific line number. $ : the last line. /regexp/ : lines matching a regular expression. /regexp/I : case‑insensitive match. addr1,addr2 : a range of lines. first~step : start at first and select every step line (e.g., 1~2 selects 1,3,5,…). 5. Common editing commands d : delete the selected line. p : print the pattern space (often used with -n to print only matched lines). a \text : append text after the matched line. i \text : insert text before the matched line. c \text : replace the matched line with text . s/regexp/replacement/flags : substitute regexp with replacement . Flags include g (global), i (case‑insensitive), p (print if substituted), w /path/to/file (write matched lines), r /path/to/file (read file into output). 6. Practical exercises Exercise 1: Print even-numbered lines Use the first~step address to select every second line: sed -n '2~2p' file.txt Exercise 2: Comment lines containing "ftp" sed '/ftp/ s/^/#This is a command/' file.txt Exercise 3: Upper‑case letters in lines ending with /sbin/nologin First select the lines, then apply a case conversion: sed -n '\#/sbin/nologin$# { s/[a-z]/\U&/g; p }' file.txt Exercise 4: Save non‑comment lines from /etc/man.config sed -n -e '/^#/d;w /tmp/sed/man.txt' /etc/man.config Exercise 5: Change the initdefault ID in /tmp/sed/inittab sed -i 's/^id:[0-9]:initdefault:/id:5:initdefault:/' /tmp/sed/inittab 7. Advanced sed concepts Besides the pattern space, sed also has a hold space for temporary storage. Relevant commands include: h : copy pattern space to hold space. H : append pattern space to hold space. g : replace pattern space with hold space. G : append hold space to pattern space. x : exchange pattern and hold spaces. D : delete the first line of a multi‑line pattern space. n : read the next line into pattern space (replace). N : read the next line and append it to pattern space. Example: print even lines using n and p : sed -n 'n;p' file.txt 8. Summary sed is a versatile text‑processing tool that follows the Unix philosophy of combining small, focused commands to accomplish complex tasks. Mastering its basic syntax, addressing, and editing commands provides a solid foundation for efficient shell scripting and data manipulation.
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Linuxcommand-lineregextext processingsedstream editor
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.