Sed Command





  


🔥 What is sed?

sed stands for Stream Editor — it's a powerful Unix tool to search, find and replace, insert, and delete text in files or streams non-interactively.


🧠 sed Syntax

sed [OPTIONS] 'COMMAND' file


Breakdown:

  • COMMAND: Script-like instructions, e.g., 's/old/new/g'

  • file: Input text file or stream (can also be piped)


📘 SECTION 1: BASIC COMMANDS

1️⃣ Print a file

sed '' filename


2️⃣ Delete lines

sed '2d' file.txt        # Delete 2nd line
sed '2,4d' file.txt      # Delete lines 2 to 4


sed '/^$/d' file.txt     # Delete all empty lines


3️⃣ Find and replace

sed 's/old/new/' file.txt            # Replace first 'old' per line
sed 's/old/new/g' file.txt           # Replace ALL 'old' per line
sed 's/foo/bar/2' file.txt           # Replace 2nd occurrence of 'foo' per line


4️⃣ Case insensitive replace

sed 's/apple/orange/I' file.txt      # 'I' for case insensitive



📘 SECTION 2: INTERMEDIATE USAGE

5️⃣ In-place edit with -i

sed -i 's/foo/bar/g' file.txt        # Linux
sed -i '' 's/foo/bar/g' file.txt     # macOS (BSD `sed`)


6️⃣ Insert / Append text

sed '3i\This is inserted above line 3' file.txt
sed '3a\This is added below line 3' file.txt


7️⃣ Replace on a specific line

sed '5s/error/ERROR/' file.txt       # Replace only on line 5


8️⃣ Replace using regex patterns

sed 's/[0-9][0-9]*/[number]/g' file.txt



📘 SECTION 3: ADVANCED USE CASES

9️⃣ Replace text between two markers

sed '/START/,/END/s/foo/bar/g' file.txt


🔟 Remove comments from config files

sed '/^\s*#/d;/^$/d' config.txt


11️⃣ Change multiple patterns

sed -e 's/apple/banana/g' -e 's/orange/mango/g' fruits.txt



⚙️ Real-Time Use Cases (DevOps/Data)

✅ Clean log files

sed '/DEBUG/d' app.log > cleaned.log


✅ Mask sensitive info

sed -E 's/(password\s*=\s*).*/\1*****/' config.ini


✅ Change URL in CI/CD pipeline YAML

sed -i 's#https://old-repo#https://new-repo#g' .gitlab-ci.yml


✅ Parse output from commands

ps aux | sed -n '/nginx/p'


Note: with print always using -n


❓ FAANG-Level Interview Questions with Answers

Q1: How would you replace the 3rd occurrence of a word in a line using sed?

sed 's/foo/bar/3' file.txt


Q2: Delete all blank lines and lines starting with #

sed '/^\s*#/d;/^$/d' config.txt


Q3: Use sed to replace only within a function in a file (e.g., between { and })

sed '/function_name {/,/}/s/foo/bar/' file.sh


Q4: What does sed -n '5p' file.txt do?

A: It prints only the 5th line (-n suppresses automatic output).

Q5: How can you extract everything before a delimiter (:)?

sed 's/:.*//' file.txt



🧪 Practice Exercises

  1. Mask all email addresses in a file

  2. Remove HTML tags from a file

  3. Replace tabs with commas

  4. Swap two columns (use sed + awk)


📌 Tips

Tip

Description

Use -n + p

For printing specific lines

Always quote patterns

's/foo/bar/' not s/foo/bar/

Test before -i

Avoid data loss

Combine with grep/awk

For powerful pipelines


📺 Recommended YouTube Channels

  1. Linux Leech – deep dive into sed and shell scripting

  2. The Net Ninja – Bash scripting series

  3. Chris Titus Tech – Linux command mastery

  4. TechTFQ – Unix interview questions

Dave Jones (eLinux) – sed/awk/grep tutorials




Here's a focused and clear tutorial on how to find lines using sed and then insert or append lines before or after the match, including FAANG-level examples.


🔍 PART 1: FIND and INSERT / APPEND in sed

🔸 Syntax Overview

Operation

Syntax

Insert line before match

sed '/pattern/i\new line' file.txt

Append line after match

sed '/pattern/a\new line' file.txt


🧪 PRACTICAL EXAMPLES


✅ 1. Insert line before a match

sed '/server_name/i\# Inserted line before server_name' nginx.conf


📌 Before:

server {
  server_name example.com;
}


📌 After:

server {
  # Inserted line before server_name
  server_name example.com;
}



✅ 2. Append line after a match

sed '/server_name/a\# Appended line after server_name' nginx.conf


📌 After:

server {
  server_name example.com;
  # Appended line after server_name
}



✅ 3. Insert a line before every line matching a keyword (e.g., ERROR)

sed '/ERROR/i\--- ERROR FOUND ---' logs.txt



✅ 4. Append a blank line after each } (useful in code formatting)

sed '/}/a\\' script.sh



✅ 5. Insert multiple lines before match

sed '/pattern/i\
Line one\
Line two' file.txt


(Each new line ends with \, escape newline)


✅ 6. Append content from a file after a pattern

sed '/pattern/r file_to_append.txt' main.txt


  • Appends file_to_append.txt after every line that matches pattern.


📘 ADVANCED COMBINED EXAMPLE

🧠 FAANG-style: Add a comment before every function and a closing marker after it.

sed '/^def /i\# START OF FUNCTION' script.py | sed '/^def /,/^$/a\# END OF FUNCTION'



⚙️ Real-Time Use Case

DevOps: Insert a new cron job into crontab file before the daily job

sed '/0 2 \* \* \*/i\15 1 * * * /usr/bin/cleanup.sh' crontab.txt



🔄 Alternative: Insert line using line number (not pattern)

sed '3i\Inserted before line 3' file.txt
sed '5a\Appended after line 5' file.txt



❓ Quick Interview Questions

Q1. How do you insert a header at the top of every config file?

sed '1i\# Configuration File - Do not edit manually' config.ini


Q2. How to add a logging line after every function definition in Python?

sed '/^def /a\    print("Entering function")' script.py




Distributed by Gooyaabi Templates | Designed by OddThemes