Sed or Stream Editor provides a lot of useful and dynamic functions. One of the most popular use case for sed is search and replace text in standard input output and text files. In this tutorial we will look various use cases of find and replace in sed.
Sed或Stream Editor提供了许多有用的动态功能。 sed最受欢迎的用例之一是在标准输入输出和文本文件中搜索和替换文本。 在本教程中,我们将研究sed中find和replace的各种用例。
替代命令 (Substitute Command)
Sed have different verbs or commands to operate on given files or standard input lines. Substitute or s
is one of the most popular sed command.
Sed具有不同的动词或命令来对给定的文件或标准输入行进行操作。 替代或s
是最流行的sed命令之一。
句法 (Syntax)
Syntax of sed substitute command is like below.
sed替代命令的语法如下。
sed 'PATTERNs/OLD/NEW/FLAGS' FILENAME
PATTERN
is used to specify the behaviour of substitute command like one at three.PATTERN
用于指定替代命令的行为,例如三分之一。s
is the substitute commands
是替代命令OLD
is the term we are searching for and it may be a regexOLD
是我们要搜索的术语,可能是正则表达式NEW
is the word we will setNEW
是我们要设置的词FLAGS
are used to set settings about sedFLAGS
用于设置有关sed的设置FILENAME
is optional if we provide text from standard input如果我们提供来自标准输入的文本,则
FILENAME
是可选的
一次替换单词(Substitute Word Once)
We can change given term into the word we specify with the simple s/ /
syntax. In this example we will change the linux
into poftut
in file sites.txt
. The output will be printed to the standard output and no change will occur in sites.txt
我们可以使用简单的s/ /
语法将给定术语更改为我们指定的单词。 在本示例中,我们将在poftut
文件sites.txt
linux
更改为sites.txt
。 输出将被打印为标准输出,并且sites.txt
不会发生任何变化
$ sed 's/linux/poftut' sites.txt
替换所有单词 (Substitute All Words)
In previous example we have changes only single match. If we need to change all occurrences of given term we should use g
flag. In this example we will change all occurrences of linux
to poftut
in file sites.txt
and print to the terminal.
在前面的示例中,我们仅更改了单个匹配项。 如果需要更改给定项的所有出现次数,则应使用g
标志。 在这个例子中,我们将改变所有出现linux
到poftut
文件sites.txt
并打印到终端。
$ sed 's/linux/poftut/g' sites.txt
仅替换Word的第二次出现 (Substitute Only 2nd Occurrence of Word)
Another useful substitute use case is changing given period matches. In this example we will change every occurrence of 2 the linux
with poftut
in file sites.txt
and print output to shell. We will put 2
as flag which means change 2nd occurences
另一个有用的替代用例是更改给定的期间匹配。 在此示例中,我们将在文件sites.txt
使用poftut
更改每2个linux
出现,并将输出打印到shell。 我们将2
作为标记,这意味着更改第二次出现
$ sed 's/linux/poftut/g' sites.txt
将输出写入文件 (Write Output To A File)
Upto now we have print output to the shell by default. We can also write changes into new file. We will redirect output to the file named sites2.txt
到目前为止,默认情况下,我们将打印输出输出到外壳。 我们还可以将更改写入新文件。 我们将输出重定向到名为sites2.txt
的文件
$ sed 's/linux/poftut/g' sites.txt > sites2.txt
就地更换 (Replace In Place)
We can also replace given search. This will write changes to the given source file. We will use -i
option.
我们也可以替换给定的搜索。 这会将更改写入给定的源文件。 我们将使用-i
选项。
$ sed -i 's/linux/poftut/g' sites.txt
查找并替换多个文件 (Find And Replace In Multiple Files)
Up to now we have only used single file. We may need to find and replace in multiple files. We can specify the files with glob *
according to their names or extensions. In this example we will made replace all file names starts with sites
and ends with .txt
到目前为止,我们仅使用了单个文件。 我们可能需要在多个文件中查找和替换。 我们可以根据文件名或扩展名使用glob *
指定文件。 在此示例中,我们将替换所有以sites
开头,以.txt
结尾的文件名
$ sed -i 's/linux/poftut/g' sites*.txt
仅当行与给定模式匹配时才替换 (Substitute Only If The Line Matches with Given Pattern)
We can set some conditions for the match and replace. We can look other patterns in the current line for find and replace. In this example we will change lines those have number 1
where find linux
term and set poftut
in file sites.txt
and print to console.
我们可以为匹配和替换设置一些条件。 我们可以在当前行中查找其他模式以进行查找和替换。 在此示例中,我们将更改编号为1
,在其中找到linux
term并在poftut
文件中设置sites.txt
并打印到控制台。
$ sed '/1/s/linux/poftut/g' sites.txt
将DOS换行符回车/换行符转换为Unix格式 (Convert DOS Newlines Carriage Return/Line Feed To Unix Format)
We can convert Windows line ends with the following sed
script. As we know windows line ends provides Carriage Return and Line Feed.
我们可以使用以下sed
脚本转换Windows行尾。 众所周知,windows的行尾提供回车和换行。
$ sed 's/.$//' sites.txt
翻译自: https://www.poftut.com/how-to-use-sed-to-find-and-replace-text-in-files-or-standard-input/