Mastering sed and expect: Powerful One‑Liner Tricks for Linux Automation
This guide introduces the Linux stream editor sed and the automation tool expect, covering essential options, line‑based editing, pattern matching, backup strategies, substitution techniques, grouping, practical exercises, non‑interactive input methods, and expect scripting fundamentals for automating interactive shell sessions.
sed
sed works on lines of a file and can modify file contents without opening the file.
Typical syntax: sed [options] ... {script} filename Key options: -i: edit files in place. -i.bak: edit in place and create a backup with a .bak extension. -n: suppress automatic printing. -r: use extended regular expressions. -p: print. -q: quit early (e.g., 3q prints up to line 3 then exits). -d: delete (e.g., 3d deletes line 3). -a: append after a line (e.g., 3a text). -i: insert before a line (e.g., 3i text). -c: change a line (e.g., 3c new). =: print line numbers.
Examples:
Extract odd lines: sed -n '1~2p' or sed -n 'p;N;D'.
Extract even lines: sed -n '2~2p'.
Print lines between two patterns (e.g., between lines starting with b and f).
Replace text: s/old/new/g (or using alternate delimiters s#old#new#g, s@old@new@g).
Modifiers: g (global), p (print matched lines), w file (write matched lines to a file), i / I (ignore case).
Backup before editing: sed -i.bak 's/SELINUX=enforcing/SELINUX=enable/' file.
Search and replace
Standard form: s/old/new/flags. Alternate delimiters ( #, @) can be used to avoid escaping characters.
Replacement exercise
Replace SELINUX=enforcing with SELINUX=enable in a file, remembering to create a backup with -i.bak.
Group reference
Only extended regular expressions support grouping.
Example pattern to capture an IP address: /inet[[:space:]]+([0-9.]+)/ where the part in parentheses is the captured group.
Exercise 1
Extract the IP address from network interface output using a pattern like /inet[[:space:]]+([0-9.]+)/.
Exercise 2
Extract the numeric part of a version string by using - as a delimiter and capturing the portion before .jar.
Exercise 3
Extract the numeric permission bits from a file permission string using a suitable regular expression.
Non‑interactive input
Multi‑line redirection example:
cat >target_file <<EOF
line1
line2
EOFThis writes multiple lines to target_file in one command.
expect
Expect solves interactive prompts in shell scripts by automating input.
Installation: yum install expect -y Key keywords: spawn: start a program. expect: wait for a pattern. exp_continue: continue expecting after a match. send: send input to the spawned program. interact: hand control back to the user. set: define variables.
Expect scripts must start with #!/usr/bin/expect, not a bash shebang.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Raymond Ops
Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
