mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-27 02:30:10 +08:00
translated sed cmd
This commit is contained in:
parent
b5c264bbbe
commit
9f7d05031e
@ -1,32 +1,32 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (hello-wn)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (How to Delete Lines from a File Using the sed Command)
|
||||
[#]: via: (https://www.2daygeek.com/linux-remove-delete-lines-in-file-sed-command/)
|
||||
[#]: author: (Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/)
|
||||
[#]: collector: "lujun9972"
|
||||
[#]: translator: "hello-wn"
|
||||
[#]: reviewer: " "
|
||||
[#]: publisher: " "
|
||||
[#]: url: " "
|
||||
[#]: subject: "How to Delete Lines from a File Using the sed Command"
|
||||
[#]: via: "https://www.2daygeek.com/linux-remove-delete-lines-in-file-sed-command/"
|
||||
[#]: author: "Magesh Maruthamuthu https://www.2daygeek.com/author/magesh/"
|
||||
|
||||
How to Delete Lines from a File Using the sed Command
|
||||
如何使用 sed 命令删除文件中的行
|
||||
======
|
||||
|
||||
Sed command stands for Stream Editor, is used to perform basic text transformations in Linux.
|
||||
Sed 代表<ruby>流编辑器<rt>Stream Editor</rt></ruby>,常用于 Linux 中基本的文本处理。
|
||||
|
||||
sed is one of the important command, which plays major role for file manipulation. It can be used to delete or remove specific lines which matches a given pattern.
|
||||
sed 命令是 Linux 中的重要命令之一,在文件处理方面有着重要作用。可用于删除或移动与给定模式匹配的特定行,还可以删除文件中的特定行。
|
||||
|
||||
Also, it’s used to remove a particular line in a file.
|
||||
它还能够从文件中删除表达式,文件可以通过指定分隔符(例如逗号、制表符或空格)进行标识。
|
||||
|
||||
It’s capable to delete expressions as well from a file, which can be identified by a specifying delimiter (such as a comma, tab, or space).
|
||||
|
||||
There are fifteen examples are listed in this article, which helps you to become a master in sed command.
|
||||
|
||||
If you understand and remember all these commands that can be useful in many ways. Also, it saves lot of time when you have some requirements to perform sed command.
|
||||
本文列出了 15 个使用范例,它们可以帮助你掌握 sed 命令。
|
||||
|
||||
**`Note:`**` ` Since it’s demonstration purpose so, i use sed command without `-i` option which prints the contents of the file on Linux terminal by removing the lines.
|
||||
如果你能理解并且记住这些命令,在你需要使用 sed 时,这些命令就能派上用场,帮你节约很多时间。
|
||||
|
||||
But, if you would like to remove the lines from the source file in real environment then use `-i` option with sed command.
|
||||
**`注意:`**` ` 为了方便演示,我在执行 sed 命令时,不使用 `-i` 选项,因为这个选项会直接修改文件内容。
|
||||
|
||||
To test this, i have created the sed-demo.txt file and added the following contents with line number for better understanding.
|
||||
但是,如果你想在实际环境中从源文件中删除行,请在 sed 命令中使用 `-i` 选项。
|
||||
|
||||
演示之前,我创建了 sed-demo.txt 文件,并添加了以下内容和相应行号以便更好地理解。
|
||||
|
||||
```
|
||||
# cat sed-demo.txt
|
||||
@ -43,19 +43,19 @@ To test this, i have created the sed-demo.txt file and added the following conte
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 1) How to Delete First Line from a File?
|
||||
### 1) 如何删除文件的第一行?
|
||||
|
||||
If you would like to delete first line from a file, use the following syntax.
|
||||
使用以下语法删除文件首行。
|
||||
|
||||
**`N`**` ` denotes Nth line in a file and d option in sed command is used to delete a line.
|
||||
**`N`**` ` 表示文件中的第 N 行,`d`选项在 sed 命令中用于删除一行。
|
||||
|
||||
**Syntax:**
|
||||
**语法:**
|
||||
|
||||
```
|
||||
sed 'Nd' file
|
||||
```
|
||||
|
||||
The below sed command removes the first line in sed-demo.txt file.
|
||||
使用以下 sed 命令删除 sed-demo.txt 中的第一行。
|
||||
|
||||
```
|
||||
# sed '1d' sed-demo.txt
|
||||
@ -71,13 +71,13 @@ The below sed command removes the first line in sed-demo.txt file.
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 2) How to Delete Last Line from a File?
|
||||
### 2) 如何删除文件的最后一行?
|
||||
|
||||
If you would like to delete first line from a file, use the following syntax.
|
||||
使用以下语法删除文件最后一行。
|
||||
|
||||
The **`$`**` ` denotes the last line of a file.
|
||||
**`$`**` ` 符号表示文件的最后一行。
|
||||
|
||||
The below sed command removes the last line in sed-demo.txt file.
|
||||
使用以下 sed 命令删除 sed-demo.txt 中的最后一行。
|
||||
|
||||
```
|
||||
# sed '$d' sed-demo.txt
|
||||
@ -93,9 +93,9 @@ The below sed command removes the last line in sed-demo.txt file.
|
||||
9 Ubuntu
|
||||
```
|
||||
|
||||
### 3) How to Delete Particular Line from a File?
|
||||
### 3) 如何删除指定行?
|
||||
|
||||
The below sed command removes the third line in sed-demo.txt file.
|
||||
使用以下 sed 命令删除 sed-demo.txt 中的第 3 行。
|
||||
|
||||
```
|
||||
# sed '3d' sed-demo.txt
|
||||
@ -111,9 +111,9 @@ The below sed command removes the third line in sed-demo.txt file.
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 4) How to Delete Range of Lines from a File?
|
||||
### 4) 如何删除指定范围内的行?
|
||||
|
||||
The below sed command removes the lines ranging from 5 to 7.
|
||||
使用以下 sed 命令删除 sed-demo.txt 中的第 5 到 7 行。
|
||||
|
||||
```
|
||||
# sed '5,7d' sed-demo.txt
|
||||
@ -127,11 +127,11 @@ The below sed command removes the lines ranging from 5 to 7.
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 5) How to Delete Multiple Lines from a File?
|
||||
### 5) 如何删除多行内容?
|
||||
|
||||
The sed command is capable to removes set of given lines.
|
||||
sed 命令能够删除给定行的集合。
|
||||
|
||||
In this example, the following sed command removes 1st line, 5th line, 9th line, and last line.
|
||||
本例中,下面的 sed 命令删除了第 1 行、第 5 行、第 9 行和最后一行。
|
||||
|
||||
```
|
||||
# sed '1d;5d;9d;$d' sed-demo.txt
|
||||
@ -144,9 +144,9 @@ In this example, the following sed command removes 1st line, 5th line, 9th line,
|
||||
8 Debian
|
||||
```
|
||||
|
||||
### 5a) How to Delete Lines Other Than the Specified Range from a File?
|
||||
### 5a) 如何删除指定范围以外的行?
|
||||
|
||||
Use the following sed command to remove all the lines from the file only except specified range.
|
||||
使用以下 sed 命令删除 sed-demo.txt 中第 3 到 6 行范围以外的所有行。
|
||||
|
||||
```
|
||||
# sed '3,6!d' sed-demo.txt
|
||||
@ -157,9 +157,9 @@ Use the following sed command to remove all the lines from the file only except
|
||||
6 Arch Linux
|
||||
```
|
||||
|
||||
### 6) How to Delete Empty or Blank Lines from a File?
|
||||
### 6) 如何删除空行?
|
||||
|
||||
The following sed command removes the empty or blank lines from sed-demo.txt file.
|
||||
使用以下 sed 命令删除 sed-demo.txt 中的空行。
|
||||
|
||||
```
|
||||
# sed '/^$/d' sed-demo.txt
|
||||
@ -176,9 +176,9 @@ The following sed command removes the empty or blank lines from sed-demo.txt fil
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 7) How to Delete Lines That Contain a Pattern from a File?
|
||||
### 7) 如何删除包含某个<ruby>表达式<rt>Pattern</rt></ruby>的行?
|
||||
|
||||
The following sed command removes the lines in sed-demo.txt file which match the **`System`**` ` pattern.
|
||||
使用以下 sed 命令删除 sed-demo.txt 中匹配到 **`System`**` ` 表达式的行。
|
||||
|
||||
```
|
||||
# sed '/System/d' sed-demo.txt
|
||||
@ -193,9 +193,9 @@ The following sed command removes the lines in sed-demo.txt file which match the
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 8) How to Delete Lines That Containing One of Multiple Strings from a File?
|
||||
### 8) 如何删除包含字符串集合中某个字符串的行?
|
||||
|
||||
The following sed command removes the lines in sed-demo.txt file which match the **`System`**` ` or **`Linux`**` ` pattern.
|
||||
使用以下 sed 命令删除 sed-demo.txt 中匹配到 **`System`**` ` 或 **`Linux`**` ` 表达式的行。
|
||||
|
||||
```
|
||||
# sed '/System\|Linux/d' sed-demo.txt
|
||||
@ -209,11 +209,9 @@ The following sed command removes the lines in sed-demo.txt file which match the
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 9) How to Delete Lines That Begin with Specified Character from a File?
|
||||
### 9) 如何删除以指定字符开头的行?
|
||||
|
||||
The following sed command removes all the lines that start with given character.
|
||||
|
||||
To test this, i have created another file called sed-demo-1.txt with following contents.
|
||||
为了测试,我创建了 sed-demo-1.txt 文件,并添加了以下内容。
|
||||
|
||||
```
|
||||
# cat sed-demo-1.txt
|
||||
@ -230,7 +228,7 @@ Arch Linux - 1
|
||||
3 4 5 6
|
||||
```
|
||||
|
||||
The following sed command removes all the lines that start with character **`R`**` `.
|
||||
使用以下 sed 命令删除以 **`R`**` ` 字符开头的所有行。
|
||||
|
||||
```
|
||||
# sed '/^R/d' sed-demo-1.txt
|
||||
@ -245,7 +243,7 @@ Arch Linux - 1
|
||||
3 4 5 6
|
||||
```
|
||||
|
||||
The following sed command removes all the lines that start with character either **`R`**` ` or **`F`**` `.
|
||||
使用以下 sed 命令删除 **`R`**` ` 或者 **`F`**` ` 字符开头的所有行。
|
||||
|
||||
```
|
||||
# sed '/^[RF]/d' sed-demo-1.txt
|
||||
@ -259,9 +257,9 @@ Arch Linux - 1
|
||||
3 4 5 6
|
||||
```
|
||||
|
||||
### 10) How to Delete Lines That End with Specified Character from a File?
|
||||
### 10) 如何删除以指定字符结尾的行?
|
||||
|
||||
The following sed command removes all the lines that end with character **`m`**` `.
|
||||
使用以下 sed 命令删除 **`m`**` ` 字符结尾的所有行。
|
||||
|
||||
```
|
||||
# sed '/m$/d' sed-demo.txt
|
||||
@ -276,7 +274,7 @@ The following sed command removes all the lines that end with character **`m`**`
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
The following sed command removes all the lines that end with character either **`x`**` ` or **`m`**` `.
|
||||
使用以下 sed 命令删除 **`x`**` ` 或者 **`m`**` ` 字符结尾的所有行。
|
||||
|
||||
```
|
||||
# sed '/[xm]$/d' sed-demo.txt
|
||||
@ -290,9 +288,9 @@ The following sed command removes all the lines that end with character either *
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 11) How to Delete All Lines That Start with Capital Letters
|
||||
### 11) 如何删除所有大写字母开头的行?
|
||||
|
||||
Use the following sed command to remove all the lines that start with entirely in capital letters.
|
||||
使用以下 sed 命令删除所有大写字母开头的行。
|
||||
|
||||
```
|
||||
# sed '/^[A-Z]/d' sed-demo-1.txt
|
||||
@ -303,9 +301,9 @@ ubuntu
|
||||
3 4 5 6
|
||||
```
|
||||
|
||||
### 12) How to Delete a Matching Pattern Lines with Specified Range in a File?
|
||||
### 12) 如何删除指定范围内匹配到<ruby>表达式<rt>Pattern</rt></ruby>的行?
|
||||
|
||||
The below sed command removes the pattern **`Linux`**` ` only if it is present in the lines from 1 to 6.
|
||||
使用以下 sed 命令删除第 1 到 6 行中包含 **`Linux`**` ` 表达式的行。
|
||||
|
||||
```
|
||||
# sed '1,6{/Linux/d;}' sed-demo.txt
|
||||
@ -320,9 +318,9 @@ The below sed command removes the pattern **`Linux`**` ` only if it is present i
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 13) How to Delete Pattern Matching Line and also the Next Line?
|
||||
### 13) 如何删除匹配到<ruby>表达式<rt>Pattern</rt></ruby>的行及其下一行?
|
||||
|
||||
Use the following sed command to delete the line which containing the pattern ‘System’ and also the next line.
|
||||
使用以下 sed 命令删除包含 `System` 表达式的行以及它的下一行。
|
||||
|
||||
```
|
||||
# sed '/System/{N;d;}' sed-demo.txt
|
||||
@ -337,9 +335,9 @@ Use the following sed command to delete the line which containing the pattern
|
||||
10 openSUSE
|
||||
```
|
||||
|
||||
### 14) How Delete lines that contains Digits from a File?
|
||||
### 14) 如何删除包含数字的行?
|
||||
|
||||
The below sed command removes all the lines that contains digits.
|
||||
使用以下 sed 命令删除所有包含数字的行。
|
||||
|
||||
```
|
||||
# sed '/[0-9]/d' sed-demo-1.txt
|
||||
@ -353,7 +351,7 @@ debian
|
||||
ubuntu
|
||||
```
|
||||
|
||||
The below sed command removes all the lines Begin with digits.
|
||||
使用以下 sed 命令删除所有以数字开头的行。
|
||||
|
||||
```
|
||||
# sed '/^[0-9]/d' sed-demo-1.txt
|
||||
@ -368,7 +366,7 @@ ubuntu
|
||||
Arch Linux - 1
|
||||
```
|
||||
|
||||
The below sed command removes all the lines End with digits.
|
||||
使用以下 sed 命令删除所有以数字结尾的行。
|
||||
|
||||
```
|
||||
# sed '/[0-9]$/d' sed-demo-1.txt
|
||||
@ -383,9 +381,9 @@ ubuntu
|
||||
2 - Manjaro
|
||||
```
|
||||
|
||||
### 15) How Delete lines that contains Alphabetic Characters from a File?
|
||||
### 15) 如何删除包含字母的行?
|
||||
|
||||
The below sed command removes all the lines that contains alphabetic characters.
|
||||
使用以下 sed 命令删除所有包含字母的行。
|
||||
|
||||
```
|
||||
# sed '/[A-Za-z]/d' sed-demo-1.txt
|
||||
@ -398,7 +396,7 @@ via: https://www.2daygeek.com/linux-remove-delete-lines-in-file-sed-command/
|
||||
|
||||
作者:[Magesh Maruthamuthu][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
译者:[hello-wn](https://github.com/hello-wn)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
Loading…
Reference in New Issue
Block a user