使用 sed 从文本文件中删除特定行号?
我想从文件中删除一个或多个特定行号。我将如何使用 sed 来做到这一点?
I want to delete one or more specific line numbers from a file. How would I do this using sed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果要删除第 5 行到第 10 行以及第 12 行:
这会将结果打印到屏幕上。如果要将结果保存到同一个文件中:
这会将未修改的文件存储为
file.bak
,并删除给定的行。注意:行号从 1 开始。文件的第一行是 1,而不是 0。
If you want to delete lines from 5 through 10 and line 12th:
This will print the results to the screen. If you want to save the results to the same file:
This will store the unmodified file as
file.bak
, and delete the given lines.Note: Line numbers start at 1. The first line of the file is 1, not 0.
您可以通过以下方式删除特定的单行及其行号。
这将删除第 33 行号上的行并保存更新的文件。
You can delete a particular single line with its line number by
This will delete the line on 33 line number and save the updated file.
还有 awk
and awk as well
这通常是反模式的症状。生成行号的工具很可能会被立即删除行的工具所取代。例如;
(其中
deletelines
是您想象中需要的实用程序)与话虽如此,如果您确实需要执行此任务,您可以生成一个简单的
sed
脚本来自行号文件。幽默地(但可能有点令人困惑)您可以使用 sed 来做到这一点。它接受一个行号文件,每行一个,并在标准输出上生成相同的行号,并在每个行号后附加 d 。这是一个有效的 sed 脚本,我们可以将其保存到文件中,或者(在某些平台上)通过管道传输到另一个 sed 实例:
在某些平台上,sed - f 不理解选项参数
-
来表示标准输入,因此您必须将脚本重定向到临时文件,并在完成后清理它,或者可能替换单独的如果您的操作系统(或 shell)有的话,请用/dev/stdin
或/proc/$pid/fd/1
破折号。与往常一样,您可以在
-f
选项之前添加-i
以使sed
就地编辑目标文件,而不是在标准输出。在 *BSDish 平台(包括 OSX)上,您还需要向-i
提供显式参数;一个常见的习惯用法是提供一个空的论证;-i ''
。This is very often a symptom of an antipattern. The tool which produced the line numbers may well be replaced with one which deletes the lines right away. For example;
(where
deletelines
is the utility you are imagining you need) is the same asHaving said that, if you are in a situation where you genuinely need to perform this task, you can generate a simple
sed
script from the file of line numbers. Humorously (but perhaps slightly confusingly) you can do this withsed
.This accepts a file of line numbers, one per line, and produces, on standard output, the same line numbers with
d
appended after each. This is a validsed
script, which we can save to a file, or (on some platforms) pipe to anothersed
instance:On some platforms,
sed -f
does not understand the option argument-
to mean standard input, so you have to redirect the script to a temporary file, and clean it up when you are done, or maybe replace the lone dash with/dev/stdin
or/proc/$pid/fd/1
if your OS (or shell) has that.As always, you can add
-i
before the-f
option to havesed
edit the target file in place, instead of producing the result on standard output. On *BSDish platforms (including OSX) you need to supply an explicit argument to-i
as well; a common idiom is to supply an empty argument;-i ''
.最短的,删除 sed 中的第一行
sed -i '1d' file
正如 Brian 所说 此处,使用
,<地址>
是<1>
和
。The shortest, deleting the first line in
sed
sed -i '1d' file
As Brian states here,
<address><command>
is used,<address>
is<1>
and<command>
<d>
.我想用 awk 提出一个概括。
当文件由固定大小的块组成时
并且每个块都会重复要删除的行,
awk 可以以这种方式正常工作
在这个例子中,块的大小是 2000,我想打印行 [1..713] 和 [1026..1029]。
NR
是awk用来存储当前行号的变量。%
给出两个整数相除的余数(或模数);nl=((NR-1)%BLOCKSIZE)+1
这里我们在变量nl中写入当前块内的行号。 (见下文)||
和&&
是逻辑运算符 OR 和 并且。print $0
写入整行I would like to propose a generalization with awk.
When the file is made by blocks of a fixed size
and the lines to delete are repeated for each block,
awk can work fine in such a way
In this example the size for the block is 2000 and I want to print the lines [1..713] and [1026..1029].
NR
is the variable used by awk to store the current line number.%
gives the remainder (or modulus) of the division of two integers;nl=((NR-1)%BLOCKSIZE)+1
Here we write in the variable nl the line number inside the current block. (see below)||
and&&
are the logical operator OR and AND.print $0
writes the full linecat -b /etc/passwd | 猫 -b /etc/passwd | sed -E 's/^( )+()(\t)(.*)/--删除---/g;s/^( )+([0-9]+)(\ t)//g'
cat -b
->打印带有数字的行s/^( )+()(\t)(.*)//g
->将行号替换为空(删除行)s/^( )+([0-9]+)(\t)//g
#removecat
打印的数字cat -b /etc/passwd | sed -E 's/^( )+(<line_number>)(\t)(.*)/--removed---/g;s/^( )+([0-9]+)(\t)//g'
cat -b
-> print lines with numberss/^( )+(<line_number>)(\t)(.*)//g
-> replace line number to null (remove line)s/^( )+([0-9]+)(\t)//g
#remove numbers thecat
printed删除所有json文件中的几行
sed -i '1,2d;53,54d;105,106d;157,158d' *.json
To delete several lines in all json files
sed -i '1,2d;53,54d;105,106d;157,158d' *.json