In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
When you are working with text files, you will most likely need to find and replace strings in the file. The sed command is primarily used to replace text in a file. In Linux this can be done by using the sed command and awk command.
In this tutorial, we'll show you how to do this using the sed command and then discuss awk command related.
What is the sed command?
The sed command stands for Stream Editor and is used to perform basic text operations on Linux. It can perform various functions such as searching, finding, modifying, inserting or deleting files.
In addition, it can also perform complex regular expression matching.
It can be used for the following purposes:
Finds and replaces content that matches the given format.
Finds and replaces content that matches the given format on the specified row.
Finds and replaces content matching the given format in all rows.
Search for and replace two different patterns simultaneously.
The fifteen examples listed in this article will help you master the sed command.
To delete lines from a file using the sed command, go to the following article.
Note: Since this is a demo article, we use the sed command without the-i option, which deletes lines and prints file contents in Linux terminals.
However, in practice, if you want to delete lines from a source file, use sed with the-i option.
Common sed substitution string syntax.
sed -i 's/Search_String/Replacement_String/g' Input_File
First we need to understand sed syntax to do this. Please refer to the relevant details.
sed: This is a Linux command.
-i: This is an option of the sed command, what does it do? By default, sed prints results to standard output. When you add this option using sed, it modifies the file in place. When you add a suffix (e.g., -i.bak), a backup of the original file is created.
s: The letter s is a substitution command.
Search_String: Search for a given string or regular expression.
Replacement_String: The string to replace.
g: Global replacement flag. By default, the sed command replaces the first occurrence of a pattern in each line; it does not replace other matches in the line. However, when the replacement flag is provided, all matches are replaced.
/: Separator.
Input_File: The name of the file to perform the operation on.
Let's look at some common examples of using sed to search and convert text in files.
We have created the following files for demonstration.
# cat sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
1)How to Find and Replace "First Time" Pattern Matches in a Line
The sed command below replaces unix in the file with linux. This changes only the first instance of each line pattern.
# sed 's/unix/linux/' sed-test.txt1 Unix linux unix 232 linux Linux 343 linuxlinux UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
2)How to find and replace patterns that appear "Nth time" in each row
Use flags such as/1,/2.../n in rows instead of corresponding matches.
The sed command below replaces the second instance of unix mode with linux in one line.
# sed 's/unix/linux/2' sed-test.txt1 Unix unix linux 232 linux Linux 343 linuxunix UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
3)How to search for and replace all pattern instances in a row
The sed command below replaces all instances of unix format with linux because g is a global replacement flag.
# sed 's/unix/linux/g' sed-test.txt1 Unix linux linux 232 linux Linux 343 linuxlinux UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
4)How to find and replace all matching pattern instances in a row starting with "Nth"
The sed command below replaces matching instances starting at the "Nth" of the pattern in one line.
# sed 's/unix/linux/2g' sed-test.txt1 Unix unix linux 232 linux Linux 343 linuxunix UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
5)Search and replace patterns at specific line numbers
# sed '3 s/unix/linux/' sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxlinux UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
6)Search and replace patterns between specific ranges of line numbers
You can specify a range of line numbers to replace strings.
The sed command below replaces Unix mode with linux between lines 1 and 3.
# sed '1,3 s/unix/linux/' sed-test.txt1 Unix linux unix 232 linux Linux 343 linuxlinux UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
7)How to find and modify patterns in the last line
The sed command below allows you to replace matching strings only on the last line.
The sed command below replaces Linux mode with Unix only in the last line.
# sed '$ s/Linux/Unix/' sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux /bin/bash CentOS Linux OSUnix is free and opensource operating system
8)How to find and replace only the correct pattern matches in a row
You may have noticed that the substring linuxunix is replaced with linuxlinux in Example 6. If you only want to change the correct match, use this delimiter\b at both ends of the search string.
# sed '1,3 s/\bunix\b/linux/' sed-test.txt1 Unix linux unix 232 linux Linux 343 linuxunix UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
9)How to search and replace patterns case-insensitive
As you all know, Linux is case-sensitive. To match case-insensitive patterns, use the I flag.
# sed 's/unix/linux/gI' sed-test.txt1 linux linux linux 232 linux Linux 343 linuxlinux linuxLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
10)How to find and replace strings containing delimiters
When you search for and replace delimited strings, we need to use the backslash\to unescape.
In this example, we will replace/bin/bash with/usr/bin/fish.
# sed 's/\/bin\/bash/\/usr\/bin\/fish/g' sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux /usr/bin/fish CentOS Linux OSLinux is free and opensource operating system
The sed command above works as expected, but it looks bad. To simplify, most people use vertical bars| As a locator for regular expressions. So, I suggest you use it.
# sed 's|/bin/bash|/usr/bin/fish/|g' sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux /usr/bin/fish/ CentOS Linux OSLinux is free and opensource operating system
11)How to find and replace numbers in a given pattern
Similarly, numbers can be replaced by patterns. The sed command below replaces all numbers with numbers [0-9].
# sed 's/[0-9]/number/g' sed-test.txtnumber Unix unix unix numbernumbernumber linux Linux numbernumbernumber linuxunix UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
12)How to find and replace only two numbers with patterns
If you want to replace two digits with patterns, use the sed command below.
# sed 's/\b[0-9]\{2\}\b/number/g' sed-test.txt1 Unix unix unix number2 linux Linux number3 linuxunix UnixLinuxlinux /bin/bash CentOS Linux OSLinux is free and opensource operating system
13)How to print only replaced lines with sed
If you want to display only changed lines, use the sed command below.
p -It outputs the replaced line twice on the terminal.
-n -It suppresses duplicate lines generated by the p marker.
# sed -n 's/Unix/Linux/p' sed-test.txt1 Linux unix unix 233 linuxunix LinuxLinux
14)How to run multiple sed commands simultaneously
The following sed command detects and replaces two different modes simultaneously.
The sed command below searches for linux unix and CentOS modes and replaces them all at once with LINUX UNIX and RHEL8.
# sed -e 's/linuxunix/LINUXUNIX/g' -e 's/CentOS/RHEL8/g' sed-test.txt1 Unix unix unix 232 linux Linux 343 LINUXUNIX UnixLinuxlinux /bin/bash RHEL8 Linux OSLinux is free and opensource operating system
The sed command below searches for and replaces two different patterns, one string at a time.
The following sed command searches for linuxunix and CentOS patterns, replacing them with Fedora30.
# sed -e 's/\(linuxunix\|CentOS\)/Fedora30/g' sed-test.txt1 Unix unix unix 232 linux Linux 343 Fedora30 UnixLinuxlinux /bin/bash Fedora30 Linux OSLinux is free and opensource operating system
15)How to find and replace entire rows if a given pattern matches
If the pattern matches, you can use the sed command to replace the entire line with a new one. This can be done by using the C flag.
# sed '/OS/ c\New Line' sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxNew LineLinux is free and opensource operating system
16)How to search for and replace matching pattern rows
In sed you can specify the appropriate pattern for the line. If the pattern matches, the sed command searches for the string to be replaced.
The sed command below first looks for lines with OS patterns and then replaces the word Linux with ArchLinux.
# sed '/OS/ s/Linux/ArchLinux/' sed-test.txt1 Unix unix unix 232 linux Linux 343 linuxunix UnixLinuxlinux /bin/bash CentOS ArchLinux OSLinux is free and opensource operating system
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.