linux sed命令_Linux sed命令用法与示例

linux sed命令

Sed, short for Stream EDitor, is a command that is used to perform text transformations and manipulations on a file. Some of these transformations include searching and replacing text. With Linux sed command, you can manipulate and edit text files without even opening them. In this tutorial, you will learn how to manipulate text files using sed command.

Sed是Stream EDitor的缩写,是用于对文件执行文本转换和操作的命令。 其中一些转换包括搜索和替换文本。 使用Linux sed命令,即使不打开文本文件也可以对其进行操作和编辑。 在本教程中,您将学习如何使用sed命令操作文本文件。

Linux sed命令语法 (Linux sed command syntax)

$ sed  {OPTIONS} filename

In this tutorial, we will be using linuxgeek.txt file as our reference file.

在本教程中,我们将使用linuxgeek.txt文件作为参考文件。

# cat linuxgeek.txt

Output

输出量

1.替换或替换字符串 (1. Substituting or replacing a string)

In most cases, sed command is used for replacing strings of text.

在大多数情况下,sed命令用于替换文本字符串。

The command below replaces ‘Linux‘ with ‘Unix‘.

以下命令将“ Linux ”替换为“ Unix ”。

# sed 's/Linux/Unix/' linuxgeek.txt
  • The s in the command is the replacement indicator.

    命令中的s是替换指示器。
  • The / are the delimiters

    /是分隔符
  • Linux is the search term

    Linux是搜索词
  • unix is the replacement term

    unix是替代术语

By default, sed command replaces only the first occurrence in a line. The subsequent occurrences will not be substituted or replaced.

缺省情况下,sed命令仅替换line中的第一个匹配项。 随后出现的情况将不会被替换或替换。

Output

输出量

2.替换或替换一行中的第n个事件 (2. Substituting or replacing the nth occurrence in a line)

If you want to replace the second string occurrence in a line, use the /2 flag as shown.

如果要替换一行中的第二个字符串,请使用/2标志,如图所示。

# sed 's/Linux/Unix/2' linuxgeek.txt

Output

输出量

3.替换或替换文件中所有出现的模式 (3. Substituing or replacing all occurrences of the pattern in a file)

To replace all the occurrences of the search pattern in the file, use the /g flag. The /g is the global replacement.

要替换文件中所有出现的搜索模式,请使用/g标志。 /g是全局替换。

# sed 's/Linux/Unix/g' linuxgeek.txt

Output

输出量

4.仅替换或替换在特定行中出现的所有字符串 (4. Substituting or replacing all occurrences of a string in a specific line only)

If you want to replace all occurrences of a string in a specific line, say in line 2, use the syntax below

如果要替换特定行(例如第2行)中出现的所有字符串,请使用以下语法

# sed '2 s/Linux/Unix/g' linuxgeek.txt

Output

输出量

5.在特定范围的行中替换或替换字符串 (5. Substituting or replacing a string in a specific range of lines)

If you want to specify the range of lines that the string replacement will occur, use the example shown below. The example instructs the replacement to occur from lines 1 -3

如果要指定将发生字符串替换的行范围,请使用以下示例。 该示例指示替换发生在第1 -3行

# sed '1,3 s/Linux/Unix/g' linuxgeek.txt

Output

输出量

Additionally, you can replace text from a specific line to the end of the file as shown

此外,您可以将文本从特定行替换到文件末尾,如图所示

# sed '2,$ s/Linux/Unix/g' linuxgeek.txt

What the command does is that it replaces all the occurrences from the 2nd line to the last line of the file.

该命令的作用是替换从文件的第二行到最后一行的所有匹配项。

Output

输出量

6.括号内每个单词的第一个字符 (6. Parenthesize the first character of every word)

The example below demonstrates how you can put parenthesis on every first character of a word in a line.

下面的示例演示如何在一行的每个单词的第一个字符上加上括号。

$ echo "Hey Guys, Welcome To Linux Operating System" | sed 's/\(\b[A-Z]\)/\(\1\)/g'

Output

输出量

7.从文件中删除行 (7. Delete lines from a file )

You can also use the linux sed command to delete lines in a file.

您也可以使用linux sed命令删除文件中的行。

Examples

例子

Syntax:

句法:

$ sed 'nd' filename.txt

To delete the 3rd line in the file execute

要删除文件中的第三行,请执行

$ sed '3d' linuxgeek.txt

Output

输出量

To delete a range of lines, say from line 3 to line 5 , run the command:

要删除行的范围(例如,从第3行到第5行),请运行以下命令:

$ sed '3,5d' linuxgeek.txt

Output

输出量

翻译自: https://www.journaldev.com/28984/linux-sed-command-examples

linux sed命令

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值