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!
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
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.
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.
💡 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 ✨
🔍 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.