Rakesh Jain Profile picture
Jul 7 25 tweets 3 min read Twitter logo Read on Twitter
Mastering Linux sed command!

A Thread with 20 most useful examples 👇
What is SED!

sed stands for "stream editor," and it is a powerful text processing tool available in Unix-like operating systems, including Linux. It operates on text files or input streams and performs various editing operations based on specified commands.
Here are 20 practical examples of using the sed command in Linux 👇
1/20 - Remove leading/trailing whitespace:

$ echo " Hello, World! " | sed 's/^[[:space:]]*//;s/[[:space:]]*$//'

Output: "Hello, World!"
2/20 - Replace a string in a file:

$ sed -i 's/foo/bar/g' file.txt

# Replaces all occurrences of "foo" with "bar" in file.txt
3/20 - Print specific lines from a file:

$ sed -n '5,10p' file.txt

# Prints lines 5 to 10 from file.txt
4/20 - Delete blank lines from a file:

$ sed '/^$/d' file.txt

# Deletes all blank lines from file.txt
5/20 - Find and replace using regular expressions:

$ sed 's/\(word\)\([0-9]\)/\2\1/g' file.txt

# Replaces "word1" with "1word" in file.txt
6/20 - Append text to the end of each line:

$ sed 's/$/ - The End/' file.txt

# Appends " - The End" to the end of each line in file.txt
7/20 - Delete lines matching a pattern:

$ sed '/pattern/d' file.txt

# Deletes all lines containing "pattern" from file.txt
8/20 - Print specific columns from a CSV file:

$ sed 's/,/\t/g' file.csv | cut -f 2,4

# Prints the 2nd and 4th columns of a CSV file, separated by tabs
9/20 - Replace tabs with spaces:

$ sed 's/\t/ /g' file.txt

# Replaces tabs with four spaces in file.txt
10/20 - Insert a line before/after a pattern:

$ sed '/pattern/i This line will be inserted before the pattern' file.txt

# Inserts a line before the line containing "pattern" in file.txt
11/20 - Delete the last line of a file:

$ sed '$d' file.txt

# Deletes the last line from file.txt
12/20 - Count the number of occurrences of a pattern:

$ sed -n 's/pattern/&/pg' file.txt | wc -l

# Counts the number of occurrences of "pattern" in file.txt
13/20 - Perform case-insensitive search and replace:

$ sed 's/foo/bar/ig' file.txt

# Replaces all occurrences of "foo" with "bar" (case-insensitive) in file.txt
14/20 - Reverse the order of lines in a file:

$ sed '1!G;h;$!d' file.txt

# Reverses the order of lines in file.txt
15/20 - Delete lines between two patterns:

$ sed '/start/,/end/d' file.txt

# Deletes all lines between "start" and "end" (inclusive) from file.txt
16/20 - Print line numbers along with matching lines:

$ sed -n '/pattern/=' file.txt

# Prints line numbers of lines containing "pattern" in file.txt
17/20 - Delete all lines except the first and last:

$ sed '1n; $!{H;d};x' file.txt

# Deletes all lines except the first and last from file.txt
18/20 - Extract text between two patterns:

$ sed -n '/start/,/end/p' file.txt

# Prints the lines between "start" and "end" (inclusive) from file.txt
19/20 - Increment a number in a file:

$ sed 's/[0-9]\+/\n&\n/g;:a; s/\n0*\([0-9]\+\)\n\n/\n\1\n/;ta' file.txt

# Increments a number found in file.txt by 1
20/20 - Limit the number of replacements per line:

$ echo "foo foo foo foo" | sed 's/foo/bar/2'
Output: "bar bar foo foo"

# Replaces the first two occurrences of "foo" with "bar"
That's it! I hope you find these examples helpful for mastering the sed command in Linux. Let me know if you have any questions!
Retweet the thread if you find it useful. Thanks!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Rakesh Jain

Rakesh Jain Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @devops_tech

Jul 8
Mastering Networking in Linux 🐧🖥️🌐

A #Twitter Thread with 20 useful examples 👇
1/ Hey, #Linux enthusiasts! Let's dive into mastering networking in Linux. 🖥️🌐

In this thread, I'll cover key concepts and commands with examples to help you become a networking pro. Let's get started! #Networking #LinuxNetworking
2/ First up, let's talk about IP addressing. Use 'ifconfig' or 'ip addr' to view network interfaces and their IP addresses.

For example: "ifconfig eth0" or "ip addr show eth0"

will display IP details for the eth0 interface. #Linux #IPaddress
Read 23 tweets
Jul 6
Mastering Linux Logical Volume Manager aka LVM!

A Thread with examples 👇
What is LVM?

LVM, or Logical Volume Manager, is a powerful disk management tool for Linux systems. It allows for dynamic allocation and resizing of logical volumes, making it easier to manage storage space.
Here are 20 LVM commands explained 👇
Read 25 tweets
Jul 5
What is Rsync! How it works!

A Thread with 15 most useful examples 👇
Rsync - Efficient File Synchronization 🔄

1/7: Rsync is a file synchronization tool used to copy and update files between different locations. It works by transferring only the differences between the source and destination, saving time & bandwidth.

#Rsync #FileSynchronization
2/7: When you run rsync, it compares file sizes and modification times to determine which files need to be updated. It then copies only the changed parts of the files, rather than transferring the entire file.

#Rsync #FileSynchronization
Read 26 tweets
Jul 4
Find Command in Linux!

A Mega Example thread 👇
1/ 💡 Example 1: Find files by name in the current directory and its subdirectories.

$ find . -name "filename"
2/ 💡 Example 2: Search for files with a specific extension.

$ find . -name "*.extension"
Read 28 tweets
Jul 3
What is AWK!

The Power of AWK: 15 Essential Examples 🐦

A Thread 👇
💡 AWK, a versatile programming language, simplifies data manipulation.
Following examples will showcase its power to process files effortlessly! Level up your text processing game! #AWKMagic ✨
1️⃣ Print Whole Line 🖨️:

awk '{print}' file.txt
Read 26 tweets
Jul 2
TCPdump: A Powerful Network Packet Analyzer

A Thread with examples 👇
What is TCPDUMP?

🔍 TCPdump is a widely-used command-line packet analyzer for capturing & analyzing network traffic. It's an essential tool for network troubleshooting and security analysis.

Let's explore some common use cases with these 15 examples! #TCPdump #NetworkAnalysis
1) Capture All Traffic:

$ sudo tcpdump -i eth0

Capture traffic on interface eth0 and display packet details in real-time.
Read 25 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(