We use the s option for substitution.
$ sed 's/durga/linux/g' demo.txt- It will replace all occurrences of "durga" with "linux" while displaying content without modifying the file.
$ sed 's/durga/linux/' demo.txt- Replaces only the first occurrence of "durga" in every line.
$ sed 's/DURGA/linux/gi' demo.txt- Replaces "DURGA" with "linux" and ignores case sensitivity.
$ sed -i 's/DURGA/linux/gi' demo.txt- Replaces "DURGA" with "linux" in the file permanently.
$ sed -i 's/^$/I Like durga/g' demo.txt- Replaces every blank line with "I Like Sunny".
$ sed -i 's/\<durga\>//gi' demo.txt- Deletes every occurrence of "sunny" in the file, ignoring case.
$ sed -i 's/\<[0-9][0-9][0-9]\>//g' demo.txt- Deletes all 3-digit words from the file.
sed -i 's/linux soft/durga soft/gi' demo.txtThe stream editor (sed) is a powerful text-processing tool often used for modifying text streams. Below, I'll provide an overview of common sed commands and demonstrate their usage with a case study.
-
Substitution (
s)- Syntax:
s/old/new/flags - Flags:
g: Replace all occurrences in a line.i: Case-insensitive matching.n: Replace the nth occurrence.
- Syntax:
-
Delete (
d)- Deletes lines matching a pattern.
-
Print (
p)- Prints lines matching a pattern.
-
Append/Insert (
a,i)a: Appends text after the current line.i: Inserts text before the current line.
-
Replace Line (
c)- Replaces the entire line matching a pattern.
-
Transform (
y)- Transforms characters in a 1:1 mapping (like
tr).
- Transforms characters in a 1:1 mapping (like
-
Read/Write (
r,w)r: Reads from a file and appends it.w: Writes matched lines to a file.
-
Hold Space Commands
h,H,g,G,x: Used for advanced processing by saving/retrieving patterns.
You have a server log file server.log with the following content:
INFO: Server started at 10:00
ERROR: Connection failed
WARNING: High memory usage
INFO: Backup completed at 12:00
ERROR: Disk space low
INFO: Shutdown initiated
Command:
sed -n '/ERROR/p' server.logExplanation:
-n: Suppresses automatic printing./ERROR/: Matches lines containingERROR.p: Prints matched lines.
Command:
sed 's/ERROR/ALERT/g' server.logExplanation:
s/ERROR/ALERT/: SubstitutesERRORwithALERT.g: Replaces all occurrences in a line.
Command:
sed '/INFO/d' server.logExplanation:
/INFO/: Matches lines containingINFO.d: Deletes the matched lines.
Command:
sed '/WARNING/a\Please monitor the memory usage.' server.logExplanation:
/WARNING/: Matches lines withWARNING.a: Appends text after the matching line.
Command:
sed 'y/aeiou/AEIOU/' server.logExplanation:
y/aeiou/AEIOU/: Converts all vowels to uppercase.
You need logs between 10:00 and 12:00.
Command:
sed -n '/10:00/,/12:00/p' server.logExplanation:
/10:00/,/12:00/: Matches lines between these patterns inclusively.p: Prints matched lines.
Command:
sed -n '/ERROR/w errors.log' server.logExplanation:
/ERROR/: Matches lines withERROR.w errors.log: Writes matched lines toerrors.log.
Command:
sed '/Disk space low/c\ERROR: Critical disk space issue' server.logExplanation:
/Disk space low/: Matches the line.c: Replaces the entire line.
You can combine multiple sed commands using -e or a script file.
Example:
sed -e 's/ERROR/ALERT/g' -e '/INFO/d' -e '/WARNING/a\Take action immediately.' server.logTo modify the file directly, use the -i option:
sed -i 's/ERROR/ALERT/g' server.logYou have a CSV file named employees.csv containing employee details:
ID,Name,Department,Salary,Join Date
101,John Doe,Engineering,50000,2020-01-15
102,Jane Smith,HR,45000,2019-03-10
103,Emily Davis,Engineering,55000,2021-05-22
104,Michael Brown,Sales,40000,2018-08-19
105,Sarah Wilson,Engineering,60000,2017-11-05You need to perform several tasks on this data using sed.
Command:
sed '1d' employees.csvExplanation:
1d: Deletes the first line (header).
Command:
sed -n '/Engineering/p' employees.csvExplanation:
/Engineering/: Matches lines with "Engineering".p: Prints the matched lines.
Command:
sed 's/,HR,/,Human Resources,/g' employees.csvExplanation:
s/,HR,/,Human Resources,/g: Substitutes the exact match of,HR,with,Human Resources,.g: Ensures all occurrences in the file are replaced.
Assume the file doesn't include a header. Add a "Years of Experience" column based on the join date.
Command:
sed 's/$/,Years of Experience/' employees.csvExplanation:
$: Matches the end of the line.,Years of Experience: Appends this text at the end.
For data rows, calculate the years of experience (assuming the current year is 2025).
Command:
sed -E 's/([0-9]{4})-[0-9]{2}-[0-9]{2}$/,&,\4y/' employees.csvCommand:
sed -E 's/([0-9]+)$/logix/"
Let's fix that last example for clarity and correctness.
---
#### **Task 5: Increase Salaries by 10% for All Employees**
To increase the salary field (4th column) by 10%, we need to process numeric values dynamically. Since `sed` alone does not handle arithmetic well, we use a pipeline with `awk` for the math but focus on the `sed` transformation logic.
**Step 1: Extract Salaries**
Command:
```bash
sed -E 's/^([^,]+,[^,]+,[^,]+,)([0-9]+)(,.*)$/echo "\1$(echo "\2*1.1" | bc)\3"/e' employees.csvExplanation:
-E: Enables extended regex for easier grouping.^([^,]+,[^,]+,[^,]+,): Captures the first three columns (everything before the salary).([0-9]+): Captures the numeric salary value.(,.*)$: Captures the rest of the line.echoandbc: Embedded commands calculate a 10% increase.
Step 2: Inline Editing
Use the -i flag to edit the file in place:
sed -i -E '...' employees.csvCommand:
sed -E '/,[0-9]{4}-[0-9]{2}-[0-9]{2}$/!d; /,201[0-9]-[0-9]{2}-[0-9]{2}$/d' employees.csvExplanation:
/,[0-9]{4}-[0-9]{2}-[0-9]{2}$/!d: Ensures we match rows with valid dates./,201[0-9]-[0-9]{2}-[0-9]{2}$/d: Deletes rows where the year is before 2020.
Command:
sed -E 's/([0-9]{4})-([0-9]{2})-([0-9]{2})/\2\/\3\/\1/' employees.csvExplanation:
([0-9]{4})-([0-9]{2})-([0-9]{2}): Captures the date components.\2/\3/\1: Rearranges and formats them asMM/DD/YYYY.
Extract employees in "Engineering" earning more than $50,000.
Command:
sed -n '/Engineering/ { /,5[5-9][0-9][0-9][0-9]/p }' employees.csvExplanation:
/Engineering/: Matches the department./,[5-9][0-9]{4}/: Ensures the salary is above 50,000.p: Prints matched lines.