使用 sed 从文本文件中删除特定行号?

发布于 2024-08-18 08:00:28 字数 43 浏览 11 评论 0原文

我想从文件中删除一个或多个特定行号。我将如何使用 sed 来做到这一点?

I want to delete one or more specific line numbers from a file. How would I do this using sed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

寂寞美少年 2024-08-25 08:00:28

如果要删除第 5 行到第 10 行以及第 12 行:

sed -e '5,10d;12d' file

这会将结果打印到屏幕上。如果要将结果保存到同一个文件中:

sed -i.bak -e '5,10d;12d' file

这会将未修改的文件存储为 file.bak,并删除给定的行。

注意:行号从 1 开始。文件的第一行是 1,而不是 0。

If you want to delete lines from 5 through 10 and line 12th:

sed -e '5,10d;12d' file

This will print the results to the screen. If you want to save the results to the same file:

sed -i.bak -e '5,10d;12d' 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.

醉酒的小男人 2024-08-25 08:00:28

您可以通过以下方式删除特定的单行及其行号。

sed -i '33d' file

这将删除第 33 行号上的行并保存更新的文件。

You can delete a particular single line with its line number by

sed -i '33d' file

This will delete the line on 33 line number and save the updated file.

乱了心跳 2024-08-25 08:00:28

还有 awk

awk 'NR!~/^(5|10|25)$/' file

and awk as well

awk 'NR!~/^(5|10|25)$/' file
昇り龍 2024-08-25 08:00:28
$ cat foo
1
2
3
4
5
$ sed -e '2d;4d' foo
1
3
5
$ 
$ cat foo
1
2
3
4
5
$ sed -e '2d;4d' foo
1
3
5
$ 
我家小可爱 2024-08-25 08:00:28

这通常是反模式的症状。生成行号的工具很可能会被立即删除行的工具所取代。例如;

grep -nh error logfile | cut -d: -f1 | deletelines logfile

(其中 deletelines 是您想象中需要的实用程序)与

grep -v error logfile

话虽如此,如果您确实需要执行此任务,您可以生成一个简单的 sed 脚本来自行号文件。幽默地(但可能有点令人困惑)您可以使用 sed 来做到这一点。

sed 's%$%d%' linenumbers

它接受一个行号文件,每行一个,并在标准输出上生成相同的行号,并在每个行号后附加 d 。这是一个有效的 sed 脚本,我们可以将其保存到文件中,或者(在某些平台上)通过管道传输到另一个 sed 实例:

sed 's%$%d%' linenumbers | sed -f - logfile

在某些平台上,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;

grep -nh error logfile | cut -d: -f1 | deletelines logfile

(where deletelines is the utility you are imagining you need) is the same as

grep -v error logfile

Having 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 with sed.

sed 's%$%d%' linenumbers

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 valid sed script, which we can save to a file, or (on some platforms) pipe to another sed instance:

sed 's%$%d%' linenumbers | sed -f - logfile

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 have sed 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 ''.

寂寞美少年 2024-08-25 08:00:28

最短的,删除 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>.

小嗲 2024-08-25 08:00:28

我想用 awk 提出一个概括。

当文件由固定大小的块组成时
并且每个块都会重复要删除的行,
awk 可以以这种方式正常工作

awk '{nl=((NR-1)%2000)+1; if ( (nl<714) || ((nl>1025)&&(nl<1029)) ) print  $0}'
 OriginFile.dat > MyOutputCuttedFile.dat

在这个例子中,块的大小是 2000,我想打印行 [1..713] 和 [1026..1029]。

  • NR是awk用来存储当前行号的变量。
  • % 给出两个整数相除的余数(或模数);
  • nl=((NR-1)%BLOCKSIZE)+1 这里我们在变量nl中写入当前块内的行号。 (见下文)
  • ||&& 是逻辑运算符 OR并且
  • print $0 写入整行

Why ((NR-1)%BLOCKSIZE)+1:
(NR-1) We need a shift of one because 1%3=1, 2%3=2, but 3%3=0.
  +1   We add again 1 because we want to restore the desired order.

+-----+------+----------+------------+
| NR  | NR%3 | (NR-1)%3 | (NR-1)%3+1 |
+-----+------+----------+------------+
|  1  |  1   |    0     |     1      |
|  2  |  2   |    1     |     2      |
|  3  |  0   |    2     |     3      |
|  4  |  1   |    0     |     1      |
+-----+------+----------+------------+

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

awk '{nl=((NR-1)%2000)+1; if ( (nl<714) || ((nl>1025)&&(nl<1029)) ) print  $0}'
 OriginFile.dat > MyOutputCuttedFile.dat

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 line

Why ((NR-1)%BLOCKSIZE)+1:
(NR-1) We need a shift of one because 1%3=1, 2%3=2, but 3%3=0.
  +1   We add again 1 because we want to restore the desired order.

+-----+------+----------+------------+
| NR  | NR%3 | (NR-1)%3 | (NR-1)%3+1 |
+-----+------+----------+------------+
|  1  |  1   |    0     |     1      |
|  2  |  2   |    1     |     2      |
|  3  |  0   |    2     |     3      |
|  4  |  1   |    0     |     1      |
+-----+------+----------+------------+

筱武穆 2024-08-25 08:00:28

cat -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 #remove cat 打印的数字

cat -b /etc/passwd | sed -E 's/^( )+(<line_number>)(\t)(.*)/--removed---/g;s/^( )+([0-9]+)(\t)//g'

cat -b -> print lines with numbers

s/^( )+(<line_number>)(\t)(.*)//g -> replace line number to null (remove line)

s/^( )+([0-9]+)(\t)//g #remove numbers the cat printed

街角卖回忆 2024-08-25 08:00:28

删除所有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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文