Linux 如何使用sed删除文件的最后n行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13380607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:47:37  来源:igfitidea点击:

How to use sed to remove the last n lines of a file

linuxbashshellsed

提问by mezda

I want to remove some nlines from the end of a file. Can this be done using sed?

我想从文件末尾删除一些n行。这可以使用sed完成吗?

For example, to remove lines from 2 to 4, I can use

例如,要删除 2 到 4 行,我可以使用

$ sed '2,4d' file

But I don't know the line numbers. I can delete the last line using

但我不知道行号。我可以使用删除最后一行

$sed $d file

but I want to know the way to remove nlines from the end. Please let me know how to do that using sed or some other method.

但我想知道从末尾删除n行的方法。请让我知道如何使用 sed 或其他方法来做到这一点。

采纳答案by ams

I don't know about sed, but it can be done with head:

我不知道sed,但可以通过以下方式完成head

head -n -2 myfile.txt

回答by richard

You could use headfor this.

你可以使用head

Use

$ head --lines=-N file > new_file

$ head --lines=-N file > new_file

where N is the number of lines you want to remove from the file.

其中 N 是要从文件中删除的行数。

The contents of the original file minus the last N lines are now in new_file

原始文件的内容减去最后 N 行现在在new_file 中

回答by Rishabh Sagar

With the answers here you'd have already learnt that sed is not the best tool for this application.

通过这里的答案,您已经了解到 sed 不是此应用程序的最佳工具。

However I do think there is a way to do this in using sed; the idea is to append N lines to hold space untill you are able read without hitting EOF. When EOF is hit, print the contents of hold space and quit.

但是我确实认为有一种方法可以使用 sed 来做到这一点;这个想法是附加 N 行来保存空间,直到您能够在不点击 EOF 的情况下阅读。当遇到 EOF 时,打印保留空间的内容并退出。

sed -e '$!{N;N;N;N;N;N;H;}' -e x

The sed command above will omit last 5 lines.

上面的 sed 命令将省略最后 5 行。

回答by Gilles Quenot

A funny & simple sedand tacsolution :

一个有趣而简单的sedtac解决方案:

n=4
tac file.txt | sed "1,$n{d}" | tac

NOTE

笔记

  • double quotes "are needed for the shell to evaluate the $nvariable in sedcommand. In single quotes, no interpolate will be performed.
  • tacis a catreversed, see man 1 tac
  • the {}in sedare there to separate $n& d(if not, the shell try to interpolate non existent $ndvariable)
  • "shell 需要双引号来评估命令中的$n变量sed。在单引号中,不会执行内插。
  • taccat反的,见man 1 tac
  • {}sed是否有分离$nd(如果不是,壳尝试插值不存在的$nd变量)

回答by potong

This might work for you (GNU sed):

这可能对你有用(GNU sed):

sed ':a;$!N;1,4ba;P;$d;D' file

回答by qstebom

From the sed one-liners:

sed one-liners

# delete the last 10 lines of a file
sed -e :a -e '$d;N;2,10ba' -e 'P;D'   # method 1
sed -n -e :a -e '1,10!{P;N;D;};N;ba'  # method 2

Seems to be what you are looing for.

似乎是你正在寻找的东西。

回答by SWW13

You can get the total count of lines with wc -l <file>and use

您可以使用wc -l <file>和使用获得总行数

head -n <total lines - lines to remove> <file>

head -n <total lines - lines to remove> <file>

回答by Kyle

If hardcoding n is an option, you can use sequential calls to sed. For instance, to delete the last three lines, delete the last one line thrice:

如果硬编码 n 是一个选项,您可以使用顺序调用 sed。例如,要删除最后三行,将最后一行删除三次:

sed '$d' file | sed '$d' | sed '$d'

回答by p014k

I prefer this solution;

我更喜欢这个解决方案;

head -$(gcalctool -s $(cat file | wc -l)-N) file

where Nis the number of lines to remove.

其中N是要删除的行数。

回答by DarkHeart

Most of the above answers seem to require GNU commands/extensions:

以上大部分答案似乎都需要 GNU 命令/扩展:

    $ head -n -2 myfile.txt
    -2: Badly formed number

For a slightly more portible solution:

对于稍微更便携的解决方案:

     perl -ne 'push(@fifo,$_);print shift(@fifo) if @fifo > 10;'

OR

或者

     perl -ne 'push(@buf,$_);END{print @buf[0 ... $#buf-10]}'

OR

或者

     awk '{buf[NR-1]=##代码##;}END{ for ( i=0; i < (NR-10); i++){ print buf[i];} }'

Where "10" is "n".

其中“10”是“n”。