如何使用Sed查找和替换文件或标准输入中的文本

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 command

    s是替代命令

  • OLD is the term we are searching for and it may be a regex

    OLD是我们要搜索的术语,可能是正则表达式

  • NEW is the word we will set

    NEW是我们要设置的词

  • FLAGS are used to set settings about sed

    FLAGS用于设置有关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标志。 在这个例子中,我们将改变所有出现linuxpoftut文件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
LEARN MORE  Powershell Comparison Operators Like Equal, Greater, Lesser, Contains, Regex
了解更多Powershell比较运算符,例如Equal,Greater,Lesser,Contains,Regex

翻译自: https://www.poftut.com/how-to-use-sed-to-find-and-replace-text-in-files-or-standard-input/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值